Re: Removing Observers eats up memory
Re: Removing Observers eats up memory
- Subject: Re: Removing Observers eats up memory
- From: Ken Thomases <email@hidden>
- Date: Mon, 25 Jan 2016 11:47:51 -0600
On Jan 25, 2016, at 3:10 AM, Markus Spoettl <email@hidden> wrote:
>
> I have a view controller with a table view that is bound to an array containing around 1000-10000 model objects. The same view controller registers itself as observer of all the objects' properties. These objects are actually proxies for other model objects and are created by my view controller (there is no outside reference to these objects).
>
> When the array changes, I remove all observers from these objects before re-observing the new objects.
If at all possible, you should do this in a more targeted fashion. Add and remove observers only from the objects which were actually added or removed from the array. If you have properly encapsulated modifications of the array in indexed collection mutating accessors, this is trivial.
> Sometimes (on El Capitan) this removing of observers causes my app to freeze and eat memory at an alarming rate. Sometimes this spirals completely out of control until all memory is exhausted, sometimes it stops after a few gigabytes of mystery allocations and my app continues.
>
> The bug doesn't surface all the time, only 30-50% of all tries.
> Has anyone any idea how the removing of observers can cause this kind of death spiral?
You should use Instruments to trace exactly what's going on during one of the runaway cases.
By the way, you should consider implementing the observationInfo property on your observed class. This avoids KVO storing observation info in a side table. Note that the property is a void pointer, not an object pointer, and therefore does no memory management.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden