DAloG
DAloG

Reputation: 137

CoreData memory overhead

Anyone can explain me how to minimize memory overhead in this NSManagedObject subclass

- (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];
}

After 2 hours of active using app, UIImagePNGRepresentation(image) generate 500MB. This is a data-object life-cycle

#   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

Sometimes object release correctly with this calls

#   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]

Upvotes: 0

Views: 1051

Answers (1)

adonoho
adonoho

Reputation: 4339

DAloG,

While it would be nice to see the property list, I believe you have a retain cycle between self.image and self.image.imageData. I think you should add a line of code at the end of your method.:

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

Andrew

Upvotes: 1

Related Questions