Correct Processing of memory warning

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.