The memory available for each app in the iPhone is limited. If the memory used by an app exceeds 20 mb, the system will send a memory warning message to the app. After receiving this message, the APP must handle it correctly. Otherwise, an error may occur or memory leakage may occur.
After the app receives memory warning, it will call:
Uiapplication: didreceivememorywarning-> uiapplicationdelegate: applicationdidreceivememorywarning, and then call all current viewcontrollers for processing. Therefore, the main task of processing is in viewcontroller.
We know that when creating viewcontroller, the execution order is loadview-> viewdidload.
When receiving a memory warning, if viewcontroller is not displayed (in the background), didreceivememorywarning-> viewdidunload is executed. If viewcontroller is currently displayed (in the foreground), only didreceivemorywarning is executed.
When this viewcontroller is re-displayed, the viewcontroller that has run viewdidunload (originally in the background) will call loadview-> viewdidload again.
Therefore, pay attention to the following functions:
| Loadview |
Create a view and build the interface; |
| Viewdidload |
Perform initialization. This function is called when viewcontroller is created and restored for the first time. Therefore, you must set the correct status for this function to distinguish between different situations. |
| Didreceivememorywarning |
Release unnecessary memory, such as cache and undisplayed view. |
| Viewdidunload |
The maximum amount of memory that can be released. For example, views should be released. These views can be re-generated after loadview is called. (The member variables should be set to nil after being released ). Whether or not non-interface data is released requires specific analysis. data that can be recovered can be released, and data that cannot be recovered cannot be released. |
In reality, if viewcontroller is an interface generated using XIB, we need to do less, mainly to restore the original interface status in viewdidload.
If the interface is created through programming, more work needs to be done, and all the above four functions need to be correctly processed.