A basic principle
&NBSP;&NBSP;&NBSP;OBJECTIVE-C's memory management mechanism is different from Java's automatic garbage collection mechanism, which is essentially a manual management method in C, but with some automatic methods. 1, OC uses the reference count (retain count) to manage object memory, for example, if an object is Alloc, the reference count of this object is added to the 1,retain 1, when the object does not need to destroy the object, free memory, the object needs to call the release method, Release causes the reference count to be reduced by 1, and only the reference count disappears, equal to 0, and the object is called Dealloc to actually destroy the object. &NBSP;&NBSP;&NBSP;OC objects cannot be destroyed automatically after use, remember to release the memory . class *obj = [[Class alloc]init];//Object obj reference count plus 1 class *obj2 = obj;//Two objects pointing to the same piece of memory [obj hello]; [obj release];//object is destroyed [obj2 hello];//error, obj2 pointing memory does not exist [obj2 release];//[obj release], OBJ2 is an invalid pointer, no memory, cannot call method . Note: Dealloc is automatically called, Must not be called manually. Pointer assignment does not introduce the Autorelease pool (auto-free object pool) in the reference count plus 1. 2,oc, and can automatically dispose of objects in the case of some rules. Newly generated objects, As long as you call autorelease, you don't need to call Release. autorelease pool manually, and when you create a new project, Xcode automatically creates one, NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; The nsautoreleasepool contains an array (Nsmutablearray) that holds all objects declared as Autorelease, an object declared as AutoreleASE, the system's job is to add this object to the array. nsautoreleasepool itself in the destruction of the time, will iterate over this array, Each member of the release array. If the retain count of the members in the array is 1 then release, retain count is 0, the object is formally destroyed, and if the retain count of the members in the array is greater than 1, then the release Retain count is greater than 0, the object is still not destroyed, memory leaks, so be sure to note the memory management rules (see below) . Note: When do I create an auto-free object pool? 1) in multi-threaded inside to create themselves. 2) It is a good idea to create an auto-free object pool If there are lots of temporary variables inside the code. for (int i=0;i<100;i++) { nsautoreleasepool *pool = [[NSAutoreleasePool alloc] init]; for (int j=0;j<100000;j++) { [nsstring stringwithformat:@ "123455666"];//produces objects that are autorelease . } [pool release]; } II, Rule 1 who created, who released. If you create an object through Alloc, new, or copy, you must call autorelease or call release when you are done with it, not when you create it, and you do not have to release it. 2 objects created in addition to alloc,new or copy are internally declared autorelease. For example, [UIButton buttonwithtype:]. 3 who retain, who release, as long as you call the retain, no matter how this object is generated, you have to call release. Iii. Paradigm 1 Create an object class *obj = [[Class alloc] init]; 2 Create an auto-release object class *obj = [ [[Class alloc] init]autorelease]; 3 pointer is assigned to another pointer class *obj2 = obj1; [ Obj2 Retain]; [obj2 release]; 4 To create and return an object in a function, you need to set this object to Autorelease-(class *) fun{Class *obj = [[[Class Alloc] init] autorelease]; return obj; } 5 calls the Dealloc method of the base class in the Dealloc method of the subclass-(void) dealloc{... [Super Dealloc]; For the memory mechanism in the properties, refer to the property details. Note: Do not rely too much on retaincount, sometimes the internal implementation is not like the surface, the count will not change the way we see, for example
Movieviewcontroller *movie = [[Movieviewcontroller alloc]init];
NSLog (@ "===%d", [Movie.view Retaincount]);//3
NSLog (@ "===%d", [Movie.view Retaincount]);//4
NSLog (@ "===%d", [Movie.view Retaincount]);//5
iOS Interview topics-----How OC Manages memory "Z"