2011-09-13 35 views
0

任何人都可以解释我如何最大限度地减少内存开销在这个NSManagedObject子类CoreData内存开销

- (void) saveImage:(UIImage*)image 
{ 
    assert(image != nil); 

    if (self.image == nil) 
    { 
     Image* image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" 
                inManagedObjectContext:self.managedObjectContext]; 
     self.image = image; 
    } 

    self.image.imageData = UIImagePNGRepresentation(image); 
    self.image.saved = [NSNumber numberWithBool:NO]; 
    self.hasPicture = [NSNumber numberWithBool:YES]; 

    NSError* error = nil; 
    if (![self.managedObjectContext save:&error]) 
    { 
     MDLogForTag(@"CoreData", @"Saving image failed with error: %@",error); 
     assert(NO); 
    } 
    [self.managedObjectContext refreshObject:self mergeChanges:NO]; 
} 

2小时活跃使用的应用程序后,UIImagePNGRepresentation(image)产生500MB。 这是一个数据对象的生命周期

# Category Event Type RefCt Timestamp Address Size Responsible Library Responsible Caller 
0 NSConcreteMutableData Malloc 1 00:45.345.122 0x109dd6d0 32 Foundation +[NSData(NSData) data] 
1 NSConcreteMutableData Autorelease  00:45.345.126 0x109dd6d0 0 UIKit   UIImagePNGRepresentation 
2 NSConcreteMutableData Retain 2 00:45.345.133 0x109dd6d0 0 ImageIO   CGImageWriteSessionCreateWithMutableData 
3 NSConcreteMutableData Release 1 00:45.366.875 0x109dd6d0 0 UIKit   UIImagePNGRepresentation 
4 NSConcreteMutableData Retain 2 00:45.366.910 0x109dd6d0 0 MyProject -[Feature saveImage:] 
5 NSConcreteMutableData Retain 3 00:45.369.430 0x109dd6d0 0 CoreData -[NSSQLCore _populateRowForOp:withObject:] 
6 NSConcreteMutableData Retain 4 00:45.370.260 0x109dd6d0 0 CoreData -[NSSQLBindVariable setValue:] 
7 NSConcreteMutableData Release 3 00:45.370.648 0x109dd6d0 0 CoreData -[NSSQLBindVariable setValue:] 
8 NSConcreteMutableData Retain 4 00:45.376.359 0x109dd6d0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:] 
9 NSConcreteMutableData Retain 5 00:45.376.668 0x109dd6d0 0 CoreData -[NSSQLRow copy] 
10 NSConcreteMutableData Release 4 00:45.426.906 0x109dd6d0 0 CoreData -[NSSQLOperation dealloc] 
11 NSConcreteMutableData Release 3 00:45.427.006 0x109dd6d0 0 CoreData -[NSKnownKeysDictionary1 dealloc] 
12 NSConcreteMutableData Release 2 00:45.427.131 0x109dd6d0 0 GraphicsServices GSEventRunModal 

有时正确地释放对象与此呼吁

# Category Event Type RefCt Timestamp Address Size Responsible Library Responsible Caller 
0 NSConcreteMutableData Malloc 1 87:35.960.485 0x1088ace0 32 Foundation +[NSData(NSData) data] 
1 NSConcreteMutableData Autorelease  87:35.960.489 0x1088ace0 0 UIKit UIImagePNGRepresentation 
2 NSConcreteMutableData Retain 2 87:35.960.496 0x1088ace0 0 ImageIO CGImageWriteSessionCreateWithMutableData 
3 NSConcreteMutableData Release 1 87:35.971.032 0x1088ace0 0 UIKit UIImagePNGRepresentation 
4 NSConcreteMutableData Retain 2 87:35.971.081 0x1088ace0 0 MyProject -[Feature saveThumbnail:] 
5 NSConcreteMutableData Retain 3 87:35.973.784 0x1088ace0 0 CoreData -[NSSQLCore _populateRowForOp:withObject:] 
6 NSConcreteMutableData Retain 4 87:35.999.687 0x1088ace0 0 CoreData -[NSSQLBindVariable setValue:] 
7 NSConcreteMutableData Release 3 87:36.000.023 0x1088ace0 0 CoreData -[NSSQLBindVariable setValue:] 
8 NSConcreteMutableData Retain 4 87:36.009.349 0x1088ace0 0 CoreData -[NSManagedObject(_NSInternalMethods) _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:] 
9 NSConcreteMutableData Retain 5 87:36.009.758 0x1088ace0 0 CoreData -[NSSQLRow copy] 
10 NSConcreteMutableData Release 4 87:36.146.717 0x1088ace0 0 CoreData -[NSSQLOperation dealloc] 
11 NSConcreteMutableData Release 3 87:36.146.843 0x1088ace0 0 CoreData -[NSKnownKeysDictionary1 dealloc] 
12 NSConcreteMutableData Release 2 87:36.146.983 0x1088ace0 0 GraphicsServices GSEventRunModal 
13 NSConcreteMutableData Release 1 87:39.734.569 0x1088ace0 0 CoreData -[NSManagedObject(_NSInternalMethods) _clearRawPropertiesWithHint:] 
14 NSConcreteMutableData Release 0 87:39.734.577 0x1088ace0 0 CoreData -[NSSQLCore managedObjectContextDidUnregisterObjectsWithIDs:] 
15 NSConcreteMutableData Free 0 87:39.734.582 0x1088ace0 -32 Foundation -[NSConcreteMutableData dealloc] 

回答

1

DALOG,

虽然这将是很好看的属性列表,我相信self.image和self.image.imageData之间有一个保留循环。我想你应该在你的方法的末尾添加一行代码:

[self.managedObjectContext refreshObject:self.image mergeChanges:NO]; 
[self.managedObjectContext refreshObject:self  mergeChanges:NO]; 

安德鲁

+0

是。我的错。退出此功能后,我会触发.image属性的错误。现在我是所有更改后的刷新对象。谢谢) – DAloG