garbage collection

  • Thread starter Sebastiaan Olijerhoek
  • Start date
S

Sebastiaan Olijerhoek

Garbage collection in .NET works fine. But as soon as two objects have
strong references to eachother (e.g. if you add an eventhandler) then the
references must be destroyed manually (e.g. in the Dispose methode) before
the garbage collection will remove the objects.

For the older C++ compilers which didn't have garbage collection tool were
available to check on memory leakages.

But is a tool or functionality within the framework available to back track
which objects refer to which and can't be reached by the main root?

Kind regards,
Sebastiaan.
 
J

Jon Skeet [C# MVP]

Sebastiaan Olijerhoek said:
Garbage collection in .NET works fine. But as soon as two objects have
strong references to eachother (e.g. if you add an eventhandler) then the
references must be destroyed manually (e.g. in the Dispose methode) before
the garbage collection will remove the objects.

Only if there's a strong reference to one of them somewhere else. Just
a simple cyclic relationship doesn't prevent garbage collection.
For the older C++ compilers which didn't have garbage collection tool were
available to check on memory leakages.

But is a tool or functionality within the framework available to back track
which objects refer to which and can't be reached by the main root?

There are various profilers available, I believe, but I can't say I've
used any of them.
 
A

Axel Müller

Sebastiaan Olijerhoek said:
Garbage collection in .NET works fine. But as soon as two objects have
strong references to eachother (e.g. if you add an eventhandler) then the
references must be destroyed manually (e.g. in the Dispose methode) before
the garbage collection will remove the objects.

Hello Sebastiaan,

NO !
1.) "Strong references" or "cyclic references" will be resolved
from the garbage collection - and have nothing to do with
the .NET-Dispose-pattern !!You don't have to care about
"strong" or "cyclic" references of your.NET - objects !

2.)You have to imlement the .NET Dispose-pattern
ONLY if you want to release managed or
unmanged ressources in your .NET objects before
the GC will destroy them later undetermined .

It is undetermined when the GC will destroy your objects.
To avoid this you have to implement the Dispose-pattern .

If you used a managed ressource like a MemoryStream or a
FileStream in your objects and you want to close the Stream
immediateley ( so that other users can use this ressource
and must not wait until the GC destroy this object)
you have to implement the Dispose-pattern !

BTW , what did you mean with adding an eventHandler ??
I did not understand why this is a "strong reference" problem ???

Regards
Axel Mueller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top