RE: Memory management in a thread
RE: Memory management in a thread
- Subject: RE: Memory management in a thread
- From: Randall Meadows <email@hidden>
- Date: Fri, 12 Mar 2004 15:29:02 -0500
At 3:02 PM -0500 3/12/04, Jonathan E. Jackel wrote:
First, there's no rule that says you have to use autoreleased objects. You
can retain objects and release them immediately when you are done.
Let's say I receive a reply to an AppleEvent; I create an NSString
from the reply text using [NSString stringWithCString:]; I now have
an autoreleased NSString that contains tab-delimited text; if I call
[data componentsSeparatedByString:@"\t"] to get an NSArray *, that
array is autoreleased as well, right? I don't own either the string
nor the array, so I can't release them. This is how I'm getting most
of the references I'm using. Isn't this "forcing" me to use
auto-released objects?
Second, there's no rule that says you can only use one autorelease pool.
You can create additional pools inside the loops and release them at the end
of the loops.
OK.
Third, a lot of those "autoreleased" objects in your loop look like they are
going to be references to objects in your data model. To the extent those
data model objects survive, they will persist in memory, even after the
autorelease pool is released. Releasing the pools more often won't reduce
memory usage if this is the case.
Actually, they're not part of my data model, really. I'm acquiring
textual data from outside sources--file system, Apple Events--and
creating Cocoa objects for immediate and short-term use. I create
it, use it immediately, then don't need it anymore. Actually, every
object that I create at the top of the loop, I'm done with by the
time I reach the end of that loop iteration.
Fourth, until you've profiled your code, you don't know whether this is
really a problem. Virtual memory makes it very difficult to run out of
memory. The practical issue is whether there's a noticeable performance
hit.
Indeed, but I didn't want to get too close to the Point Of No Return
before I understood what I might find there...I think the nested
autorelease pools will satisfy my concerns.
Thanks!
randy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.