How to Observe Low-Memory Warnings in Your iOS App - dummies

How to Observe Low-Memory Warnings in Your iOS App

By Jesse Feiler

Even if you’ve done everything correctly, in a large iOS app, you may simply run out of memory. When that situation occurs, the system dispatches a low-memory notification to your app — and it’s something you must pay attention to. If you don’t, it’s a reliable recipe for disaster. UIKit provides several ways for you to set up your app so that you receive timely low-memory notifications:

  • Override the didReceiveMemoryWarning methods in your custom UIViwComtroller subclass.

  • Implement the applicationDidReceiveMemoryWarning: method of your application delegate.

  • Register to receive the UIApplicationDidReceiveMemoryWarningNotification: notification.

The didReceiveMemoryWarning method

The didReceiveMemoryWarning method is sent to the view controller when the app receives a memory warning. Your implementation of this method should do anything it can to reduce its use of memory. This may involve drastic steps such as actually closing views, but there are many other steps you can take that depend on your specific view controller and its views.

For example, if you have stored data and objects, they may be able to be removed and set to nil if you can recreate them as needed.

The applicationDidReceive MemoryWarning: method

Your application delegate should set any references to objects it can safely free to nil.

The UIApplicationDidReceiveMemory WarningNotification: notification

Low-memory notifications are sent to the Notification Center, where all notifications are centralized. An object that wants to get informed about any notifications registers itself to the Notification Center by telling which notification it wants to be informed about, and a block to be called when the notification is raised. Instead of a block, you can supply a target method to be called.

A model object, for example, could then release data structures or objects it owns that it doesn’t need immediately and can re-create later by setting references to nil.

For those of you who are curious, in your model object, you could add the following:

[[NSNotificationCenter defaultCenter] addObserverForName:
 object:[UIApplication sharedApplication] queue:nil
 usingBlock:^(NSNotification *notif) {
              //your code here

You can test applicationDidReceiveMemoryWarning: and UIApplicationDidReceiveMemoryWarningNotification: in the Simulator by choosing Hardware→Simulate Memory Warning.

Picking the right memory-management strategy for your application

Each of these strategies gives a different part of your app a chance to free the memory it no longer needs (or doesn’t need right now). How you actually get these strategies working for you depends on your app’s architecture, so you’ll have to explore that on your own.

Not freeing enough memory will result in iOS sending your app the applicationWillTerminate: message and shutting the app down. For many apps, though, the best defense is a good offense, which means you need to manage your memory effectively and eliminate any memory leaks in your code by following the ARC rules.

For testing, implement all of these methods and place NSLog statements in them along with breakpoints so that you can see the memory issues as they happen. It’s easy to pinpoint places where you can free up memory, but it’s most efficient to address the ones that provide the biggest bang for your programming buck.