Guess the title must have provided you with the sufficient idea of what I want to know, but I must elaborate it a little here, so that you can get the exact idea.
I know the following about the Finalize
method:
We need to create a destructor in C# using
~Classname
inside the class. According to C#,Finalize
& destructors are synonyms (I read it on MSDN, I don't know if the last part of the statement is even correct or not).It is used to "gracefully release the unmanaged resources".
It can be called by the user (developer) accordingly, or when the application is running short on memory (managed heap), or at the end of the application.
Any object having the
Finalize
method will be added to the finalization queue during runtime.
The following image is taken from MSDN Magazine Article. It shows the Roots, Managed Heap (MH), Finalization Queue (FQ) & Freachable Queue (F-RQ). All this process occurs during runtime.
My questions are as follows:
How does the references to, objects present in MH are added to FQ? (Means, are they automatically added to FQ because they have
Finalize
or GC adds them by keeping their track.)Does GC runs through FQ as well to clear the references? (For the object references that uses
GC.SuppressFinalization
.)If MH has enough memory for the incoming objects for sometime, but at the same time the special thread in F-RQ is done with the
Finalize
method of the reference(s) present in the F-RQ than, at that instant will GC run specially for F-RQ to reclaim the memory or will it run for both MH & F-RQ or will it wait until MH falls short on memory?
EDIT:
For question 3: Consider the following image with all the references in F-RQ not reachable.
If there are any mistake(s) in the above stated questions or knowledge, feel free to point it out & an explanation to it will be really appreciated.