What exactly does this line of code do?
GC.Collect();
GC.WaitForPendingFinalizers();
What exactly does this line of code do?
GC.Collect();
GC.WaitForPendingFinalizers();
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Abhishek YadavPosted Oct 19, 2022, 8:38 AM
GC.Collect() technique used to name rubbish collector explicitly. GC.WaitForPendingFinalizers() to await accumulate unused item and clean reminiscence allotted to the item. BTW you may undergo Garbage collector idea in deep . You can be capable of recognize it.
Brahma Prakash ShuklaPosted Oct 17, 2022, 4:51 PM
GC.collect ()
if you have a operation that is a rare operation which uses a large object graph, and you know that your going to be "idle" afterwards, you might want to call
GC.Collectimmediately afterwards to release the memory used by the objects. That being said, this is often still best left up to the system to handle.For the most part, I've found the most useful scenario for
GC.Collect()is for debugging. It can help you guarantee that you don't have a leak, since it allows you to force a full Gen2 collection.GC.WaitForPendingFinalizers() -
When the garbage collector finds objects that can be reclaimed, it checks each object to determine the object's finalization requirements. If an object implements a finalizer and has not disabled finalization by calling SuppressFinalize, the object is placed in a list of objects that are marked as ready for finalization. The garbage collector calls the Finalize methods for the objects in this list and removes the entries from the list. This method blocks until all finalizers have run to completion.
The thread on which finalizers are run is unspecified, so there is no guarantee that this method will terminate. However, this thread can be interrupted by another thread while the WaitForPendingFinalizers method is in progress. For example, you can start another thread that waits for a period of time and then interrupts this thread if this thread is still suspended.
Rajanikant HawaldarPosted Oct 17, 2022, 10:42 AM
GC.Collect() method used to call garbage collector explicitly.
GC.WaitForPendingFinalizers() to wait for collect unused object and clear memory allocated to the object.
BTW you can go through Garbage collector concept in deep . You will be able to understand it.
Vishal YelvePosted Oct 17, 2022, 9:09 AM
GC.Collect(); -> It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.
GC.WaitForPendingFinalizers(); -> Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue.
Please refer to the below link for more details
https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-6.0
https://learn.microsoft.com/en-us/dotnet/api/system.gc.waitforpendingfinalizers?view=net-6.0