Talking about iOS memory management mechanism and ios Memory Management

Source: Internet
Author: User

Talking about iOS memory management mechanism and ios Memory Management

The principle of iOS memory management mechanism is reference counting. reference counting is simply to count the ownership of a piece of memory. When this piece of memory is created, its reference counting increases from 0 to 1, indicates that an object or pointer holds the memory and owns the memory. If another object or Pointer Points to the memory, to indicate the ownership of the subsequent object or pointer to the memory, the reference count is increased by 1 to 2. If an object or pointer no longer points to the memory, the reference count is reduced by 1, this indicates that this object or pointer no longer owns this memory. When the reference count of a memory becomes 0, it indicates that no object or pointer holds this memory, the system will immediately release this memory.

The reference count during development is divided into ARC (Automatic Memory Management) and MRC (manual memory management ). The essence of ARC is actually MRC. It is simply that the system helps developers manage the created objects or memory space, and automatically releases the memory space that has been lost at the appropriate time and location that the system deems appropriate, the principle is the same. Although ARC is easy to operate, it not only reduces the amount of code, but also reduces the probability of memory errors, because ARC may not be released in time, so sometimes the program may occupy a large amount of memory. If MRC is well performed, it can be manually managed to release unnecessary memory space in time to ensure that the program runs well for a long time.

Keywords that cause reference count changes in MRC include alloc, retain, copy, release, and autorelease. (The strong keyword is only used for ARC, and its function is equivalent to retain)

Alloc: When a class object is created and memory space needs to be opened up, alloc is used. alloc is a class method and can only be called using classes, it is used to open up a new memory space and increase the reference count of this memory from 0 to 1. Note that it is a new memory space, each time a class alloc is used, it is a new memory space, which is not necessarily related to the memory space generated by the last alloc, and the memory space generated by the last alloc still exists and will not be released.

Retain: retain is an instance method that can only be called by an object. Its function is to increase the reference count of the memory space of this object by 1, and it will not open up a new memory space, generally, a value assignment is called, for example:

Object 2 = [object 1 retain]; indicates that object 2 also owns this memory. If the value is simply assigned, for example, object 2 = Object 1; when the memory space of object 1 is released, object 2 becomes a wild pointer, operations on Object 2 will cause memory errors.

Copy: copy is also an instance method and can only be called by objects. A new object is returned. It is used to copy an object to a new memory space, the reference count of the old memory space does not change, and the reference count of the new memory space increases from 0 to 1. That is to say, although the content is the same, it is essentially two pieces of memory, which is equivalent to cloning, one is changed to two. Among them, copy is divided into the shortest copy, deep copy and real deep copy. The shortest copy address is the same as the retain; the deep copy is the copy content, and new memory will be opened, different from the retain; the real deep copy is for the container class, such as the array class, Dictionary class, and collection class (including variable and immutable). Suppose there is an array class object, ordinary deep copy will open up a new memory to store this object, but the address of each element in this array object has not changed, that is, the array element is only retain or shallow copy, without creating a new memory space, the real deep copy not only copies the array object itself, but also copies the array elements in depth, that is, new memory space is opened for each array element.

Release: release is an instance method and can only be called by objects. It is used to reduce the reference count of the object's memory space by 1, if the reference count is 0, the system immediately releases the memory. If you call release after the reference count is 0, excessive release will occur and the memory will crash;

Autorelease: autorelease is an instance method that can only be called by objects. It acts like release, but not immediately minus 1, which is equivalent to a delayed release. It is usually used to release method return values, for example, constructor. Autorelease is executed when the program goes out of the automatic release pool. Generally, the system automatically generates an automatic release pool (even under MRC). You can also set the automatic release pool by yourself, for example:

@ 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 above keywords, there are also some methods that will cause reference count changes, such as adding or removing sub-views in the parent view in the UI, and introducing new view controllers and responses from the navigation controller or view controller, add and remove elements to and from a container class (array, dictionary, and set.

When a child view is added to the parent view, the reference count of the Child view is increased by 1. When the child view is removed, the reference count is reduced by 1. If the reference count of the parent view is changed to 0, the memory is released, all of its subviews are release once, that is, the reference count is reduced by 1. In principle, the reference count of the subview changes only in these three cases, the addition and subtraction of reference count of the parent view does not affect the child view.

The scenario of the container class is similar to that of the view. When an element is added, the reference count of this element increases by 1, and the reference count of this element decreases by 1, the memory occupied by the container reference count changes to 0 and is released, when all elements of the container are release, the reference count is reduced by 1. In other cases, the reference count changes of the container itself will not affect the reference count changes of elements in the container.

The New View Controller introduced by the navigation controller or View Controller increases the reference count of the introduced View Controller by 1. When the View Controller returns the reference count by 1, the specific method is as follows:

The navigation controller introduces the call method of the View Controller:-(void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated;

Call the method using the navigation controller when returning:-(UIViewController *) popViewControllerAnimated :( BOOL) animated;

View Controller call method:-(void) presentViewController :( UIViewController *) viewControllerToPresent animated: (BOOL) flag completion :( void (^) (void) completion

Method of calling the View Controller that is introduced when the returned result is:-(void) dismissViewControllerAnimated: (BOOL) flag completion: (void (^) (void) completion

Note: When the reference count of an object changes to 0, memory usage is released, the-(void) dealloc method is called. Therefore, if you define a class in MRC, you must set the attribute keywords in this class to retain or copy attribute release once in this method to avoid Memory leakage. Do not forget to add [super dealloc] In the first line of the rewrite method.

 

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.