Re: Dispose pattern (was: Re: GC pros and cons)
Re: Dispose pattern (was: Re: GC pros and cons)
- Subject: Re: Dispose pattern (was: Re: GC pros and cons)
- From: "Stephen J. Butler" <email@hidden>
- Date: Mon, 29 Jun 2009 00:23:36 -0500
On Sun, Jun 28, 2009 at 10:50 AM, Konrad Neitzel<email@hidden> wrote:
> But a garbage collector must be able to detect an object which memory can / must be released. And for all this, the system has to do some work.
>
> I still have to read much more about the GC used within Objective-C / Cocoa so I am not sure, if I am not writing some kind of "bullshit" from your view:
>
> Isn't it possible to simply flag an object to be removed? Is there a dispose pattern in Objective-C?
> (e.g. http://www.codeproject.com/KB/cs/idisposable.aspx describes such a pattern in C#)
>
> Something like that would simply more the world together again. That way, the developer has some more control, the GC has less work and all are maybe happy?
>
> So is there such an Dispose Pattern in Objective-C using a GC or is there nothing like that?
That article does not say what you think it says. Dispose() does not
"flag an object to be removed". It is also not normally the place to
start setting all your instance variables to null. In fact, the only
thing it has to say about what you're asserting is this:
"You should also consider setting any large and expensive managed
objects that you own to null before calling Dispose. This is seldom
necessary, but it can help reduce the lifetime of the object by making
it eligible for garbage collection sooner. Of course, the definition
of large and expensive is subjective, and should be based on
performance profiling and measurement.
If you are creating a value type, you should avoid making it
disposable, and it should never contain unmanaged resources directly."
So, according to your own evidence, the effort put forth to null
instance variables should only be done when performance profiling and
measurement warrants it. Otherwise, it's simply doesn't have a
benefit. And in classes that don't wrap unmanaged resources, you
shouldn't write a Dispose() at all!
Everything else in that article about setting instance variables to
null has to do with breaking Dispose() loops when calling Dispose() on
owned objects. NOT because it makes them collectable faster, or easier
to collect.
Again, Dispose() is only for handling UNMANAGED resources, stuff the
GC doesn't know how to collect or can't collect. And if you want a
Dispose() like handling in Objective-C (just like Java), write a
"close" message and document that it should be called when the user is
done with the object. It gives you just as much benefit as Dispose()
does in C#.
I also challenge your assertion that a bunch of disconnected objects
are easier to collect than an entire graph of connected objects.
Without looking at the actual GC code, I could make an argument either
way. It would be entirely up to the implementation over which gives a
benefit, and GC implementations change all the time.
I have no idea why so many people are hell bent on circumventing the
GC. The biggest reason to use it is because then you don't have to
worry about managing your memory anymore. Yet here you all are,
inventing bogus ways to once again manage the memory! If you can't let
go and allow the system to do its thing, then don't use GC.
_______________________________________________
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