How to detect level = 2 warning in iOS?

Apple defines lev = 1 and lev = 2 memory warnings as very different (one is “Hmm, there is not much memory around. Be careful,” the other is “I'm going to kill a process, you have 5 ... 4 .. . 3 ... ")

... but how do you know programmatically which one you got?

I have an iPad app that processes images with special tools, and lv = 1 warnings of memory are inevitable - even when processing the image in small parts, it still uses a lot of memory to display the entire graphical interface, etc.

I get lev = 1 ALL TIME, and there literally can't do anything about it.

lev = 2 memory warnings is another matter entirely. If I get lev = 2, it usually means that some background application takes up a lot of memory or something that the user has done has bloated memory - and I need to take emergency measures so that I don't die. There are things that I can do, but they are all bad for the user (for example, delete the entire GUI, wait a couple of seconds, and then restart it).

So ... I need two different code paths inside "didRecieveMemoryWarning": one for lev = 1 warnings and one for lev = 2 warnings.

EDIT: Warning Level Information: iPhone OS Memory Alerts. What do the different levels mean?

NB: Apple often doesn't “document” things. If we, the developers, adhere to only documented information and without errors in the API, then many (most?) Applications that expand the boundaries will not exist until several iOS releases are later. Instead, we work on bugs, and we develop what actually happens where Apple cannot document it.

EDIT2: ... looking at the associated header file, it looks like it got the main private function that Apple uses to determine the current level: "OSMemoryNotificationLevel OSMemoryNotificationCurrentLevel (void)" - but I think we could not get this past Apple Submission :(?

+8
ios
source share
2 answers

I don’t think that the warning level is available for applications, but ... and I know that this is not what you want to hear - it does not really matter. Just because the OS has two different memory levels, this does not mean that you have to do different things at each level and hope that other applications do the right thing.

The documentation says:

Implementing this method is highly recommended. If your application does not allocate enough memory under low memory conditions, the system may end it directly.

You cannot talk about "caution" or "about killing." It may be what is happening now, but is it the same in iOS5? Or in later versions? It is very dangerous to make such assumptions.

I think you need to consider optimizing your memory usage. Almost always there is the opportunity to optimize images, release cached / intermediate data, more efficiently use autocomplete pools, smaller / more efficient data structures. You do not say what methods you are currently using, so it is difficult to be specific.

+6
source share

An alternative approach that I DID NOT SPECIFY, but may work. Grab the output of Apple System Logger (ASL), find the warning line lev2.

Assuming Apple is actually using ASL (which they may not be!), You can try the following instructions: http://www.cocoanetics.com/2011/03/accessing-the-ios-system-log/ - which show how to access ASL using Apple’s public APIs.

0
source share

All Articles