任何人都可以解释如何最小化此NSManagedObject子类中的内存开销
- (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]
答案 0 :(得分:1)
DALOG,
虽然看到属性列表会很高兴,但我相信你在self.image和self.image.imageData之间有一个保留周期。我认为你应该在方法的末尾添加一行代码。:
[self.managedObjectContext refreshObject:self.image mergeChanges:NO];
[self.managedObjectContext refreshObject:self mergeChanges:NO];
安德鲁