C# language: Garbage Collection, SuppressFinalize

Viewed 3523

I'm reading "The C# Language", 4th Edition, it talks about garbage collection as below:

"BILL WAGNER: The following rule is an important difference between C# and other managed environments.

Prior to an application’s termination, destructor's for all of its objects that have not yet been garbage collected are called, unless such cleanup has been suppressed (by a call to the library method GC.SuppressFinalize, for example)."

So I have a couple of questions here:

  • Q1. Why .net is different from other managed environments (I suppose this is hinting Java?) here? Any particular design concern?

  • Q2. What will happened to objects that GC.SuppressFinalize is called? I understand that this means GC will not call such objects' finalizer (destructor), if so, when will these objects got really destroyed, so that the allocated memory bits are returned to the heap? Otherwise there'll be Memory Leak?

5 Answers

There is because once call GC.Collect() The object with finalize will move to next generation at least once.

We can call GC.SuppressFinalize, and GC will know this object is de-reference, we can remove this object and compact heap then.

Usually, this implement with dispose pattern

Related