The developer documentation says:
If your application or thread is long-lived and potentially generates many auto-implemented objects, you should periodically merge and create autofill pools (for example, the Application Kit does in the main thread); otherwise, auto-realized objects accumulate, and the amount of memory increases. If, however, your dedicated thread does not cause Cocoa calls, you do not need to create an autorun pool.
I was wondering what the best way to do this. I have several methods that I think will work, but I don’t know what is the “best”. Currently, I have a method that starts a thread and makes it wait for operations to complete:
- (void)startThread { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; accessoryRunLoop = [NSRunLoop currentRunLoop]; //Add a dummy port to avoid exiting the thread due to no ports being found [accessoryRunLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; while(accessoryThreadIsRunning) { //Keep the thread running until accessoryTheadIsRunning == NO [accessoryRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } [pool release]; }
, , :
1) (accessoryThreadIsRunning), 50 100 .
2) , ( performSelector: onThread:), .
3) , , .
, 1 , , , . !
/ .
, .
- . run-loop, ; .
, , runloop. ?
while(...) { ... create pool ... ... do stuff ... ... drain pool ... }
. , .
, . , , -, - , , , . ( , iOS, , , , , , Nib - , ... ).
"" , . . , , - L1 L2. , 100 . , , (, , ), .
, 50-100 .
, , , X , .
, . , , . , , . , .
The usual way is yes, to keep the counter and drain every 50 or so times, but as bbum himself said, just start by draining the pool in each cycle and go from there. OR you could have the -initobjects you need, and not create any objects with auto-implementation. (just stick with factory methods). Remember -releaseall your objects.
-init
-release