gpt4 book ai didi

ios - CoreData 内存开销

转载 作者:行者123 更新时间:2023-11-29 04:58:53 25 4
gpt4 key购买 nike

任何人都可以向我解释如何最小化此 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]

最佳答案

DALoG,

虽然很高兴看到属性列表,但我相信您在 self.image 和 self.image.imageData 之间有一个保留周期。我认为你应该在方法末尾添加一行代码。:

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

安德鲁

关于ios - CoreData 内存开销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7399587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com