Testing for Memory Warnings

Have you ever opened up an app on your iPhone or iPad and noticed that it crashed immediately? Then it was OK when you restarted it?

That’s usually because there is a bug in the way the application responds to the memory management processes in iOS.

Memory Management in iOS

One of the objectives of iOS memory management is that the user of the device shouldn’t have to worry about closing apps — it’s done automatically by iOS. When the home button is pressed on a device the foreground app goes into a frozen background state where it is still consuming memory but is no longer a burden on the processor.

As other apps are opened, iOS attempts to free memory by delivering memory warnings to all backgrounded apps. By default, an app frees memory consumed by views that weren’t visible when it was backgrounded. The idea is that the app can come back to life quickly and regenerate views as the user navigates to them.

You can tap into this process and be a good citizen within iOS by freeing objects in your view controllers when told about these memory warnings. iOS tells your app when a view is unloading and unloading using the viewDidUnload and viewDidLoad methods respectively (but look out for changes to this when iOS 6 comes out). The basic rule for freeing up objects is that as long as you can recreate the state of an object when a view reloads, it is safe to discard it when the view unloads. User-interface controls (outlets) and lazy properties are good candidates.

If memory gets really short, frozen apps are terminated completely. Sometimes you’ll notice that apps that haven’t been used for a while re-display their splash screen. That’s because the app has been terminated and needs to restart from scratch.

Testing for Memory Warnings with the Simulator

The iOS simulator has a menu option called “Simulate Memory Warning”. This is a really useful tool for triggering bugs that might otherwise appear intermittently in deployed app.

As part of your testing you should really try a memory warning from each visible view in your application. If you then get crashes or weird behavior, you need to investigate what you are freeing up that you shouldn’t be; or what you are failing to re-create that you should be. Often the culprit will be an over-zealous viewDidUnload method but bugs are bugs and there could be lots of reasons.

Careful testing with memory warnings can avoid obscure crash logs coming back from deployed apps, perhaps days, weeks or even months after you submitted to the App Store and is highly recommended.

You can learn more about memory management and the view lifecycle on Learning Tree’s iPhone® and iPad® Programming: A Comprehensive Hands-On Introduction course.

Richard Senior

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.

maltcasino maltcasino maltcasino bedava bahis güvenilir bahis siteleri canlı bahis siteleri
bedava bahis Yatırımsız Deneme Bonusu bonus veren siteler