--from 《iPhone Application Development For Dummies - Neal Goldstein》
Observing low-memory warnings
When the system dispatches a low-memory notification to your application, itis something you must pay attention to. If you don’t, it is a reliable recipe fordisaster. (Think of your low-fuel light going on as you approach a sign thatsays “Next services 100 miles.”) UIKit provides several ways of setting upyour application so that you receive timely low-memory notifications:
-
✓ Implement theapplicationDidReceiveMemoryWarning:method ofyour application delegate. Your application delegate could then releaseany data structure or objects it owns — or notify the objects to releasememory they own.
-
✓ Override thedidReceiveMemoryWarning:method in your customUIViewControllersubclass. The view controller could then releaseviews — or even other view controllers — that are off-screen.
✓ Register to receive theUIApplicationDidReceiveMemoryWarningNotification:notification. A model object could then release datastructures or objects it owns that it doesn’t need immediately and canre-create later.
Each of these strategies gives a different part of your application a chance tofree up the memory it no longer needs (or doesn’t need right now). As for howyou actually get these strategies working for you, while I will mention a strat-egy for implementing the view controller’sdidReceiveMemoryWarning:inChapter 14, this is something that is dependent on your application’s architec-ture. That means you’ll need to explore it on your own.
Not freeing up enough memory will result in the iPhone’s OS sending yourapplication theapplicationWillTerminate:message and shutting youdown. For many applications, though, the best defense is a good offense,and you need to manage your memory effectively and eliminate any memoryleaks in your code.