业精于勤,荒于嬉
全部博文(763)
分类:
2010-09-24 17:21:32
When the system dispatches a low-memory warning to your application, heed it. iOS notifies the frontmost application whenever the amount of free memory dips below a safe threshold. If your application receives this warning, it must free up as much memory as it can by releasing objects it does not need or clearing out memory caches that it can easily recreate later.
UIKit provides several ways to receive low-memory warnings, including the following:
Implement the applicationDidReceiveMemoryWarning:
method of your application delegate.
Override the didReceiveMemoryWarning
method in your custom UIViewController
subclass.
Register to receive the UIApplicationDidReceiveMemoryWarningNotification
notification.
Upon receiving any of these warnings, your handler method should respond by immediately freeing up any unneeded memory. For example, the UIViewController
class responds by automatically purging its view if that view is not currently visible; subclasses can supplement the default behavior by purging additional data structures. An application that maintains a cache of images might respond by releasing any images that are not currently onscreen.
If your custom objects have known purgeable resources, you can have those objects register for theUIApplicationDidReceiveMemoryWarningNotification
notification and release their purgeable resources directly. Have these objects register if you have a few objects that manage most of your purgeable resources and if it is appropriate to purge all of those resources. But if you have many purgeable objects or want to coordinate the release of only a subset of those objects, you might want to use your application delegate to release the desired objects.
Important: Like the system applications, your applications should always handle low-memory warnings. System applications consume small amounts of memory while processing requests. When a low-memory condition is detected, the system delivers low-memory warnings to all running programs (including your application) and may terminate some background applications (if necessary) to ease memory pressure. If not enough memory is released—perhaps because your application is leaking or still consuming too much memory—the system may still terminate your application.
You can test your application’s behavior under low-memory conditions using the Simulate Memory Warning command in the simulator.