Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store 'this' at finalization

How could be defined a code that store 'this' during class finalization? How the garbage collector should behave (if defined somewhere)?

In my mind the GC should finalize multiple times the class instance, and the following test application shall print "66", but the finalizer is executed only once, causing the application to print "6".

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);
        }
    }
}

What I'm trying to do is to understand how finalizers manage instance memory. In my application could be desiderable to re-use instance reference again for further processing.

It's clear that the finalizer doesn't "free" memory (at least in my test application). May the memory chunk be reused for other purposes? Or even freed? And if it isn't, that would be a memory leak or what?

Now, I'm confused more than before.

like image 274
Luca Avatar asked Aug 16 '12 23:08

Luca


People also ask

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected. For example, closing a opened file, closing a opened database Connection. Finalization is a process done to run the code when that particular object is deallocated.

What finalization means?

transitive verb. 1 : to put in final or finished form soon my conclusion will be finalized— D. D. Eisenhower. 2 : to give final approval to finalizing the papers prepared … by his staff — Newsweek.

How do you use finalize?

The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the object is destroyed. The method is protected and therefore is accessible only through this class or through a derived class.

What is the Finalize method called?

The finalize() method is called the finalizer. Finalizers get invoked when JVM figures out that this particular instance should be garbage collected.


1 Answers

This is due to Resurrection. By storing the object in another variable during finalization (assigning this to a variable), you resurrect the obejct instance as far as the GC is concerned. You are allowed to resurrect your object in .NET, and you can actually cause the GC to finalize the object more than once, but you have to explicitly request it via GC.ReRegisterForFinalize .

For details, see Automatic Memory Management in the Microsoft .NET Framework.

like image 196
Reed Copsey Avatar answered Oct 05 '22 11:10

Reed Copsey