The button created by the class method provided by Apple is received by weak but not destroyed. Why? Because the bottom layer of the apple framework is written in MRC, buttoWithType: The returned objects containautorelease
It means that the destruction will be delayed, but it will only be added to the automatic release pool, waiting for the scope of the automatic release pool or the automatic release pool to be destroyed. Instead of destroying them immediately. A classic interview questionQ: Is there any problem with the following code? If so, how can this problem be solved?
for (long i = 0; i < largeNumber; ++i) { @autoreleasepool { NSString *str = [NSString stringWithFormat:@"hello - %ld", i]; str = [str uppercaseString]; str = [str stringByAppendingString:@" - world"]; } }
Answer: If largeNumber is very large, too many automatically released objects will be created, and the automatic release pool may be "full". Tip: there is often one user interaction, far more than one, there will be a lot of code before and after the for, but this for will occupy a lot of space to automatically release the pool
SolutionThe automatic release pool is introduced. There are two solutions on the network!
1> automatically release the external pool (FAST): After the for loop ends, all automatically released objects generated internally will be destroyed after the for loop ends, will release the memory 2> Add the automatic release pool (slow): can be released every time the for generated automatic release object! Question: That method is highly efficient! Speed! Most tests are more internal than external ones! If you ask during the interview: I want to talk about it a little faster, "test it "!!! Tip: in daily work! Sometimes, the Code memory will soar! You need to know the solution!
Set the value of largeNumner:
long largeNumber = 5 * 1000 * 1000;
Test Demo:
NSLog (@ "Internal start"); CFAbsoluteTime start = CFAbsoluteTimeGetCurrent (); for (long I = 0; I <largeNumber; ++ I) {@ autoreleasepool {NSString * str = [NSString stringWithFormat: @ "hello-% ld", I]; str = [str uppercaseString]; str = [str stringByAppendingString: @ "-world"] ;}} NSLog (@ "end-% f", CFAbsoluteTimeGetCurrent ()-start); [self answer1];
Answer1 method:
NSLog (@ "start outside"); CFAbsoluteTime start = CFAbsoluteTimeGetCurrent (); @ autoreleasepool {for (long I = 0; I <largeNumber; ++ I) {NSString * str = [NSString stringWithFormat: @ "hello-% ld", I]; str = [str uppercaseString]; str = [str stringByAppendingString: @ "-world"] ;}} NSLog (@ "end-% f", CFAbsoluteTimeGetCurrent ()-start );
Test results:
11:18:50. 663 12-automatically release the pool of questions [28015: 743360] internal start2015-03-25 11:19:03. 996 12-questions about automatic release pool [28015: 743360] end-13.3323712015-03-25 11:19:03. 996 12-automatically release the pool of questions [28015: 743360] Outside start2015-03-25 11:19:18. 227 12-questions about automatic release pool [28015: 743360] end-14.230752
It can be seen from the test results that for cycles, autorelease is added internally and externally. The speed is almost the same, and the internal speed is slightly faster, but it depends on the compiler. However, adding autorelease externally occupies a large amount of memory (note that, unlike not using autorelease, the memory opened without autorelease will not be released ). In development, it is usually added internally.