4 Memory Management-autoreleasepool

Source: Internet
Author: User

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

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.