Re: CoreData huge memory usage - is this right ?
Re: CoreData huge memory usage - is this right ?
- Subject: Re: CoreData huge memory usage - is this right ?
- From: Martin Linklater <email@hidden>
- Date: Fri, 14 Dec 2007 07:19:47 -0800
> Core Data or not, creating many objects in a tight loop (regardless
>of saving a file in the middle of it) never gives autoreleased objects
>a chance to be released. Since the
>-insertNewObjectForEntityForName:inManagedObjectContext: method
>returns an autoreleased object, the thousands of objects you're
>creating aren't going away until (likely) the end of your import.
>
> As suggested above, create an autorelease pool prior to entering
>your loop. Every thousand, you don't need to save, but you do need to
>-drain the pool. This will effectively cap the memory usage in the
>scenario you described.
This doesn't seem to work... The memory usage isn't affected at all. My code now looks like this:
import()
{
NSAutoreleasePool* subPool = [[NSAutoreleasePool alloc] init];
while( not end of file )
{
id coreDataElement = [NSEntityDescription insertNewObjectForEntityForName:name in ManagedObjectContext:moc];
// add my attributes for the element
[coreDataElement setValue:value forKey:key];
[coreDataElement setValue:value forKey:key];
[coreDataElement setValue:value forKey:key];
etc.
[coreDataElement release];
// then every 1000 entries or so I am saving the context
{
[moc save:&error];
[subPool drain];
}
}
[subPool drain];
return;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden