Resolve alerts and processes when IOS memory is low _ios

Source: Internet
Author: User

Memory warning
The available memory for each app under iOS is limited, and if an app uses more memory than this threshold, the system sends memory warning messages to the app. After receiving the message, the app must release as much unnecessary memory as possible or the OS will shut down the app.
Several memory warning levels (easy to understand behavior after memory warnings)
Memory Warning Level:

Copy Code code as follows:

typedef enum {
Osmemorynotificationlevelany =-1,
Osmemorynotificationlevelnormal = 0,
osmemorynotificationlevelwarning = 1,
Osmemorynotificationlevelurgent = 2,
Osmemorynotificationlevelcritical = 3
}osmemorynotificationlevel (abandoned after 5.0)

1, Warning (not-normal)-exit or close some unnecessary background programs e.g. Mail
2, urgent-quit all the background program e.g. Safari and IPod.
3, Critical and beyond-reboot

Response Memory Warning:
Implementing Applicationdidreceivememorywarning in Application delegates: methods:
Receive memory warning message in application delegate object
Implement the Didreceivememorywarning method in your Uiviewcontroller subclass:
Receive memory warning message in view Controller
Register Uiapplicationdidreceivememorywarningnotification Notice:
Use notifications in other classes to receive memory warning messages (for example, to clean up cached data)

View Controller

Build View:
Loadview
1, Loadview every time use Self.view this property, and Self.view is called to nil, to produce an effective self.view (manual maintenance views, you must override this method)
2. When the view controller receives the didreceivememorywarning message, the default implementation is to check whether the current controller's view is in use. If its view is not in the view hierarchy currently in use, and your controller implements the Loadview method, the view will be release and the Loadview method will be redeployed to create a new view. (Note: ios6.0 below if Loadview is not implemented, the memory warning does not invoke Viewdidunload)
Viewdidload
In general we do the initialization of the interface here, such as adding some child views to the view, loading the model data from the database or the network to the child view
The flow chart of the generated view is provided by the official website:

The flowchart of the uninstall view provided by the official website:

On IOS 5 and earlier
1 system warns or Viewcontroller itself causes didreceivememorywarning to be invoked
2 release view after calling Viewwillunload
3 Call Viewdidunload
ios5.0 Leaksdemo

Copy Code code as follows:

-(void) didreceivememorywarning
{
In earlier versions of IOS, the system automatically attempts to unload a view controller's views when memory are low
[Super didreceivememorywarning];

Didreceivememorywarining determines whether the current Viewcontroller view is displayed on the window, or if it does not appear on the window, didreceivememorywarining Automatically destroys the Viewcontroller view and all of its child view, and then calls the Viewcontroller Viewdidunload method.


}
-(void) viewdidunload
{
The object to be set must be the object that can be recreated in the Viewdidload
For example:
Self.myoutlet = nil;
Self.tableview = nil;
DataArray = nil;

[Super Viewdidunload];
}



On IOS 6 and later

1 system warns or Viewcontroller itself causes didreceivememorywarning to be invoked
2-(void) Didreceivememorywarning frees resources that are not currently in use
ios6.0 Leaksdemo

Copy Code code as follows:

-(void) didreceivememorywarning
{
[Super didreceivememorywarning];//will not automatically release Self.view even if it is not displayed on the window. Pay attention to the distinction before ios6.0
ADD code to clean up any your own the are no longer necessary.
Here do the compatibility processing need to add ios6.0 macro switch, guaranteed to be used under 6.0, 6.0 before shielding the following code, otherwise it will be used when the Self.view automatically loaded Viewdidunload
if ([[Uidevice currentdevice].systemversion Floatvalue] >= 6.0) {
It is important to note that self.isviewloaded is essential, and other ways of accessing the view will cause it to load, and the WWDC video also ignores this.
if (self.isviewloaded &&!self.view.window)/Is the view being used
{
ADD code to preserve data stored in the "views" that might is
Needed later.
ADD code to clean up I strong references to the view in
The view hierarchy.
Self.view = nil;//purpose is to be able to reload the call viewdidload function once it is entered again.
}
}
}

Handling when there is not enough memory
When we open a lot of applications, and 3d games, the program is out of memory, memory warning will occur, that memory next to what the left?????
The memory warning is about a process issue! Each program is a process, some processes in the background after the program began to hibernate, and some programs into the background has been running the program, such as qq! After the controller receives a memory warning, the following methods are used:

1. The Didreceivememorywarning method is invoked when the controller receives a memory warning
The following steps are implemented by default within the 2.didReceiveMemoryWarning method: First, the controller's view is detected on the screen.

Copy Code code as follows:

if (Self.view.superview = = nil) {//The view of the test controller is not on the screen
Will attempt to destroy the controller's view
When it is about to be destroyed, the controller's viewwillunload is invoked.
When the destruction is complete, the Viewdidunload method of the controller is invoked.
} else {

Do not destroy the controller's view
}


3. When you need to use the controller's view again, the Loadview method is called to create the view

4. Then a series of life cycle methods are called
Viewdidload-> ...

5. Life cycle Cycle
Loadview–> viewdidload–>. Visible.. – Memory warning –> didreceivememorywarning-> viewwillunload–> viewdidunload-again using-> Loadview

So when our program memory is too big, we mount in the backstage QQ sometimes will appear has already launched the situation! When we click again, QQ and reload to run up!

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.