The iOS thing about memory management

Source: Internet
Author: User
Tags modifier modifiers
iOS Memory Management

iOS memory management on the whole, four points:
① own generation of objects, holding themselves;
② is not a generation of their own objects, they can hold;
③ no longer needs to be released when the object is held by itself;
④ objects that are not held by themselves cannot be freed. memory management under the MRC

when administering MRC memory, we need to be aware of a few things:
① own generation of objects, own hold, such as new/alloc/copy/mutablecopy creation;
② not own generation of objects, they can hold, such as retain hold;
③ is no longer required to release the object it holds, releasing it with release;
④ objects that are not held by themselves cannot be released, such as when an object release has been released, and again releasing can cause a crash. memory management under ARC

in the case of Arc memory management, we need to note a few things:
Circular reference problem in ①block;
②corefoundation and foundation frame bridging conversion;
③ manually opened up the memory needs to release itself;
④ in the class, the KVO and notification are placed nil in the Dealloc method.
⑤ cannot display calls retain, release, Dealloc methods
⑥ use @autoreleasepool blocks instead of NSAutoreleasePool objects when using an automatic free pool.
⑦ because memory management in arc is done by the compiler, and there is no method in the C language to manage the lifecycle of the members of the struct body, the variables in OC cannot be members of the C language structure.
⑧ the conversion id and void * that can be displayed in MRC, but the bridging conversion is required through __bridge in arc. look at the title modifier

the ownership modifiers in memory management are grouped into four categories:
__strong, __weak, __autorelease, __unsafe_unretained. The first three ownership modifiers can be guaranteed to be initialized to nil, and the last ownership modifier cannot be automatically set to nil.

The ①__strong modifier is the default modifier for the ID type and object type, and the introduction of the __strong modifier solves the need for manual release in MRC to solve memory management problems.

The ②__weak modifier is used to break the circular reference, resulting in two types of circular references, one being a strong reference to each other, and one being the object's strong references to itself.

③__weak is used to break circular references after iOS5, IOS5 uses __unsafe_unretained to break circular references, and when __unsafe_unretained is used in arc, the modifiers decorate objects whose memory does not belong to the compiler's administrative scope.

④__unsafe_unretained when you decorate an object, you must ensure that the object is not discarded, or it may cause a crash. __strong ownership modifiers and __weak ownership modifiers

__strong title modifier:
① through Alloc initialization, pseudo code implementation
ID obj = objc_msgsend (obj, @selector (alloc));
Obj_msgsend (obj, @selector (init));
Obj_release (obj);
② by class method initialization, pseudo code implementation
ID obj = objc_msgsend (Nsmutablearray, @selector (array));
Objc_retainautoreleasedreturnvalue (obj);
Objc_release (obj);

__weak title modifier:
1th: An object that is caused by a variable with a __weak modifier is discarded, and the nil is assigned to the variable.
ID __weak obj1 = obj;
code above, using Pseudocode to implement
ID obj1;
Objc_initweak (&obj1,obj);
Objc_destoryweak (&OBJ1); The
defines the function Objc_storeweak, takes the address of the assignment object of the second parameter as the key value, registers the address of the variable with the __weak modifier of the first argument in the weak table, and deletes the address of the variable from the weak table if the second argument is 0.
The above pseudocode can also be implemented through the following pseudocode
ID obj1 = 1;
Objc_storeweak (&obj1,obj);
Objc_storeweak (&obj1,0); The
Weak table is the same as the reference count table and is implemented as a hash list. If the weak table is used, the address of the discarded object is retrieved as the key value, and the corresponding variable address with the __weak modifier can be obtained at high speed. In addition, because an object can be assigned to multiple variables with the __weak modifier at the same time, a key value can register the address of multiple variables. When the
frees weak-decorated objects, the program's execution steps are divided into four steps:
① a record of the address of the discarded object as a key value from the weak table;
② the address of all the __weak modifier variables that will be included in the record, assigned to nil;
③ deletes the record from the weak table, and
④ deletes the record of the discarded object's address as the key value from the reference count table.

2nd: The variable with the __weak modifier, that is, the object that is registered with the Autoreleasepool
ID __weak obj1 = obj;
NSLog (@ "%@", obj1);
The code above is implemented by pseudo code
Declares two functions that objc_loadweakretained the object referenced by the __weak modifier variable and retain,objc_autorelease the function to register the object in the automatic free pool.
ID obj1;
Objc_initweak (&obj1,obj);
ID tmp = Objc_loadweakretaind (&OBJ1);
Objc_autorelease (TMP);
NSLog (@ "%@", TMP);
Objc_destoryweak (&OBJ1); ownership modifiers in property modifiers

Property modifiers, the ownership modifiers for the Assgin and unsafe_unretained are __unsafe_unretained, strong, and retain, and the copy's ownership modifier is the title modifier for __strong,weak, which is __ Weak

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.