[Mono-devel-list] Mono GC

Jerome Laban jlaban at wanadoo.fr
Thu Mar 6 15:45:48 EST 2003

Hi monoers,

    Here is some simple code involving finalization process :

using System;

namespace myns {
 class Class2 {
  ~Class2() {
   Console.WriteLine("Finalizer called");

 class Class1 {
  Class2 a = new Class2();

  void Run() {
   a = null;
   GC.Collect();             /// Finalizer should be called from here

  static void Main(string[] args) {
   Class1 instance = new Class1();

   /// GC.Collect();        /// It is actually called from here in mono

   Console.ReadLine();  /// Finalizers are called asynchonously, let the
                                  /// time for the GC to do it.

The problem here is that the finalizer is not called roght after the first
call to GC.Collect, as it is on MS.NET runtime. Mono 0.21 needs to have a
second call to GC.Collect avec the call to Class1.Run to have the finalizer

    My guess is that the internal reference to the instance of Class2
maintained somewhere in something like the stack and during its collection
passes, the GC finds this ghost reference, thus never collecting the freed
memory. Leaving the Class1.Run scope somehow erases this reference.

    Is my assumption correct ? If it is, how should this be addressed ?


More information about the Mono-devel-list mailing list