is a semi-automatic memory management method
Autorealease Method:
-(Instancetype) autorelease
This method places the object in the auto-free pool, and all objects in the pool are destroyed when the pool is automatically freed.
Common ways to use:
Person *p = [[[[Perosn alloc] init] autorelease];
Using the @autoreleasepool keyword to use the auto-free pool
After {...} Equivalent to the lifetime of the auto-release pool, such as:
@autoreleasepool {Person *p = [[[Perosn alloc] init] autorelease]; ...}
Benefits: Do not care about the time the object is released, do not care when to call release
Attention:
1) objects that occupy large memory do not use autorelease as much as possible
2) @autoreleasepool can be nested
There is a stack structure in the system that automatically frees the pool, and the Autorelease method is to put the object into the top pool of the stack.
3) do not call autorelease multiple times, such as:
[[[Person alloc] init] autorelease] autorelease];
4) When the auto-release pool is destroyed, the objects in the pool are released once, meaning that the Autorelease method does not change the reference count.
It is a good practice to encapsulate the Autorelease method in a Class object method
+ (amperson*) person {//return [[[Person alloc] init] autorelease]; return [[[Self alloc] init] autorelease];}
Use of an older version of the automatic release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; ... [Pool release];
The basic use of autorelease since XCODE5
This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745625
OBJECTIVE-C (8) automatic release pool of memory management