iOS system handles memory warnings

Source: Internet
Author: User

The memory available to each app under the iphone is limited, and if an app uses more than 20M of memory, the system sends a warning message to the app. After you receive this message, the app must handle it correctly, or there may be an error or a memory leak.

When the app receives memory warning, it calls: UIApplication::d idreceivememorywarning-uiapplicationdelegate:: Applicationdidreceivememorywarning, and then invokes all current viewcontroller for processing. So the main work to be done is in Viewcontroller.

When our program receives an out-of-memory warning for the first time, it should release some unused resources to save some memory. Otherwise, when the out-of-memory situation persists and iOS sends out an out-of-memory warning to our program, our program will be killed by iOS.

The iOS Uiviewcontroller class gives us an interface to handle out-of-memory interfaces. Before iOS 3.0, when the system was out of memory, Uiviewcontroller's Didreceivememorywarining method would be called, and we could didreceivememorywarining method to release some temporarily unused resources.

Starting with iOS3.0, Uiviewcontroller added the Viewdidunload method. This method is paired with the viewdidload. When the system memory is low, the first Uiviewcontroller didreceivememorywarining method is called, and didreceivememorywarining Determines whether the current Viewcontroller view is displayed on the window, and if it is not displayed on the window, Didreceivememorywarining automatically Viewcontroller View and all of its child view are destroyed, and then call Viewcontroller's Viewdidunload method. If the current Uiviewcontroller view is displayed on a window, the Viewcontroller view is not destroyed, and viewdidunload is not called, of course. However, after the ios6.0, there has been a change, ios6.0 memory warning Viewdidunload is blocked, that is, back to the ios3.0 period of memory management mode.

ios3-ios5.0 previous version received memory warning: Calling Super's didreceivememorywarning within Didreceivememorywarning will release the controller's view. So we can't release the controller's view again. Processing method:

Java code
  1. -(void) didreceivememorywarning
  2. {
  3. [Super didreceivememorywarning];//will automatically release Self.view if it is not displayed on the window.
  4. ios6.0 before, do not do this processing, Self.view release, will call the following Viewdidunload function, in the Viewdidunload function to do the processing.
  5. }
  6. -(void) viewdidunload
  7. {
  8. Release any retained subviews of the main view. does not contain Self.view
  9. Handle some memory and resource issues.
  10. [Super Viewdidunload];
  11. }
-(void) didreceivememorywarning        {                 [Super didreceivememorywarning];//will automatically release Self.view if it is not displayed on the window.                 ///ios6.0 Previously, do not have to do this processing, Self.view release, will call the following Viewdidunload function, in the Viewdidunload function to do the processing.        }        -(void) viewdidunload        {               //Release any retained subviews of the main view. Does not include Self.view               //handling some memory and resource issues.                [Super Viewdidunload];                      }

Memory warning in iOS6.0 and above: Calling Super's didreceivememorywarning call within didreceivememorywarning only releases the controller's Resouse and does not release the view Processing method:-(void) didreceivememorywarning {[Super didreceivememorywarning];//does not appear on the window and will not automatically self.vi             EW release. ADD code to your own resources is no longer necessary.
Here to do compatible processing need to add ios6.0 macro switch, guaranteed to be used under 6.0, 6.0 blocked the following code before, otherwise it will be automatically loaded when using Self.view below Viewdidunload

if ([[Uidevice currentdevice].systemversion Floatvalue] >= 6.0) {

It is important to note that self.isviewloaded is essential, other ways to access 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 of that might be                    //needed later.                            //ADD code to strong references to the view in   &nbs p;               //The View hierarchy .                    Self.view = nil;// The goal is to re-load the call to the Viewdidload function when it is again entered.             }

}     }

But it seems that the writing is not easy compared to the previous. In the end, we found an article in which the article said it was not worth reclaiming this part of the memory for the following reasons:

1. UIView is a subclass of Uiresponder, and Uiresponder has a calayer member variable, Calayer is specifically used to draw yourself onto the screen.

2. Calayer is a wrapper class for bitmap images, and Calayer creates this bitmap image class when UIView calls its own drawrect.

3. The actual memory is actually a bitmap image class, Calayer only accounted for 48bytes, UIView only accounted for 96bytes. and an ipad full-screen UIView of the bitmap class will take up to 12M size!

4. When iOS6, the system automatically recycles the bitmap class when the system issues memorywarning. However, the UIView and Calayer classes are not recycled. This means that most of the memory is reclaimed, and can be rebuilt according to the Calayer class when the bitmap class is required.

So, what iOS6 to do is that we don't need to recycle memory for dozens of byte at all.

--------------------------Cut cake Split Line--------------

Ps:

1. Official document about this: https://developer.apple.com/library/ios/#featuredarticles/viewcontrollerpgforiphoneos/ Viewloadingandunloading/viewloadingandunloading.html

2, zon2012 does not seem to have ios6 this compatibility (in fact, view is no problem, the key is resources)

iOS system handles memory warnings

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.