Regarding the black art of managing memory on iPhone OS devices: what do the different levels of memory warning mean. Level 1? Level 2? Does the dial go to 11?
Context: After an extensive memory stress testing period - including running my iPad app with the iPod music player app playing, I am inclined to ignore the random yet infrequent memory warnings I am receiving. My app never crashes. Ever. My app is leak free. And, well, the mems warnings just don't seem to matter.
Thanks,
Doug
From OSMemoryNotification.h,
totoal 5 levels of memory warning (-1,3).
Regarding Memory Level warning description, @KennyTM's answer is excellent.
I want to add several related points which may help PM and others.
What should you do when having Memory Level Warning?
Upon receiving any of these warnings, your handler method should respond by immediately freeing up any unneeded memory. For example, the default behavior of the UIViewController class is to purge its view if that view is not currently visible; subclasses can supplement the default behavior by purging additional data structures. An app that maintains a cache of images might respond by releasing any images that are not currently onscreen.
How to observe Memory Level warning?
From http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/PerformanceTuning/PerformanceTuning.html
When the system dispatches a low-memory warning to your app, respond immediately. iOS notifies all running apps whenever the amount of free memory dips below a safe threshold. (It does not notify suspended apps.) If your app receives this warning, it must free up as much memory as possible. The best way to do this is to remove strong references to caches, image objects, and other data objects that can be recreated later.
UIKit provides several ways to receive low-memory warnings, including the following:
How to Reduce Your App’s Memory Footprint?
Details at http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/PerformanceTuning/PerformanceTuning.html
How to allocate memory wisely?
Memory level warnings are logged by SpringBoard. As an app developer you don't need to care about it. Just responding to
-{application}didReceiveMemoryWarning
is enough.There are 4 levels of warnings (0 to 3). These are set from the kernel memory watcher, and can be obtained by the not-so-public function
OSMemoryNotificationCurrentLevel()
.How the levels are triggered is not documented. SpringBoard is configured to do the following in each memory level:
Killing the active app (jetsam) is not handled by SpringBoard, but
launchd
.Basically the warnings mean that the device is running low on memory, and that, "If you could please free some memory you aren't actively using that'd be swell!". If your memory management is tight, and you have no objects that could practically be discarded, just pass the message along and ignore it.