@ Autoreleasepool in loop and loop in @ autoreleasepool

Source: Internet
Author: User

If a large number of autorelease objects are generated in a loop, you can use autorelease pool for encapsulation. There are two encapsulation methods:

1:

123456
while ([date next]) {    @autoreleasepool {        NSDictionary *dict = [self dictFromXX];        //...    }}

2:

123456
@autoreleasepool {    while ([date next]) {        NSDictionary *dict = [self dictFromXX];        //...    }}

First, @ autoreleasepool in loop mode generates a pool for each loop, which is dropped by drain after the end of a single loop. This method is suitable for generating a large number of autorelease objects for each loop, after a single cycle ends, resources can be released in a timely manner.

Second, there is only one pool in loop in @ autoreleasepool, and it will only be drain after the end of the entire loop, that is to say, the autorelease object generated during the first loop will not be released with the pool until the end of the entire loop. This method is applicable to scenarios where the number of cycles is low and only a small number of autorelease objects are generated for each loop. After all, these objects will be released after the loop ends.

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.