Groups | Blog | Home
all groups > dotnet faqs > september 2006 >

dotnet faqs : Garbage collection


luigi
9/17/2006 1:08:59 PM
If i dispose all object in my vb.net c# code what perfomances advantage
i have?

mant thanks
Jon Shemitz
9/17/2006 2:32:15 PM
[quoted text, click to view]

As written, you get a performance hit, or a negative performance
advantage. Disposing of an object does not reclaim memory, nor does it
hasten garbage collection; writing and calling an empty Dispose method
merely wastes space and time.

Otoh, if you don't really mean "all object[s]" but actually mean "all
objects that implement IDispose" then you are closing resources when
you are done with them, and avoiding "finalization costs."
Finalization is expensive because a dead object must be brought back
to life, promoted to the next generation, and relocated; it then
sticks around, taking up memory, until the next higher-level
collection. So, by calling Dispose on every object that implements it,
you are reducing memory consumption and garbage collection costs.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Jon Shemitz
9/25/2006 3:13:19 PM
[quoted text, click to view]

No, I meant what I said. Invoking IDispose allows data to be closed
when you're done with, and thus avoids the use of the finalizer. The
finalizer is a fail-safe, but using it adds (avoidable) relocation
costs and means that the gc has to look at the dead object (at least)
twice.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Scott M.
9/25/2006 6:03:56 PM
[quoted text, click to view]

You mean you are increasing memory consumption and garbage collection costs,
not reducing them.

Jon Shemitz
9/26/2006 7:10:37 PM
[quoted text, click to view]

No, in most cases it's spending time to save time. When an IDispose
class has a finalizer, its Dispose methods typically runs the exact
same code that the finalizer runs, plus it calls GC.SuppressFinalize.
This is saving all the CPU costs involved in running the finalizer,
plus it allows the memory to be reused sooner.

When an IDispose class does not have a finalizer, it usually has
private reference(s) to handle class(es) that do have a finalizer, so
the call to Dispose allows it to close its handle(s), thus (again)
saving all the CPU costs involved in running the handle finalizer(s),
plus allowing the memory to be reused sooner.

The only time calling Dispose doesn't save time is with the minority
of IDispose implementations that aren't avoiding a finalizer, that are
doing something like restoring the GUI cursor or noting how long it's
been since a stopwatch class has been created.

[quoted text, click to view]

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Scott M.
9/26/2006 9:34:22 PM
Yes, but calling Dispose on every class that exposes it will cause
additional CPU costs, not less.

[quoted text, click to view]

AddThis Social Bookmark Button