Managing Memory Efficiently
When it comes to view controllers and memory management, there are two issues to consider:
How do you allocate memory efficiently?
When and how do you release memory?
Although some aspects of memory allocation are strictly yours to decide, there are a handful of methods in the UIViewController class that usually have some connection to memory management tasks. Table 4-1 lists the places in your view controller object where you are likely to allocate or deallocate memory, along with information about what you should be doing in each place.
Task | Methods | Discussion |
|---|---|---|
Allocating critical data structures required by your view controller | Initialization methods | Your custom initialization method (whether it is named |
Creating your view objects | Overriding the | |
Allocating or loading data to be displayed in your view | Typically, data objects are provided by configuring your view controller’s properties. Any additional data objects your view controller wants to create should be done by overriding the | |
Releasing references to view objects | If you maintain strong references to any view objects in your view hierarchy using outlets or other instance variables in your class, you must always release those references when the views are no longer needed. You release a view object by setting your outlet or variable to | |
Releasing data that is not needed when your view is not displayed | You can use the | |
Responding to low-memory notifications | Use this method to deallocate all noncritical custom data structures associated with your view controller. Although you would not use this method to release references to view objects, you might use it to release any view-related data structures that you did not already release in your | |
Releasing critical data structures required by your view controller | Override this method only to perform any last-minute cleanup of your view controller class. Objects stored in instance variables and properties are automatically released; you do not need to release them explicitly. |
浙公网安备 33010602011771号