Re: Memory not being released in a timely manner
Re: Memory not being released in a timely manner
- Subject: Re: Memory not being released in a timely manner
- From: Jeff Johnson <email@hidden>
- Date: Tue, 04 Jun 2013 10:30:34 -0500
Hi Johnny.
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.
We've run into this issue in a number of apps. As a workaround, we add a timer to the main thread and have it fire periodically. The timer's action method does the following:
NSEvent *event = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint modifierFlags:0 timestamp:[NSDate timeIntervalSinceReferenceDate] windowNumber:0 context:nil subtype:0 data1:0 data2:0]
[NSApp postEvent:event atStart:YES];
This basically "tickles" the event loop and causes the autorelease pool to drain.
-Jeff
On Jun 3, 2013, at 10:59 AM, Jonathan Taylor <email@hidden> wrote:
> Hi all,
>
> Can anybody advise what tools I should be using to debug a particular out of memory condition - and maybe even how I can fix it? The error that I ultimately encounter (in a 32-bit application on Snow Leopard) is:
> 2013-06-03 15:44:30.271 MyApplication[25115:a0f] NSImage: Insufficient memory to allocate pixel data buffer of 1228800 bytes
> However as I'll explain I don't feel that I am doing anything that should result in running out of memory.
>
> The program loads and processes a large number (~2000) image files, and I believe that I am disposing of memory and draining autorelease pools correctly. Running the program under ObjectAlloc, it never reports memory usage over 300MB. The problem seems to be that, even though ObjectAlloc thinks the memory has been released, it is not actually being properly freed up until some time later. The memory usage as reported in Activity Monitor climbs higher and higher until, if left unattended, there is apparently no more memory available. Clicking the mouse or switching applications causes an immediate and significant drop in memory usage.
>
> Thus the situation seems to be that I believe I am doing everything I can for good memory management, but the OS is not actually freeing things up for re-use. I encountered a similar problem that I asked about on here last year:
> http://lists.apple.com/archives/cocoa-dev/2012/Jul/msg00602.html
> Although in that case the eventual conclusion was that I should be using a different approach in my code, it involved the same issue of memory not being released when I would have expected it to be. In that case from last year, I was wrong to rely on it being released when I expected, but I feel that this time it should be reasonable to expect that I won't run out of memory entirely, given that I am releasing all the buffers that I am finished with and only keeping a small working set allocated!
>
> This is of course partly just speculation because ObjectAlloc isn't giving any info about this unavailable but apparently not-allocated memory. So...
> 1. Can anybody recommend a way of debugging this problem to get more concrete evidence for what is actually happening?
> 2. Assuming my interpretation is correct, can anybody suggest a solution?
>
> Many thanks
> Jonny
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden