leaking memory in Core Data

  • Thanks to the advice of Apple employees who responded to my earlier
    question, I dramatically reduced my memory use.  I am, however, still
    seeing a very low rate of memory leaks.  These are very rare (i found
    6 after processing 10K of records), but I'm doing a massive import,
    and they add up.  They occur regardless of whether I'm using manual
    memory management or garbage collection, and eventually run me out of
    memory or kill the performance in 64bits.

    Using Malloc Debug, i've tracked it down to something that happens
    after I establish a relationship between two objects, but the leak
    occurs deep in the Core Data methods that follow.  I've pasted in the
    call stack below, with my methods bolded.  The
    "addHierarchyLeafObject:" method is just the standard one produced by
    XCode at this point.

    I could probably handle importing by spawning a series of child
    processes, so that the leaks die with the process, but I'd prefer to
    avoid the hassle.  Is there any way to set up a relationship without
    running into this problem?

    Thanks for any advice,

    John

    STACK
    Address /1e9ae000/size/0000326c
    0x00067ea9  / 0x67ea9 / libMallocDebug.A.dylib
    0x913fc444  / PF_ALLOCATE_OBJECT_ARRAY / CoreData
    0x9141f6c0  / newFetchedRowsForFetchPlan_MT / CoreData
    0x91452ae1  / -[NSSQLCore newFetchedPKsForSourceID:andRelationship:] /
    CoreData
    0x9145258f  / -[NSSQLCore
    retainedRelationshipDataWithSourceID:forRelationship:withContext:] /
    CoreData
    0x914522f7  / -[NSFaultHandler
    retainedFulfillAggregateFaultForObject:andRelationship:withContext:] /
    CoreData
    0x91429241  / -[_NSFaultingMutableSet willRead] / CoreData
    0x9142c037  / -[_NSFaultingMutableSet count] / CoreData
    0x9141003c  / -[NSManagedObject(_NSInternalMethods)
    _newPropertiesForRetainedTypes:andCopiedTypes:preserveFaults:] /
    CoreData
    0x9140fd70  / -[NSManagedObject(_NSInternalMethods) _allProperties] /
    CoreData
    0x9140fc64  / -[NSManagedObjectContext(_NSInternalChangeProcessing)
    _establishEventSnapshotsForObject:] / CoreData
    0x91413036  / _PFFastMOCObjectWillChange / CoreData
    0x91443a53  / -[NSManagedObject
    willChangeValueForKey:withSetMutation:usingObjects:] / CoreData
    0x000087ad  / -[jt_HierarchyObject addHierarchyLeafObject:] / ImportApp
    0x91451434  / -[NSManagedObject(_NSInternalMethods)
    _includeObject:intoPropertyWithKey:andIndex:] / CoreData
    0x914128b1  / -[NSManagedObject(_NSInternalMethods)
    _didChangeValue:forRelationship:named:withInverse:] / CoreData
    0x925e6f8e  / NSKVONotify / Foundation
    0x925639c2  / -[NSObject(NSKeyValueObserverNotification)
    didChangeValueForKey:] / Foundation
    0x914125c7  / -[NSManagedObject didChangeValueForKey:] / CoreData
    0x000081d5  / -[jt_ThreadedImport createHierarchy] / ImportApp
    0x9256704d  / -[NSThread main] / Foundation
    0x92566bf4  / __NSThread__main__ / Foundation
    0x96b82075  / _pthread_start / libSystem.B.dylib
    0x96b81f32  / thread_start / libSystem.B.dylib
    ENDSTACK
  • > I am, however, still  seeing a very low rate of memory leaks.  These
    > are very rare (i found
    > 6 after processing 10K of records), but I'm doing a massive import,
    > and they add up.

    Hmm.  I've looked at the stack trace and the control flow here, and
    don't see any obvious leaks.  I haven't seen anything like this from
    our unit tests (running heap on nightly builds == yummy goodness).
    That and the fact it only happens 6 / 10,000 times suggests something
    very odd, possibly threading related, is going on.

    Double check you're following the multi-threading guidelines in <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdMultiThreading.html
    >

    File a bug report with bugreport.apple.com and we'll take a more
    extended look.  If you attach a sample project with source that
    reproduces your problem, it would really expedite things.

    - Ben
  • Thanks for checking.  I did follow the threading guidelines - there's
    a separate PersistentStoreCoordinator and MOC for the thread, both of
    which are released when it's done.  If i comment out the code that
    populates a relationship, there are no leaks, so i'm pretty sure it's
    specific to that.

    I'll file a bug now and, once i get the import implemented as a
    separate process, see if i can distill things down to a simple case.
    If you think de-threading the code and rechecking for leaks would be
    informative, I'm willing to give that a try.  I could also send you
    the full code of the threaded message off list, if you think it would
    be helpful.

    Cheers,

    John

    On Nov 11, 2007, at 8:53 PM, Ben Trumbull wrote:

    >> I am, however, still  seeing a very low rate of memory leaks.
    >> These are very rare (i found
    >> 6 after processing 10K of records), but I'm doing a massive
    >> import,  and they add up.
    >
    > Hmm.  I've looked at the stack trace and the control flow here, and
    > don't see any obvious leaks.  I haven't seen anything like this from
    > our unit tests (running heap on nightly builds == yummy goodness).
    > That and the fact it only happens 6 / 10,000 times suggests
    > something very odd, possibly threading related, is going on.
    >
    > Double check you're following the multi-threading guidelines in <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdMultiThreading.html
    > >
    >
    > File a bug report with bugreport.apple.com and we'll take a more
    > extended look.  If you attach a sample project with source that
    > reproduces your problem, it would really expedite things.
    >
    > - Ben
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today