On iOS memory management mechanism

Source: Internet
Author: User

The principle of iOS memory management mechanism is reference count, reference counting is simply to count the ownership of a piece of memory, when the memory is created, its reference count from 0 to 1, indicating that there is an object or pointer to hold this memory, ownership of the memory of this block, If there is another object or pointer pointing at this memory, then in order to represent this later object or the ownership of this memory, the reference count plus 1 becomes 2, then if an object or pointer no longer points to the memory, the reference count minus 1, indicating that the object or pointer no longer owns this memory ownership, When the reference count of a piece of memory becomes 0, it means that no object or pointer holds the memory, and the system immediately frees the memory.

The reference count at development time is also divided into arc (automatic memory Management) and MRC (Manual memory Management). The essence of Arc is the MRC, which is simply a system that helps developers manage created objects or memory spaces, automatically releasing lost memory space at the time and place the system deems appropriate, the same principle. Although the arc is convenient to operate, it not only reduces the amount of code, but also reduces the probability of memory errors, but because the arc is not necessarily released in time, the program can sometimes take up a larger memory footprint. The MRC, if done well, through manual management, the timely release of unnecessary memory space, you can ensure that the program for a long time running in good condition.

Keywords that can cause reference count changes in the MRC are: Alloc,retain,copy,release,autorelease. (The strong keyword is used only for arc, equivalent to retain)

alloc: When a class of object creation, need to open up memory space, will use Alloc,alloc is a class method, can only be called by the class, its role is to open up a new memory space, and make this memory reference count from 0 to 1, note, is the new memory space, Each use of the class Alloc out is a new memory space, and the last alloc out of the memory space is not necessarily associated with, and the last time the memory space alloc out still exists, will not be released.

retain: Retain is an instance method that can only be called by an object, and its purpose is to make the reference count of the object's memory space plus 1, and will not open a new chunk of memory space, usually the assignment is called, such as:

Object 2=[Object 1 retain], which means that object 2 also owns the memory. If you simply assign a value, such as Object 2 = Object 1, then object 2 will become a wild pointer when object 1 's memory space is freed, and then operation on object 2 will cause a memory error.

copy: Copy is also an instance method, can only be called by the object, return a new object, its role is to copy an object to a new memory space, the old memory space reference count does not change, the new memory space reference count from 0 to 1, that is, Although the content is the same, but essentially two pieces of memory, the equivalent of cloning, one becomes two. Where copy is divided into shallow copy, deep copy and True deep copy, shallow copy is only copy address and retain equivalent; Deep copy is copy content, new memory is opened, and retain is different; True deep copy is for container classes, such as array classes, dictionary classes, and collection classes (including mutable and immutable). Suppose there is an array class object, the normal deep copy will open up a new memory to hold the object, but the address of the elements inside the array object is not changed, that is, the array element is only retain or shallow copy, and does not create a new memory space, and the real deep copy, Not only is the array object itself deeply copied, but even the array elements are deeply copied, which opens up new memory space for each array element.

Release: Release is an instance method that can only be called by an object, which is used to reduce the reference count of the object's memory space by 1, and if the reference count becomes 0, the system will immediately release the memory. If you call release on the basis of a reference count of 0, it causes excessive release and crashes the memory;

autorelease: Autorelease is an instance method, which can only be called by an object, it acts on release similar, but not immediately minus 1, equivalent to a delayed release, usually used for the release of the method return value, such as a convenience constructor. Autorelease will be executed when the program comes out of the auto-release pool, and usually the system automatically generates an auto-release pool (even under MRC), and can also set its own auto-release pool, such as:

@autoreleasepool {

obj= [[NSObject alloc]init];

[obj autorelease];

}

When the program goes out of "}", the reference count of obj will be reduced by 1.

In addition to the keywords described above, there are methods that can cause changes in reference counts, such as the addition of parent views in the UI, removal of child views, navigation controllers or view controllers that introduce new view controllers, and return, container classes (arrays, dictionaries, and collections) to add and remove elements.

When the child view is added to the parent view, the reference count for the child view is 1, the reference count minus 1 is removed, and if the parent view reference count becomes 0 memory is freed, all its child views are released once, that is, the reference count is reduced by 1, in principle only the three cases the reference count of the child view will change. Other addition and subtraction, such as the parent view reference count, do not affect the child view.

The container class is similar to a view, adding an element that references the Count plus 1, removing the element, the element reference count minus 1, the container reference count becoming 0 of the memory being freed, the container all elements release, the reference count minus 1, In other cases, the change in the reference count of the container itself does not affect the change in the reference count of the elements within the container.

The introduction of a new view controller by a navigation controller or a view controller adds 1 to the referenced count of the view controller being rolled out, and the reference count is reduced by 1 when it returns, as follows:

The navigation controller launches the View Controller invocation method:-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated;

The same method is called with the navigation controller when returning:-(Uiviewcontroller *) popviewcontrolleranimated: (BOOL) animated;

View Controller rollout View Controller call method:-(void) Presentviewcontroller: (Uiviewcontroller *) viewcontrollertopresent animated: (BOOL) flag Completion: (void (^) (void)) completion

Called by the view controller that was rolled out when returned:-(void) dismissviewcontrolleranimated: (BOOL) flag completion: (void (^) (void)) completion

It should be noted that when the reference count of an object becomes 0 of the memory being freed, the-(void) Dealloc method is called, so if you customize the class under MRC, The property key in the class must be set to retain or copy's property in this method to release once, so as not to cause a memory leak, overriding the method do not forget to add [Super Dealloc] in the first line;.

On iOS memory management mechanism

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.