IPhone OS memory alerts. What do the different levels mean?

As for the black art of memory management on iPhone OS devices: what do different levels of memory mean. 1st level? Level 2? Does the dial go to 11?

Context: after an extensive period of stress testing the memory β€” including launching my iPad app with an iPod music player application, I tend to ignore the random but rare memory warnings I receive. My application never works. Ever. My application does not contain leaks. And, well, warnings about memes just don't seem important.

Thank,
Doug

+84
memory-management warnings ipad
May 26 '10 at 17:16
source share
3 answers

Basically, warnings mean that the device is running low in memory, and that: β€œIf you could free some memory that you are not actively using, it will be swell! ”. If your memory management is tight and you don’t have objects that can be practically discarded, just send a message and ignore it.

+95
May 26 '10 at 17:41
source share

Memory level alerts are logged by SpringBoard. As an application developer, you do not need to worry about this. Just answering -{application}didReceiveMemoryWarning enough.




There are 4 warning levels (0 to 3). They are set from the kernel memory observer and can be obtained using the not so public function OSMemoryNotificationCurrentLevel() .

 typedef enum { OSMemoryNotificationLevelAny = -1, OSMemoryNotificationLevelNormal = 0, OSMemoryNotificationLevelWarning = 1, OSMemoryNotificationLevelUrgent = 2, OSMemoryNotificationLevelCritical = 3 } OSMemoryNotificationLevel; 

How levels are launched is not documented. SpringBoard is configured to perform the following operations at each memory level:

  • Warning (not normal). Restart or start automatic reuse of non-essential background applications, for example. Post office.
  • Urgent - close all background applications, for example. Safari and iPod.
  • Critically further - The kernel will take effect, possibly by killing SpringBoard or even rebooting.

Killing the active application (jetsam) is not performed by SpringBoard, but launchd .

+192
May 26 '10 at 17:56
source share

From OSMemoryNotification.h ,

 /* ** Threshold values for notifications */ typedef enum { OSMemoryNotificationLevelAny = -1, OSMemoryNotificationLevelNormal = 0, OSMemoryNotificationLevelWarning = 1, OSMemoryNotificationLevelUrgent = 2, OSMemoryNotificationLevelCritical = 3 } OSMemoryNotificationLevel; 

totoal 5 levels of memory warning (-1.3).

Regarding the memory alert, @KennyTM's answer is excellent.

I want to add some related points that can help PM and others.




What if you have a memory level warning?

After receiving any of these warnings, your handler method should respond by immediately freeing up any unnecessary memory. For example, the default behavior of the UIViewController class is to clear its view if that view is not currently displayed; subclasses can complement the default behavior by clearing additional data structures. An application that supports the image cache can respond by releasing any images that are not currently displayed on the screen.




How to observe a warning about the memory level?

From http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/PerformanceTuning/PerformanceTuning.html

When the system sends a low memory alert to your application, respond immediately. iOS notifies all running applications whenever the amount of free memory falls below a safe threshold. (It does not notify suspended applications.) If your application receives this warning, it should free up as much memory as possible. The best way to do this is to remove strong cache links, image objects, and other data objects that you can recreate later.

UIKit provides several ways to receive low memory alerts, including the following:

  • Implementing applicationDidReceiveMemoryWarning: Your application's delegate method.
  • Override the didReceiveMemoryWarning method in your regular UIViewController Subclass.
  • Register to receive UIApplicationDidReceiveMemoryWarningNotificationnotification.



How to reduce application memory?

  • Repair memory leaks.
  • Make resource files as small as possible.
  • Use master data or SQLite for large datasets.
  • Loading resources is lazy.
  • Build your program using the Thumb option.

Details http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/PerformanceTuning/PerformanceTuning.html




How to allocate memory wisely?

  • Reduce the use of auto-implemented objects: with automatic reference counting (ARC), it is better to select / initialize objects and the compiler will release them for you at the appropriate time. This is true even for temporary objects that may have been auto-implemented in the past to prevent their current method.
  • Introduce resource size limits: avoid loading a large resource file when it does a smaller one. Instead of using a high-resolution image, use the one that matches the size for iOS devices. if you must use large resource files, find ways to download only parts of the file that you need at any given time. For example, instead of loading the entire file into memory, use the mmap and munmap functions to display parts of the file in and out of memory. For more information about mapping files to memory.
  • Avoid unlimited problem sets: for unlimited problem sets, you may need an arbitrarily large amount of data to compute. If the kit requires more than is available, your application may not be calculating. Your applications should avoid such sets when possible, and work on issues with known memory limitations.
+12
Apr 29 '13 at 5:32
source share



All Articles