In my Garbage Collected Mac application I'm experiencing significantly high memory usage as a result of a helper function I put together to delete all Core Data entities of a particular type. Here's the function, for reference:
- (void)deleteAllObjectsForEntity:(NSString *)entityDescription {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:self];
[request setEntity:entity];
[request setIncludesPropertyValues:NO];
NSError *error = nil;
NSArray *items = [self executeFetchRequest:request error:&error];
[request release];
// Delete all objects
for (NSManagedObject *managedObject in items)
{
[self deleteObject:managedObject];
}
// Should probably check for errors here
[self save:&error];
[self reset];
// Suggest the garbage collector tries to tie up any loose ends
[[NSGarbageCollector defaultCollector] collectIfNeeded];
}
By using heapshot analysis, I can see that each time I use the above function to remove all instances of my Fil开发者_如何学CeRecord entity, the heap grows by around 50MB. Memory which appears to never be recovered.
By using the -com.apple.CoreData.SQLDebug 1
launch argument I can see that the majority of this memory caused is caused by the (many) SELECT statements fired during my NSFetchRequest. It appears that each time these objects are selected, Core Data is firing the fault (and thus allocating memory). Obviously I don't need these faults fired because I'm subsequently going to delete the object. Here's an example of my console output:
CoreData: annotation: fault fulfilled from database for : 0x2002ed3e0 <x-coredata://17E6216A-C2FA-42A6-B8E4-5209CD1AB2CA/FileRecord/p117418>
CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNORMALIZEDPATH, t0.ZSIZE, t0.ZFILENAME, t0.ZKIND, t0.ZNORMALIZEDFILENAME, t0.ZPATH FROM ZFILERECORD t0 WHERE t0.Z_PK = ?
Whatever I do, I simply cannot get this memory to be relinquished. As you can see, I save and then reset my NSManagedObjectContext. Furthermore, I hint to the Garbage Collector that it should possibly attempt to collect any lingering data.
Unfortunately I'm working with an existing Core Data schema and, as such, cannot setup anything like cascade deletion rules. In any case, what am I doing wrong here?
Perhaps the best way of solving this issue that I came across was to re-engineer my data model to create an intermediary object that contained a to-many relationship to all of the objects I was looking to remove. By setting the intermediary object's delete rule to cascade, it was possible to delete the intermediary object and have it remove the remaining objects behind the scenes.
精彩评论