导航

Managing Memory Efficiently

Posted on 2012-02-19 23:33  残影无伤  阅读(196)  评论(0)    收藏  举报

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.

Table 4-1  Places to allocate and deallocate memory

Task

Methods

Discussion

Allocating critical data structures required by your view controller

Initialization methods

Your custom initialization method (whether it is named init or something else) is always responsible for putting your view controller object in a known good state. This includes allocating whatever data structures are needed to ensure proper operation.

Creating your view objects

loadView

Overriding the loadView method is required only if you intend to create your views programmatically. If you are using storyboards, the views are loaded automatically from the storyboard file.

Allocating or loading data to be displayed in your view

viewDidLoad

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 viewDidLoad method. By the time this method is called, your view objects are guaranteed to exist and to be in a known good state.

Releasing references to view objects

viewDidUnload

dealloc

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 nil. For more information about when views get released, see “Understanding How Views Are Loaded and Unloaded.”

Releasing data that is not needed when your view is not displayed

viewWillUnload

viewDidUnload

You can use the viewDidUnload method to deallocate any data that is view specific and that can be re-created easily enough if the view is loaded into memory again. If re-creating the data might be too time-consuming, though, you do not have to release the corresponding data objects here. Instead, you should consider releasing those objects in your didReceiveMemoryWarning method.

Responding to low-memory notifications

didReceiveMemoryWarning

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 viewDidUnload method. (The view objects themselves should always be released in the viewDidUnload method.)

Releasing critical data structures required by your view controller

dealloc

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.