Memory Management Mechanism and ios Memory Management Mechanism

Source: Internet
Author: User

Memory Management Mechanism and ios Memory Management Mechanism

Objective-C provides two memory management mechanisms: MRC (MannulReference Counting) and ARC (Automatic Reference Counting), which provide manual and Automatic memory management to meet different needs.

 

ARC:

 

ARC is short for Auto Reference Counting, that is, automatic Reference Counting. The Compiler automatically adds the retain/Release/Autorelease/dealloc Method to the proper position of the Code for memory management.

 

Several key points of ARC:

 

  • When an object is created, retain count + 1 and retain count-1 when the object is release. When retain count is 0, the object is destroyed.
  • The autoreleasepool method is automatically added to the object in the program. If the reference count of the object is 0, the autorelease method is destroyed.

 

So what problems did ARC solve? This is traced back to the MRC manual memory management era.

 

Disadvantages of Memory Management in MRC:

 

  • When we want to release a heap memory, we must first determine that all pointers to the heap space are release. (Avoid releasing in advance)
  • To release the heap space pointed to by pointers, you must first determine which pointers point to the same heap. These pointers can only be released once. (In MRC, it refers to who created and who released it to avoid repeated release)
  • During modular operations, the object may be created and used by multiple modules, and it cannot be determined who will release the object.
  • When performing multi-threaded operations, you are not sure which thread is used up.

 

 

 

 

Memory management-related identifiers in ARC can be divided into variable Identifiers (_ strong, _ weak, _ unsafe_unretained, autoreleasing) and attribute Identifiers (nonatomic/atomic, assign/retain/strong/weak/unsafe_unretained/copy, readonly/readwrite), the default variable is _ strong, and the default attribute is unsafe_unretained. Autoreleasepool also exists.

 

Here, assign/retain/copy has the same meaning as the property identifier in MRC, strong is similar to retain, assign is similar to unsafe_unretained, strong/weak/unsafe_unretained is the same as the variable identifier in ARC, it is just an identifier used for attributes, and a variable identifier (with two dashes __). The other identifiers listed are of the same meaning as those listed in MRC.

 

(1) For assign, you can use this attribute for scalar type (such as int. You can imagine a float. It is not an object, so it cannot be retain or copy.

 

(2) For copy, specify that the object's copy (deep copy) should be used, and the previous value will send a release message. Basically like retain, but does not increase the reference count, it is to allocate a new memory to place it. This is especially applicable to NSString. If you do not want to change the existing one, use this because NSMutableString is also an NSString.

 

 

 

MRC:

In MRC memory management mode, the methods related to variable management include retain, release, and autorelease. The retain and release methods operate on the reference count. When the reference count is zero, the memory is automatically released. Additionally, you can use the NSAID utoreleasepool object to manage the variables that are added to the autorelease pool, and reclaim the memory when drain is enabled.

 

The difference between Strong and Weak:

Strong references hold objects, and weak references do not hold objects.

Strong references can release objects, but weak references cannot, because weak references do not hold objects. When a weak reference points to an object held by a strong reference, when the strong reference releases the object, weak references are automatically assigned nil, that is, weak references automatically point to nil.

Strong reference. For example:

1 id _ strong test0 = [[NSObject alloc] init];/* set to object A */2 3 id _ strong test1 = [[NSObject alloc] init]; /* set to object B */

Both test0 and test1 are strongly referenced. test0 is the holder of object A, that is, it owns A. test1 is the holder of object B, that is, it owns object B. if:

Test1 = test0;/* The holder of object A becomes test1 */

In this way, object B has no owner, and objects without the owner will be recycled by ARC, that is, released:

Test1 holds object A, and test0 also holds object.

 

Weak references are mainly used to prevent memory leakage in circular references. It is mainly used to prevent weak references. weak references only point to this object if they do not hold objects. For example:

1 id _ strong test0 = [[NSObject alloc] init];/* set to object A */2 3 id _ strong test1 = [[NSObject alloc] init]; /* set to object B */4 5 id _ weak test2 = test0;/* test1 holds weak references of object */

Test0 holds the strong reference of object A, while test2 holds the weak reference of object A. That is to say, test0 still holds the weak reference of object A of test0, no object A is held. When test2 leaves the scope, the reference to object A will be lost. When object A is released, test2 will be set to nil, crash does not appear. If:

Test1 = test0;/* test1 strongly references object */

In this case, object B will be released because there is no owner.

 

Another example is:

1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 id _ weak obj0 = nil; 6 if (YES) {7 id obj1 = [[NSObject alloc] init]; // The default value is strong reference, that is, strong type 8 obj0 = obj1; 9 NSLog (@ "obj0: % @", obj0); 10} 11 NSLog (@ "obj0: % @", obj0); 12} 13 return 0; 14} 15 16/* 17 * output result 18 * obj0: <NSObject: 0x1003066c0> 19 * obj0: (null) 20*21 * because the default value generated by obj1 is strongly referenced (_ strong ), After the scope, the object held by obj1 is released, and 22 * obj0 is a weak reference. Therefore, obj0 does not hold the object. After the obj1 object is released, obj0 is automatically assigned to nil23 *. The weak reference feature is that it does not hold objects, even if it is written as id _ weak obj1 = [[NSObject alloc] init]; 24 * This code system will give a warning, because here obj1 is declared as a weak reference, then after the value is assigned, the alloc objects will be released immediately. 25 */

 

 

 

 

 

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.