Memory leak: _NSActivityAssertion

Greetings,


I'm running Instruments looking for memory leaks in my (macOS) application. It's a data intensive app that uses a lot of asynchronous processing.


I did find some memory leaks of my own making, and have improved things significantly, but now what floats to the top of the allocataions list are _NSActivityAssertion objects ... millions of them.


It appears that one of these gets allocated every time an NSOperation object is created. My app has been running for an hour now, and there are 40,000,000+ of these objects, occupying over 3GB of memory.


Is this normal, a Cocoa bug, something I'm doing wrong, or a side effect of using the Allocations instrument?

Replies

Update: after running another hour, all of the_NSActivityAssertion objects have disappeared.


The applciation works in phases, so it's possible these were associated with the NSOperationQueue, and when that queue was destroyed everything got cleaned up.


Even so, 3GB of RAM seems like a bug, even if it's transient.

Did you call any of the NSProcessInfo methods in the NSProcessInfoActivity category? It appears that's where these objects come from, and they're placed into the autorelease pool. It's possible the system is using this interface on your behalf. What does the creation callstack look like? Do you happen to have an old trace when this was happening?


3GB is rough. If you feel like it could be a bug, then filing a bug report will help, even when you think we probably have already seen this before.

The NSActivityAssertion objects are being created by -[NSProcessInfo(NSProcessInfoActivity) beginActivityWithOptions:reason:], which is being called by -[NSOperation init] when I create my custom subclass of NSOperation.


But you are correct, they were being added to the autorelease pool. (This fact was not at all obvious in Instruments. But to be honest, memory management in the era of ARC is half science, half dark magic.)


I wrapped an autoreleasepool directive around the -[NSOperation init] call, and no more leak!


Thanks for the nudge in the right direction.