Automatic release pool is a memory recovery mechanism in OC, you can generally add some temporary variables to the auto-release pool, unified reclaim release, when the automatic free pool destruction, all objects inside the pool will be called once release, that is, the counter will be reduced by 1, but the automatic free pool is destroyed, The objects inside are not necessarily destroyed.
The OC object sends a AUTORELEASE message and adds the object to the nearest auto-release pool, which is the top-of-stack release pool. Autorelease actually put a call to release delayed , for each autorelease, the system just put the object into the current autorelease poo ( stack top release pool ) L, When the pool is released, all objects in the pool are called release.
1. Under Arc, you cannot use the [[NSAutoreleasePool alloc] init] (Can be used before 5.0), but should use @autoreleasepool
2. Do not put a large number of loops in the Autoreleasepool, which will cause a spike in memory, because the objects created inside the release pool to be destroyed before release, this situation should be manually managed memory.
3. Try to avoid large memory use this method, for this delay release mechanism, try to use less
objects created and returned in 4.SDK using static methods are already autorelease and do not require our own manual release.
----------------------------------------------------
Autorelease the Object counter,just throw them into a pool.
When destroy the pool?
}
-------------------------------------------
The writing
1
Main ()
{
@autorelease {
Student *stu=[[student Alloc]init];
[Stu Autorelease];
Student *stu1=[[student Alloc]init];
[Stu1=autorelease];
}
return 0;
}
2//most Common used
Student *stu=[[[student Alloc]init]autorelease];
Student *stu1=[[[student Alloc]init]autorelease];
-------------------------------------------
Create Autoreleasepool
1. IOS 5.0 After
@ autoreleasepool{
code here
}
2.iOS 5.0 Before
NSAutoreleasePool *pool=[[nsautoreleasepool Alloc]init];
code here
[Pool release];
or [pool drain]; Only has difference in MAC development between release and//drain.
-------------------------------------------------
Another clever use of autorelease
Create a static method to speedly create a object
Student.h (Method name is the same as class name by default)
+ (ID) student;
+ (Student) Student;
Student.m
+ (ID)student{
Student *stu=[[[student Alloc]init]autorelease];
return Stu;
}
Main () {
autoreleasepool{
Student *stu=[student Student]
}
}
Latent rules
IOS Static Method/object actually don ' t need developers to manage memory
They do autorelease inside themselves
-----------------------------------
4 Memory Management-autoreleasepool