I'm having a trouble to understand what is the difference between WaitForFullGCComplete
and WaitForPendingFinalizers + collect
.
I already know that when a new object is created (which has a finalizer ) — A reference to that object is created in the finalization queue
.
Now - when a GC .collect
occurs and finds that the object should be collected , the reference from the finalization queue
moves to the f-reachable queue
.
These references in the queue are considered as a root
. Now — a special thread awakes from sleep at the f-reachable queue
and run each object's finalize
method .
Notice this is done AFTER the collect
phase. So only the next time we run GC.collect
the object would be truly gone.
If we want to wait until the f-reachable queue's
thread is done with all the finalize
methods - we should call : WaitForPendingFinalizers
.
And now if we want full collect , then we should run GC.collect again!.
If so - it seems that a full GC can be made by :
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
So why would I need GC.WaitForFullGCComplete()
?
The docs says :
Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime has completed.
Question
I don't understand : I already know when Gc has completed and it is at that point :
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
//HERE <--
So when should I use that method as opposed to my code?
NB
(I saw it's usages here and here )
but their usage is not complete because - Not to mention that there is another method which should must be called in pairs :
From CLR via C# 4:
Note that you should always call the
WaitForFullGCApproach
andWaitForFullGCComplete
methods in pairs because the CLR handles them as pairs internally.