Home / iPhone Tips and Tutorials

Low-Memory Warnings

Even if you have done everything correctly, in a large application you may simply run out of memory. When that situation occurs, the system dispatches a low-memory notification to your application - and it's something you must pay attention to. If you don't, it's a reliable recipe for disaster.

(Think of your low-fuel light going on as you approach a sign on the highway that says "Next services 100 miles.") UIKit provides several ways for you to set up your application so that you receive timely low-memory notifications:

  • Override the viewDidUnload and didReceiveMemoryWarning methods in your custom UIViewController subclass.
  • Implement the applicationDidReceiveMemoryWarning: method of your application delegate.
  • Register to receive the UIApplicationDidReceiveMemoryWarningNotification: notification.

The viewDidUnload method

When a low-memory condition occurs, one way to free some memory is to remove views that are not being used. You don't have to worry about that, however, because it is handled in the view controller class from which you derive your view controllers, such as RTViewController in the RoadTrip project.

What you do have to worry about, though, is managing any references to the view or its subviews that you have.

That's why Interface Builder so kindly added the following code to your project:

- (void)viewDidUnload
{
  [self setCar:nil];
  [self setTestDriveButton:nil];
  [self setBackgroundImage:nil];
  [super viewDidUnload];
}

Here you set the references to the outlets you created to nil, which will tell ARC to generate the code to release these references and which will enable them to be released from memory. You can also use this method to release any objects that you created to support the view but that are no longer needed now that the view is gone, by setting them to nil as well. You should not use this method to release user data or any other information that cannot be easily recreated.

The didReceiveMemoryWarning method

The didReceiveMemoryWarning is sent to the view controller when the application receives a memory warning. The default implementation of this method checks to see whether the view controller can safely release its view. If the view can be released, this method releases it and sends the viewDid- Unload message (see previous section).

You should override this method to release any additional memory used by your view controller, but be sure to send the [super didReceiveMemory- Warning] to allow the view controller to release its view applicationDidReceiveMemoryWarning: Similar to viewDidUnload, your Application Delegate should set any references to objects it can safely free to nil.

UIApplicationDidReceiveMemoryWarning Notification: notification

Low memory notifications are sent to the notification center, where all notifications are centralized. An object that wants to get informed about this notification 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. 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.

However for those of you who are curious, in your model object, you could add:

[[NSNotificationCenter defaultCenter] addObserverForName:
  UIApplicationDidReceiveMemoryWarningNotification
  object:[UIApplication sharedApplication] queue:nil
  usingBlock:^(NSNotification *notif) {
//your code here
}];
Tip
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 application 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 application's architecture, so you'll have to explore that on your own.

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

[Previous] [Contents] [Next]