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.