IOS low-memory processing, compatible with ios5 and ios6

Source: Internet
Author: User

The memory of mobile devices is extremely limited. Applications must handle low-memory to avoid program crashes due to memory usage.

 

How to handle low-memory
An application usually contains multiple view controllers. When you jump from view to another view, the previous view is only invisible and will not be cleared immediately, but saved in memory, so that the next quick appearance can be made. However, if the application receives the low-memory warning from the system, we have to clear the views in the invisible state to free up more memory; currently, the visible View Controller also needs to reasonably release some cached data, image resources and resources that are not in use to avoid application crash.

 

The idea is that the specific implementation varies slightly according to the system version. This article will detail the low-memory processing for iOS 5 and iOS 6.

Handling of iOS 5
Before iOS 6, if the application receives the low-memory warning, the currently invisible view controllers will receive the viewdidunload message (or automatically call the viewdidunload method ), therefore, we need to release all outlets and the resources that can be created again in the viewdidunload method. The currently visible View Controller uses didreceivememorywarning to reasonably release resources. For details, see the code annotations.

For a simple example, there is a view controller:
@ Interface myviewcontroller: uiviewcontroller {

Nsarray * dataarray;
}
@ Property (nonatomic, strong) iboutlet uitableview * tableview;
@ End

The corresponding processing is:
# Pragma mark-
# Pragma mark Memory Management


-(Void) didreceivememorywarning {
// Releases the view if it doesn't have a superview.
[Super didreceivememorywarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}


-(Void) viewdidunload {
// Relinquish ownership of anything that can be recreated in viewdidload or on demand.
// For example: Self. myoutlet = nil;
Self. tableview = nil;
Dataarray = nil;

[Super viewdidunload];
}

Processing of iOS 6
IOS 6 discards the viewdidunload method, which means we need to operate everything on our own in didreceivememorywarning.
What should I do?

1. Set outlets to weak
When view dealloc, no one holds any strong reference pointing to subviews, The subviews instance variable will be automatically empty.

@ Property (nonatomic, weak) iboutlet uitableview * tableview;

 

2. Set the cache data to null in didreceivememorywarning.
# Pragma mark-
# Pragma mark Memory Management


-(Void) didreceivememorywarning
{
[Super didreceivememorywarning];
// Dispose of any resources that can be recreated.
Dataarray = nil;
}

Do not forget that every time tableview reload is performed, You need to judge dataarray. If it is empty, create a new one.

 
Compatible with iOS 5 and iOS 6
Well, the point is, what if I want the program to be compatible with iOS 5 and iOS 6? Here is a tip. We need to do something about didreceivememorywarning:

# Pragma mark-
# Pragma mark Memory Management


-(Void) didreceivememorywarning
{
[Super didreceivememorywarning];

If ([self isviewloaded] & self. View. Window = nil ){
Self. view = nil;
}

Dataarray = nil;
}

Determine whether the view is part of the window. If not, you can safely leave self. View empty in exchange for more available memory.

What will this happen? If you switch from View Controller A to View Controller B and simulate the low-memory warning, View Controller A will execute self. view = nil; when we return A from B, A will call viewdidload again. At this time, all the data is re-created, which is simple and compatible with no pressure ~~

Related Article

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.