Store 'this' on completion

How can I define code that stores 'this' during class completion? How should the garbage collector behave (if defined somewhere)?

In my opinion, the GC should complete the class instance several times, and the next test application should print “66”, but the finalizer runs only once, forcing the application to print “6”.

A few lines of code:

using System;

namespace Test
{
    class Finalized
    {
        ~Finalized()
        {
            Program.mFinalized = this;
        }

        public int X = 5;
    }

    class Program
    {
        public static Finalized mFinalized = null;

        static void Main(string[] args)
        {
            Finalized asd = new Finalized();
            asd.X = 6;
            asd = null;
            GC.Collect();

            if (mFinalized != null)
                Console.Write("{0}", mFinalized.X);

            mFinalized = null;
            GC.Collect();

            if (mFinalized != null)
                Console.Write("{0}", mFinalized.X);
        }
    }
}

I am trying to understand how finalizers manage instance memory. In my application, I could use the instance link again for further processing.

, "" ( , ). ? ? , ?

, .

+5
4

. ( this), obejct GC. .NET, GC , GC.ReRegisterForFinalize.

. Microsoft.NET Framework.

+8

GC.Collect , . , GC . ( , ), . , IIRC, .

+4

.

MSDN : " , , ". " #" : " ​​. ". 2 , , , - ?

0

The finalizer receives only once. You can freely take a seat and prevent garbage collection. But as soon as the object is again available to the GC, it does not start the finalizer.

0
source

All Articles