Re: "forcing" garbage collection

Tech-Archive recommends: Fix windows errors by optimizing your registry



Jon Mcleod wrote:
I'm trying to wrap my ming around C#/CLR garbage collection. In an ASP.NET project, I'm having a problem because an object destructor

destructor = finalizer ?

is being called by another thread,

Some code explicit calling a finalizer ?

long after my code is done. I'm wanting to know if I can force it to happen immediately (when the object goes out of scope)

Making an explicit call ? Or an implicit call ? And what about the
reference in the other thread ?

without going back and adding all of the required Dispose() calls..

public Method()
{
CustomClass o = new CustomClass(..);
..
..
..
};

Is there a way to "force" the CustomClass destructor to be called right when the object goes out of scope, by the same thread as the Method?

Dispose is for unmanaged resources.

Garbage collection is for managed resources.

If the object only holds managed resources then let the GC do
what it wants to do when it want to do it.

If the object holds unmanaged resources then use the
Dispose pattern.

Arne



.