IOS6 Handling 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 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. Ten. [Super Viewdidunload];
  11. 11.
  12. 12.}

Memory warning for iOS6.0 and later:
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 automatically release Self.view even if it is not displayed on the window.
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 "that" might be
Needed later.

ADD code to strong references to the view in
The view hierarchy.
Self.view = nil;//The purpose is to re-load the call 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)

Share to:

xcode4.5 (IOS 6) Development with previous differences | How iOS sends an email

    • 2013-03-03 16:24
    • Browse 4798
    • Comments (1)
    • Category: Mobile development
    • Related recommendations

Comments

1 Floor squealing Day 2013-05-14

The memory of the mobile device terminal is very limited, the application must do the low-memory processing, in order to avoid the program because the memory usage is too big and crashes.

Low-memory Treatment Ideas
Typically an application contains multiple view controllers, and when you jump from view to another view, the previous view is just not visible and will not be cleaned up immediately, but saved in memory for the next quick appearance. However, if the application receives the Low-memory warning from the system, we have to clear out the views of the current invisible state, freeing up more memory for use, and the currently visible view controller should release some cached data reasonably. Picture resources and some resources that are not in use to avoid application crashes.

The idea is that specific implementations vary slightly depending on the system version, and this article will explain in detail the low-memory processing of iOS 5 and iOS 6.

Handling of IOS 5
Prior to iOS 6, if the application received a low-memory warning, the currently invisible view controllers would receive a viewdidunload message (also understood to call the Viewdidunload method automatically), so we need to The Viewdidunload method frees all outlets and resources that can be created again. The currently visible view controller uses didreceivememorywarning to rationally release resources, as described in code comments.

To give 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 has a superview.
[Super didreceivememorywarning];

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

-(void) Viewdidunload {
Relinquish ownership of anything that can is recreated in viewdidload or on demand.
for example:self.myOutlet = nil;
Self.tableview = nil;
DataArray = nil;

[Super Viewdidunload];
}

Handling of IOS 6
IOS 6 Deprecated The Viewdidunload method, which means that everything needs to be done by ourselves in didreceivememorywarning.
What should be done specifically?

1. Place the outlets as weak
When view Dealloc, no one holds a strong reference to Subviews, then the subviews instance variable is automatically empty.
@property (nonatomic, weak) Iboutlet UITableView *tableview;

2. Empty the cache data in didreceivememorywarning
#pragma mark-
#pragma Mark Memory Management


-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
DataArray = nil;
}
Do not forget a little, whenever tableview reload, need to judge DataArray, if empty is re-created.

Compatible with iOS 5 and iOS 6
Well, the point is, what if you want the program to be compatible with iOS 5 and iOS 6? Here's a little 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 if the view is part of a window, and if not, you can rest assured that Self.view is empty in exchange for more available memory.

What would be the phenomenon? If, from view controller a jumps to view controller B and then simulates the low-memory warning, view controller A will execute Self.view = nil; When we return a from B, a will recall the viewdidload once, when the data is all re-created, simple and compatible with no pressure ~ ~

Note:
If you're curious about why Apple abandoned viewdidunload, you can look at Apple's explanation:
Apple deprecated viewdidunload for a good reason. The memory savings from setting a few outlets to nil just weren ' t worth it and added a lot of complexity for little benefi T. For IOS 6+ apps, you can simply forget about view unloading and only implement didreceivememorywarning if the view cont Roller can let go of cached data so can recreate on demand later.

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.