OBJECTIVE-C (8) automatic release pool of memory management

Source: Internet
Author: User

    • Auto Free Pool

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

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.