Runloopautorelease and runloopautorelease

Source: Internet
Author: User

Runloopautorelease and runloopautorelease

Is there any problem with the code below for such an ios interview question? If yes? How can this problem be solved?

for (int i = 0 ; i < largeNumber; i++) {NSString *str = [NSString stringWithFormat:@"hello -%04d",i];str = [str stringByAppendingString:@" - world"]; }
Concept of local release pool and RunLoop release pool:

The RunLoop of the main thread is enabled by default (the view stops it with [[nsunloop currentRunLoop] runUntilDate: [NSDate date], which is also impossible ), the automatic release pool is created at the beginning of each message loop. The automatic release pool is released before the cycle ends, and RunLoop waits for the next event source. In this process, the release pool created by RunLoop is similar to a global release pool. However, developers can create a release pool, that is, a local release pool, where the release pool is similar to a code block that is automatically released when the release pool ends. Therefore, the partial automatic release pool is released soon, And the RunLoop release pool is released when a message loop ends.

What types of objects will be handed over to the release pool for management:

Returns the objects created by the class method of the current class, all of which are autorelisted and are handed over to the release pool for management. For example, to create a Person class, the management of objects created using the [self alloc] init] method will not be handed over to the release pool where it is located, but will control the release time based on the reference count, if you use [[[self alloc] init] autorelease] to create an object, it is handed over to the release pool for management and control the release time.

-(Void) test {@ autoreleasepool {Person * p = [[Person alloc] init]; p = nil; NSLog (@"---");} NSLog (@ "autorelease ended ");}

Execution result:

Person --- dealloc --- autorelease ends

 

-(Void) test1 {@ autoreleasepool {Person * p = [Person person Person]; // [[self alloc] init] autorelease] p = nil; NSLog (@ "---");} NSLog (@ "autorelease ended ");}

The execution result is:

--- Person --- deallocautorelease ends

Therefore, when the automatic release pool is destroyed or used up, a release message is sent to all objects created using autorelease in the pool to release all autorelease objects instead of all objects.

Back to the interview question:

When we use the for loop to create many NSString objects created using the autorelease method, all the object release permissions are handed over to the RunLoop release pool, the RunLoop release pool will be released only after the event is processed. Therefore, the object cannot be released in time, and memory leakage occurs due to memory accumulation. You can observe the memory surge in Debug Navigation. To verify the memory leakage caused by the creation of autorelease, I did the following test:

Int largeNumber = 100000000;-(void) test3 {for (int I = 0; I <largeNumber; I ++) {NSString * str = [[NSString alloc] initWithFormat: @ "hello-% 04d", I]; str = [str stringByAppendingString: @ "-world"] ;}// the result is that the memory is almost unchanged, verify that this is the reason.

However, when writing code, we are still used to the quick creation method of classes, rather than alloc + init. Therefore, the solution is to add a local release pool and release the memory when it is released. if you add a local release pool outside the loop:

- (void)test4{    @autoreleasepool {        for (int i = 0 ; i < largeNumber; i++) {            NSString *str = [[NSString alloc]initWithFormat:@"hello -%04d",i];            str = [str stringByAppendingString:@" - world"];        }    }}

This obviously has no effect. The release pool needs to be released after loop execution, which is no different from the release pool created using RunKLoop. A better solution is to add a release pool for each loop:

- (void)test5{    for (int i = 0 ; i < largeNumber; i++) {        @autoreleasepool {            NSString *str = [[NSString alloc]initWithFormat:@"hello -%04d",i];            str = [str stringByAppendingString:@" - world"];        }    }}

In this way, the memory is released at the end of each loop. Therefore, when all the loops are completed, the memory is almost no longer consumed.

Summary:

During multi-threaded development, you need to manually add an automatic release pool to the thread scheduling method. Especially when a loop is executed, if there is an object created by using the class quick creation method in the loop, you must place the cyclic body in the Auto Release pool.

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.