1. MRC and Arc
Apple provides two memory management mechanisms: One is MRC (manual reference count), manual reference calculation, and arc (auto reference count), which is the automatic reference count. Manual reference Meter, as the name implies, requires the programmer to actively invoke retain, release and other methods to manage memory, and automatic reference counting method, is the compiler in the compilation phase of the memory management code is automatically inserted in the need to manually call the place. This frees the programmer from the complexity of managing memory and puts more effort into implementing specific business logic. It is important to note that arc is available in xcode4.2 or later versions, LLVM compiler version 3.0.
2. How to implement the reference calculation
One is the implementation of the GNUstep: When the object is generated, add the retained property to the object's head, represent the reference count of the object, retain the method call when the retained plus 1,release method is called, retained minus 1, When the retained count is 0 o'clock, the object is disposed.
One is the way Apple is implemented: the reference count table, the approximate form of the table {object A's reference count: The memory address of object A, the reference count of object B: the memory address of object B, ..., the reference count of object N: The memory address of object n}.
3. How to think about memory management
A, self-generated objects, own hold
ID obj = [[NSObject alloc] init]; At this point, the object is generated and obj holds the object. This series of methods includes: Alloc, new, copy, and Mutablecopy
b, not their own generated objects, they can also hold
The id obj = [nsmutablearrray array];//object is generated, and obj points to the object, but obj does not hold the object and needs to call [obj retain] to actually hold
C. release when you no longer need the objects you hold
D. Objects not owned by themselves cannot be released
Novice may be difficult to notice and understand the second rule, how it is done, the implementation is probably as follows:
-(ID) object{ //object is generated, and obj holds the object id obj = [[NSObject alloc] init]; The acquisition object exists, but does not own the object [obj autorelease]; return obj;}
4, about Autorelease
The autorelease uses objects that are automatically and correctly freed when they are outside the established survival range, similar to the automatic variables in the C language. The typical usage of autorelease is as follows:
NSAutoreleasePool *pool = [[NSAutoreleasePool] alloc init]; ID obj = [[NSObject alloc] init]; [obj autorelease]; [Pool drain]; When the pool object is deprecated, the [obj release] method is automatically called
PS: Most of the ideas in this article are from the book "Objective-c Advanced Programming", the right to read the summary of the individual.
iOS development--Ramble on memory management (i.)