Memory Management differences between different versions of IOS and compatibility between iso6 and memory management of previous versions

Source: Internet
Author: User

The official documents are described in more detail:

Https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html

Simulated memory warning:

There are three methods to implement memory warnings.

 

1. simulator menu: hardware-Simulate Memory warning

 

2. To implement it using a program method, you only need the following code:

  1. Cfnotificationcenterpostnotification (cfnotificationcentergetdarwinnotifycenter (), (cfstringref) @ "uisimulatedmemorywarningnotification", null, null, true );

 

 

3. This is a private API method:

  1. Sel memorywarningsel = @ selector (_ performmemorywarning );
  2. If ([[uiapplication sharedapplication] respondstoselector: memorywarningsel]) {
  3. [[Uiapplication sharedapplication] performselector: memorywarningsel];
  4. } Else {
  5. Nslog (@ "% @", @ "whoops uiapplication no Loger responds to-_ javasmmemorywarning ");
  6. }

 

Memory warning processing:

Since the iPhone 4 supports multiple tasks, we need to carefully handle insufficient memory. If the user runs n software in the background while running our program, the iPhone program running on the foreground will easily receive a warning of insufficient memory.

In general, IOS will give users a chance to process memory resources when the memory is insufficient. When our program receives a warning of insufficient memory for the first time, it should release unnecessary resources to save some memory. Otherwise, when the memory shortage still exists and iOS sends a warning of memory insufficiency to our program again, our program will be killed by IOS.
The IOS uiviewcontroller class provides an interface to handle insufficient memory. Before IOS 3.0, when the system memory is insufficient, the didreceivememorywarining method of uiviewcontroller will be called. We can release some resources temporarily unused in the didreceivemorywarining method.

The viewdidunload method has been added to uiviewcontroller since ios3.0. This method is paired with viewdidload. When the system memory is insufficient, the didreceivemorywarining method of uiviewcontroller is called first, while didreceivemorywarining checks whether the view of the current viewcontroller is displayed on the window. If the view is not displayed on the window, didreceivemorywarining
The viewcontroller view and all its sub-views are automatically destroyed, and the viewdidunload method of viewcontroller is called. If the current view of uiviewcontroller is displayed on the window, the view of the viewcontroller will not be destroyed. Of course, viewdidunload will not be called.

However, after ios6.0, The ios6.0 memory warning viewdidunload is blocked, Which is back to the memory management mode of ios3.0.

Previous iOS3-iOS6.0 versions receive memory Warnings:
Calling didreceivememorywarning to call didreceivememorywarning of super will release the Controller view. Therefore, we cannot release the Controller view again.
Solution:
-(Void) didreceivememorywarning
{
[Super didreceivememorywarning]; // if it is not displayed on the window, the self. View is automatically released.
// Before ios6.0, you do not need to process it here. After self. View is released, the following viewdidunload function will be called to process it in the viewdidunload function.
}
-(Void) viewdidunload
{
// Release any retained subviews of the main view. does not contain self. View
[Super viewdidunload];

// Handle some memory and resource problems.
}

Memory warning for ios6.0 and later versions:
Call didreceivememorywarning to call didreceivememorywarning of super. The call only releases the resouse of the controller and does not release the view.
Solution:
-(Void) didreceivememorywarning
{
[Super didreceivememorywarning]; // The self. view is not automatically released even if it is not displayed on the window.
// Add code to clean up any of your own resources that are no longer necessary.

// The ios6.0 macro switch must be added for compatibility processing to ensure that it is used under 6.0. The following code is blocked before 6.0. Otherwise, viewdidload will be automatically loaded when self. View is used below.
If ([self. View window] = nil) // whether the view is in use
{
// 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 reload and call the viewdidload function when you enter the page again.
}
}

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.