Re: Strange Memory Patterns

Alex Zavatone

Nice observation, Jonathan.  Do you suspect that throwing them on their own queue that you made would be an easy way to solve that?

I’d try it out myself, but am not in the best location to do that at the moment.  


Alex Zavatone

On Apr 25, 2019, at 10:44 AM, Jonathan Taylor <jonathan.taylor@...> wrote:

This sounds very similar to problems I have encountered myself.

Issue 1: GCD callbacks are part of an autorelease pool, but you can't rely on it getting drained in a reliable manner, so wrap your callbacks with your own explicit pool.

Issue 2: Background apps do not seem to always have their main thread autorelease pools drained reliably. A snippet of code I've posted a few times over the years:

// Create a periodic timer that "tickles" the main event loop to drain autorelease pools.
// Response from cocoa-dev discussion was that:
//  This is a long-standing problem with AppKit. According to the documentation,
//  "The Application Kit creates an autorelease pool on the main thread at the
//  beginning of every cycle of the event loop, and drains it at the end, thereby
//  releasing any autoreleased objects generated while processing an event."
//  However, this is somewhat misleading. The "end" of the event loop cycle is
//   immediately before the beginning. Thus, for example, if your app is in the background
//   and not receiving events, then the autorelease pool will not be drained. That's why
//   your memory drops significantly when you click the mouse or switch applications.
[JDispatchTimer allocRepeatingTimerOnQueue:dispatch_get_main_queue() atInterval:5.0 withHandler:^{
NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:[NSDate timeIntervalSinceReferenceDatewindowNumber:0 context:nil subtype:0 data1:0 data2:0];
[NSApp postEvent:event atStart:YES];

Join to automatically receive all group messages.