views:

270

answers:

8

Hey all,

Can someone please explain me how garbage collection is working?

(I'm using C# and Java).

+2  A: 

Try the book Garbage Collection: Algorithms for Automatic Dynamic Memory Management. It wont have the more recent stuff in it, but it'll get you on your way.

Tom Hawtin - tackline
+5  A: 

The basic idea behind Garbage Collection is that you don't have to care about memory management. What the Garbage Collectod does is to periodically check upon objects references and find the one that are no more used (not referenced anymore) to reclaim their memory and compact the other. Garbage Collector use various algorithms to perform their work and they differ for some details from a lenguage to another. Wikipedia gives you a good starting point. If you're looking for more in depth information about actual implementation of various Garbage Collectors (Java, .NET, ..) you can check here and here or search google for more.

Stefano Driussi
A: 

It's a bit complex question to give a short answer. Here start reading with this link.

A: 

For a very interesting article about GC in .Net read: http://msdn.microsoft.com/en-us/magazine/bb985011.aspx

mandel
That link doesn't work
Ray Booysen
strange, try this one : http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
mandel
A: 

Here's a nice webcast that discusses simple mark-and-sweep (non-generational) garbage collection, complete with nice animations to help clairy the concept.

ctacke
+1  A: 

Perfmon provides a number of counters for GC related performance...

Matt Davison
A: 

I think you need to know that the Garbage Collector is a thread that runs on your program freeing the memory occupied by the objects whose references make them unreachable. You also need to know that the moment in which int GC runs can't be predicted, you may make a call to System.gc() to make a suggestion for the GC to run but not to make it run, it is the JVM who'll take that decision.

If you have: Object objectReference = null;

the Object referenced by objectReference is GC bait. The subjects of "islands of isolation" and how the finalize() method works are interesting topics to read. I suggest a quick google search on both.

omgzor