Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do when "memory leaks" are caused by the Foundation framework?

The application I'm working on at the moment is accumulating more and more memory as time passes. It's not really a memory leak, since the Leaks tool doesn't recognize it as a leak, but it is something that keeps on taking more and more memory.

I've been using Instruments (Memory Allocation tool) to find out why this happens, and I am now making heapshots in order to find the memory differences between different points in the running cycle. It seems that after each cycle, the memory used increases by around 560 KB.

This is how it looks like in the Instruments tool:

Instruments initial

And when choosing the CFString row:

CFString allocation

The full Stack Trace from the right side looks like this:

   0 CoreFoundation _CFRuntimeCreateInstance
   1 CoreFoundation __CFStringCreateImmutableFunnel3
   2 CoreFoundation CFStringCreateWithBytes
   3 Foundation -[NSString initWithCoder:]
   4 Foundation _decodeObject_old
   5 SyncServices -[ISDProperty initWithCoder:]
   6 SyncServices -[ISDRelationship initWithCoder:]
   7 Foundation _decodeObject_old
   8 Foundation _decodeValueOfObjCType
   9 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  10 Foundation -[NSArray(NSArray) initWithCoder:]
  11 Foundation _decodeObject_old
  12 SyncServices -[ISDEntity initWithCoder:]
  13 Foundation _decodeObject_old
  14 Foundation _decodeValueOfObjCType
  15 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  16 Foundation -[NSArray(NSArray) initWithCoder:]
  17 Foundation _decodeObject_old
  18 SyncServices -[ISDRelationship initWithCoder:]
  19 Foundation _decodeObject_old
  20 Foundation _decodeValueOfObjCType
  21 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  22 Foundation -[NSArray(NSArray) initWithCoder:]
  23 Foundation _decodeObject_old
  24 SyncServices -[ISDEntity initWithCoder:]
  25 Foundation _decodeObject_old
  26 Foundation _decodeValueOfObjCType
  27 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  28 Foundation -[NSArray(NSArray) initWithCoder:]
  29 Foundation _decodeObject_old
  30 SyncServices -[ISDRelationship initWithCoder:]
  31 Foundation _decodeObject_old
  32 Foundation _decodeValueOfObjCType
  33 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  34 Foundation -[NSArray(NSArray) initWithCoder:]
  35 Foundation _decodeObject_old
  36 SyncServices -[ISDEntity initWithCoder:]
  37 Foundation _decodeObject_old
  38 Foundation _decodeValueOfObjCType
  39 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  40 Foundation -[NSArray(NSArray) initWithCoder:]
  41 Foundation _decodeObject_old
  42 SyncServices -[ISDRelationship initWithCoder:]
  43 Foundation _decodeObject_old
  44 Foundation _decodeValueOfObjCType
  45 Foundation -[NSUnarchiver decodeValueOfObjCType:at:]
  46 Foundation -[NSArray(NSArray) initWithCoder:]
  47 Foundation _decodeObject_old
  48 SyncServices -[ISDEntity initWithCoder:]
  49 Foundation _decodeObject_old
  50 Foundation +[NSUnarchiver unarchiveObjectWithData:]
  51 SyncServices -[ISDObjectGraphWrapper initWithCoder:]
  52 Foundation -[NSKeyedPortCoder decodeObjectForKey:]
  53 Foundation -[NSArray(NSArray) initWithCoder:]
  54 Foundation -[NSKeyedPortCoder decodeObjectForKey:]
  55 Foundation -[NSKeyedPortCoder _decodeObjectNoKey]
  56 Foundation -[NSKeyedPortCoder _walkAndDecodeDataWithType:at:chase:invocation:inStructure:]
  57 Foundation decodeInvocationArguments
  58 Foundation -[NSKeyedPortCoder decodeInvocation]
  59 Foundation -[NSKeyedPortCoder decodeObjectForKey:]
  60 Foundation -[NSConnection handleRequest:sequence:]
  61 Foundation -[NSConnection handlePortCoder:]
  62 Foundation -[NSConnection dispatchWithComponents:]
  63 Foundation __NSFireMachPort
  64 CoreFoundation __CFMachPortPerform
  65 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
  66 CoreFoundation __CFRunLoopDoSource1
  67 CoreFoundation __CFRunLoopRun
  68 CoreFoundation CFRunLoopRunSpecific
  69 Foundation -[NSRunLoop(NSRunLoop) runMode:beforeDate:]
  70 SyncServices -[ISyncConcreteSession _waitForTransitionFromPhase:untilDate:]
  71 SyncServices +[ISyncSession _sessionWithClient:entityNames:beforeDate:clientHasTruthForEntityNames:quietlyPushTruth:target:selector:anchors:hasChanges:skip:error:]
  72 SyncServices +[ISyncSession beginSessionWithClient:entityNames:beforeDate:]
  73 SyncServices -[ISyncConcreteSessionDriver _beginSyncSession:]
  74 SyncServices -[ISyncConcreteSessionDriver _preSync]
  75 SyncServices -[ISyncConcreteSessionDriver _sync:]
  76 SyncServices -[ISyncConcreteSessionDriver sync]
  77 **Our application** - [SLSyncOperation performLocalSync] /Users/andrei/Desktop/MacOSX_Client/osx/Classes/SLSyncOperation.m:94
  78 **Our application** -[SLSyncOperation main] /Users/andrei/Desktop/MacOSX_Client/osx/Classes/SLSyncOperation.m:251
  79 Foundation -[__NSOperationInternal start]
  80 Foundation ____NSOQSchedule_block_invoke_2
  81 libdispatch.dylib _dispatch_call_block_and_release
  82 libdispatch.dylib _dispatch_worker_thread2
  83 libsystem_c.dylib _pthread_wqthread
  84 libsystem_c.dylib start_wqthread

I am using SyncServices in order to get contact information. I'm using an ISyncSessionDriver to check (sync) for new information at a certain time interval (10 seconds, can be seen on the memory allocation image as well). The code looks like this.

SLSyncSessionDriverDataSource *dataSource = [[SLSyncSessionDriverDataSource alloc] initWithManagedObjectModel:managedObjectModel context:managedObjectContext];

ISyncSessionDriver *localDriver = [ISyncSessionDriver sessionDriverWithDataSource:dataSource];
SLSyncSessionDriverDelegate *theDelegate = [[SLSyncSessionDriverDelegate alloc] init];
[localDriver setDelegate:theDelegate];

[theDelegate release];
[dataSource release];    

[localDriver sync];

Thanks for any help you could give me!

like image 760
Alex Avatar asked Nov 05 '22 15:11

Alex


1 Answers

Assuming you've found a real leak, a few ideas:

  • You can only run the sync when there's likely to be new information (watch for changes in ~/Library/Application Support/AddressBook with kqueue or FSEvents).

  • You could run the sync in a child process which you recycle periodically, or with the previous suggestion, just have a single run of the process do a sync then exit.

Sync Services never worked terribly well and is deprecated as of 10.7, so I imagine it won't see much if any additional work. I don't think it'd be a waste of time to come up with a self-contained example and submit it as a bug, especially if it's leaking this badly. Also, I'd suggest filing another bug on the Address Book framework explaining that you'd like the ability to check for/be notified of new/updated information.

like image 119
Nicholas Riley Avatar answered Nov 15 '22 06:11

Nicholas Riley