Re: Adding an observer is memory expensive
Re: Adding an observer is memory expensive
- Subject: Re: Adding an observer is memory expensive
- From: Greg Guerin <email@hidden>
- Date: Thu, 10 Sep 2009 12:18:08 -0700
Torsten Radtke wrote:
I observed excessive memory usage (working in a garbage-collected
environment under 10.6) if I add an observer to a KVO-compliant
object. So if I add an observer to say 100000 objects, Instruments
shows that 100000 2KB blocks of memory are allocated (additionaly
there are another 100000 48B blocks and 200000 32B blocks).
Is this when the first observer is added to each of the 100k objects?
Additionaly, if I remove the observer only 100000 32B blocks are
released and collected. This also does not seem right to me.
Suppose that each observed object is doing lazy instantiation of the
internal objects needed for managing its observers. What would you
expect to see?
The first thing I'd expect is that the cost of adding the first
observer would be much higher than adding a 2nd or 3rd observer. The
second thing I'd expect is that removing all observers would leave
the internal objects in place, because the observed class can't know
you won't add another observer in the future.
In other words, the baseline cost of managing observers is paid once,
when the 1st observer is added. After that, only the marginal cost
of each observer is added or removed as each observer is added or
removed.
If you are unhappy about the memory costs of managing observers, then
you should probably investigate alternatives. Depending on the class
of the observed object, you may be able to save memory, but you have
to weigh that against the cost of developing and maintaining your own
code, and whether it has or needs the same generality as KVO-
compliance. That may or may not be a good engineering tradeoff. YMMV.
-- GG
_______________________________________________
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