Aug 202009
 

Predicting user behavior on the iPhone is difficult. One can never know how many pages of Safari someone might have in the background hogging up valuable resources. Every now and then, I get reports of our application crashing for no apparent reason. More often than not, it’s caused by the phone running low on memory. It’s amazing how little memory we have to work with on the iPhone. With Safari taking up 8 pages in the background, our application was crashing at mere 8 MB of memory usage.

Load Late and Lazily

Sometimes you have to sacrifice the snappy UI for a low memory footprint. It’s better than your application crashing. This means never preloading resources but waiting until the moment you need it. If your XIB (NIB) file has a lot of views, try separating them into multiple NIB files so that you can load it individually when you need it. When a view is not visible, the system will free it from memory.

- (void) didReceiveMemoryWarning

The didReceiveMemoryWarning message will be called when the iPhone is running low on memory. It is highly recommended that you override this message in all your view controllers and print out a simple debug message in it. Also, do not forget to call [super didReceiveMemoryWarning].

- (void) setView:(UIView*) view

If you receive the didReceiveMemoryWarning message, the setView message will be called with a nil parameter. It will be called to release the view if the view is not the active view. This is where you will want to release as much resources as you can, such as all your IBOutlet variables. Also, do not forget to call [super setView:view] or your application will crash.

Initialization

Once your view is freed, the NIB will be read back into memory when it is needed. This means the viewDidLoad message will be called again. So take care not to place initialization code that should only be initialized once per application in that message.

CLANG Analyzer

If you haven’t tried this tool already, then you are truly missing out. Check out this extremely helpful post by Jeff Lamarche, the author of Beginning iPhone Development. CLANG will analyze your code and give you a detailed report of problems your code may have, including memory leaks.

Also, If you’ve missed my previous post about the dangers of UIImage, you can check it out here.

 Posted by at 8:22 pm

  5 Responses to “Handling didReceiveMemoryWarning”

  1. I have error in
    – (void)didReceiveMemoryWarning {
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren’t in use.
    }
    and the system says that required memory undeclared in program and this is first time that it is declared, and when i have used before creating the table, it was working and database was created.
    and how to show the database contents in the IPhone development kit, in MAC pro, mean what is the procedure to reach up to that spacific folder…

  2. > The didReceiveMemoryWarning message will be called when the iPhone is running load on memory.

    What is a “running load on memory”?

    (And why are there always so many errors on this site???)

    • Meant to say running “low”.

      What kind of errors? Do you have javascript enabled? It’s a normal wordpress site. Seems to work fine on Chrome, IE, Safari and Firefox.

  3. What would be some actually code-fragments that you would put inside didReceiveMemoryWarning?

    I can’t really think of a single thing to “free”. Everything I’m doing has either been:

    1. Immediately freed as soon as I’m done with it.

    2. Can’t be freed… because I still need it, and my app is still using it.

    Is getting a memory-warning more likely to mean:

    A. You are leaking memory or writing very buggy code.

    B. You just have a very feature-full app… and there’s nothing you can do about it.
    (Other than start removing all those great features.)

    • If that’s the case, then I don’t see any other way but to redesign the app to use less memory, being very careful about memory management and in the worst case, remove features that are causing issues.

      You can always dump the contents of the memory onto the file system if you aren’t actively doing anything with the data. But that may not be feasible (if you are actively using the data). Not to mention bad performance.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>