Home>

Memory warning

The memory available to each app in iOS is limited.If the memory used by an app exceeds this threshold,The system will send a memory warning message to the app. After receiving the message,The app must release as much unnecessary memory as possible.Otherwise os will close the app.

Several memory warning levels (to facilitate understanding of behavior after memory warnings)

memory warning level:

typedef enum {
 osmemorynotificationlevelany=-1, osmemorynotificationlevelnormal=0, osmemorynotificationlevelwarning=1, osmemorynotificationlevelurgent=2, osmemorynotificationlevelcritical=3
 } osmemorynotificationlevel;(deprecated since 5.0)

1. warning (not-normal) — quit or close some unnecessary background programs e.g. mail

2.urgent — Quit all background programs e.g. safari and ipod.

3. critical and beyond — restart

Respond to memory warnings:

Implement the applicationdidreceivememorywarning:method in the application delegate:

Receive memory warning message in application delegate object

Implement the didreceivememorywarning method in your uiviewcontroller subclass:

Receive memory warning message in view controller

Sign up for uiapplicationdidreceivememorywarningnotification notification:

Use notifications in other classes to receive memory warning messages (for example:clear cache data)

view controller

Generate view:

loadview

1, loadview is called every time the self.view property is used and self.view is nil,Used to generate a valid self.view (manually maintain views, you must override this method)

2. When the view controller receives the didreceivememorywarning message, The default implementation is to check if the current controller's view is in use. If its view is not in the current view hierarchy and your controller implements the loadview method, then this view will be released and the loadview method will be called again to create a new view. (Note:If loadview is not implemented under ios6.0, viewdidunload will not be called when memory warning)

viewdidload

Generally we will do the initialization operation on the interface here,For example, add some subviews to the view, load model data from the database or the network into the subviews

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

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

on ios 5 and earlier

1 The system issued a warning or the viewcontroller itself called caused didreceivememorywarning to be called

2 release view after calling viewwillunload

3 call viewdidunload

ios5.0 leaksdemo

-(void) didreceivememorywarning
{
 //in earlier versions of ios, the system automatically attempts to unload a view controller "s views when memory is low
 [super didreceivememorywarning];
</p>
<p>
//didreceivememorywarining will determine whether the current viewcontroller's view is displayed on the window. If it is not displayed on the window, didreceivememorywarining will automatically destroy the viewcontroller's view and all its child views
Then call the viewdidunload method of the viewcontroller.
</p>
<p>
}
-(void) viewdidunload
{
 //The released object must be an object that can be recreated in viewdidload
 //for example:
 self.myoutlet=nil;
 self.tableview=nil;
 dataarray=nil;
 [super viewdidunload];
}

on ios 6 and later

1 The system issued a warning or the viewcontroller itself called caused didreceivememorywarning to be called

2-(void) didreceivememorywarning;release resources that are not currently in use

ios6.0 leaksdemo

-(void) didreceivememorywarning
{
 [super didreceivememorywarning];//Even if it is not displayed on the window, it will not automatically release self.view. Note the difference from before ios6.0
 //add code to clean up any of your own resources that are no longer necessary.
 //Compatibility processing here needs to add the ios6.0 macro switch,Guaranteed to be used under 6.0,The following code was blocked before 6.0,Otherwise viewdidunload will be automatically loaded when using self.view below
 if ([[uidevice currentdevice] .systemversion floatvalue]>= 6.0) {
 //Note that self.isviewloaded is essential,Accessing the view in other ways will cause it to load,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 be
 //needed later.
 //add code to clean up other strong references to the view in
 //the view hierarchy.
 self.view=nil;//The purpose is to be able to reload and call the viewdidload function when entering again.
 }
 }
}

What to do when there is insufficient memory

When we open a lot of applications,And 3d games, the program is out of memory,A memory warning occurs,What's left of the memory next?????

Memory warnings are about a process issue! Every program is a process,Some processes start to sleep after the program enters the background.Some programs are still running when they enter the background.For example qq! When the controller receives a memory warning,Will do the following:

1. When the controller receives a memory warning,Will call didreceivememorywarning method

2. The default implementation of didreceivememorywarning method implements the following steps:First, it will detect that the controller's view is not on the screen

if (self.view.superview == nil) {//detect if the controller's view is not on the screen
//will try to destroy the controller's view
//when it is about to be destroyed,Will call the controller's viewwillunload
//When the destruction is complete,Will call the controller's viewdidunload method
} else {
</p>
<p>
//don't destroy the controller's view
}

3. When the controller's view needs to be used again, the loadview method will be called to create the view.

4. Then a series of lifecycle methods will be called

viewdidload —>...

5. Life cycle cycle

loadview->viewdidload->.. visible ..-memory warning->didreceivememorywarning->viewwillunload->viewdidunload-reuse->loadview

So when our program memory is too large,The qq we mounted in the background sometimes appears to have been launched! When we click again,qq reload and run again!

ios
  • Previous GUI Design Basis for Applications in Java Programming
  • Next Summary of JavaScript date formatting methods