RE: Memory management in a thread
RE: Memory management in a thread
- Subject: RE: Memory management in a thread
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 12 Mar 2004 15:02:17 -0500
>
In my program I kick off a thread to do some intense processing of a
>
bunch of data. At the top of the thread method, I create an
>
NSAutoreleasePool, which I release at the end of that method (when
>
the thread is effectively done). In between these two events, I
>
create a *bunch* of autoreleased objects, mostly from methods like
>
[NSArray arrayWithObjects], [NSString stringWithFormat], etc., in a
>
nested looping construct which make a bunch of function calls.
>
Something like this pseudo-code:
>
>
doThreadProcess {
>
pool = [[NSAutoreleasePool alloc] init]
>
projects = ProjectList
>
for each project in projects
>
files = ProjectFiles
>
for each file in files
>
revs = FileRevisions
>
for each rev in revs
>
check for user abort
>
ProcessRevision
>
end
>
end
>
end
>
[pool release]
>
}
>
>
NSArray * ProjectFiles {
>
data = ReadProjectData
>
create array
>
for each entry in data
>
string = [NSString stringFromCString:]
>
add string to array
>
end
>
return array
>
}
>
>
etc.
>
>
They get [auto]released when the pool is released, right? If so,
>
that means I'm going to have a *LOT* of unused objects taking up
>
memory in the midst of my loop.
>
>
Or am I confused? I'm guessing I cannot release on an
>
already-autoreleased object, right? What to do to clean up my loop?
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.
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.
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.
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.
Jonathan
_______________________________________________
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.