Re: Allocating too much memory kills my App rather than returning NULL
Re: Allocating too much memory kills my App rather than returning NULL
- Subject: Re: Allocating too much memory kills my App rather than returning NULL
- From: Conrad Shultz <email@hidden>
- Date: Tue, 08 Nov 2011 22:39:24 -0800
On 11/8/11 9:43 PM, Don Quixote de la Mancha wrote:
> It would be enough if there were some way I could check ahead of time
> how much I can safely allocate, without actually trying to allocate
> it. Is there an API for that?
I don't see how there COULD be an API for that for the simple reason
that the phone is not a static device, and your app does not run in a
vacuum. Phone calls can come in, alerts can fire, background processes
can run. At best you have a race condition.
This seems very much like the "how do I check for server availability?"
question that comes up on the list every couple months, with the
inevitable response of "try it."
> A bad workaround would be to just empirically determine how much
> memory is safe to allocate by trying various grid sizes. A binary
> search would determine that pretty quickly for each of my devices.
> But that is not the desired solution, because future hardware models
> will likely have more physical RAM. Also the amount that can be
> safely allocated would depend on how much is left over from other
> processes, and so could not be counted upon to be any particular fixed
> amount.
And there's a bigger problem. That memory warning notification that you
referenced in your first message is there to tell you that you need to
free up resources ASAP at run-time. Quoting from the iOS App
Programming Guide:
"Using large amounts of memory can seriously degrade system performance
and potentially cause the system to terminate your app."
You don't just get to hold onto whatever memory the OS can allocate at
some arbitrary point in your app's life cycle. iOS will keep system
services (from telephony on down) running at all costs, and (AFAICT)
will keep Apple-supplied background tasks running as well (e.g.
iPod/music app). Under low memory conditions, it will terminate
suspended apps first but will eventually kill your app too, even if it's
running in the foreground.
You are expected to allocate as LITTLE memory as possible, make caches
discardable, and play nicely with memory warnings.
--
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________
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