开发者

Memory warning level in applicationDidReceiveMemoryWarning

开发者 https://www.devze.com 2023-01-10 20:23 出处:网络
As far as I know there are 2 levels of memory warnings. applicationDidReceiveMemoryWarning doesn\'t come with the warning level. Is there a开发者_开发百科 way to get this information?I\'m adding this

As far as I know there are 2 levels of memory warnings. applicationDidReceiveMemoryWarning doesn't come with the warning level. Is there a开发者_开发百科 way to get this information?


I'm adding this as a separate answer than my other one, because it IS a separate answer. The answer I gave earlier is the practical one, the one you SHOULD use. It boils down to "deal with the one level of memory warning you're notified with and don't concern yourself with the details".

If, however, you really really want to know more about it, check out this undocumented API: http://www.opensource.apple.com/source/Libc/Libc-594.1.4/include/libkern/OSMemoryNotification.h

That's the header for kernel code that generates memory warnings, and it declares the following typedef:

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

We obviously don't have the implementation of the key method (OSMemoryNotificationCurrentLevel()) that does the math to say which warning to return, but that's the list of possible return values.

You could use the Mach.h library to test the actual memory levels on the device (let me know if you want some code that does that), and then use OSMemoryNotificationCurrentLevel() to get the current level of memory warning, and really map out the notification level to the physical state of the machine. Such an app would be completely unwelcome on the app store, and it wouldn't really help you any in real life development, where your job is to respond to the single level of low memory warning you receive.


It's not that there are two levels of warning--not in terms of, like, "you have a little memory left" and then "you have less memory left". It's that the same "low memory" warning gets fired two places. Once on the UIViewController subclass currently in view (and, if that UIVC doesn't implement didReceiveMemoryWarning, then it'll bubble up to the top of the view controller stack) and again on the Application Delegate in applicationDidReceiveMemoryWarning. One's not a "worse" warning than the other, they're just two different hooks for implementing a response to the same warning from the OS.

It's one of the quirks of the platform that you can't really know how much memory you'll have available at any time. Background apps (mail, phone, etc) suck as much RAM as they need, and you're left to get the rest, and the only way you can know you've gotten too big is when the OS says so.

It's important to respond appropriately, by ditching resources you don't need right now. Start with the low-hanging fruit--anything big you're keeping around for later. Images, for instance, that you can ditch and then pull from a server or from disk later when you need them again.

0

精彩评论

暂无评论...
验证码 换一张
取 消