none
what GC.WaitForPendingFinalizers does RRS feed

  • Question

  • what GC.WaitForPendingFinalizers(); does ? does it call garbage collector immediately or something else ?

    GC.Collect();
    GC.WaitForPendingFinalizers();

    Sunday, April 14, 2019 11:39 AM

Answers

  • When you execute GC.Collect, any objects that are collected are examined to see if they have a Finalizer. If they do, then GC.Collect does not free their memory. Instead, they are moved to a place that is called the "finalization queue". A separate thread is launched internally that executes the Finalize method of each object in the queue. After each Finalize is executed, the object is once again made available to the Garbage Collector, and the memory of the object will be freed the next time that the Garbage Collector runs.

    From the preceding explanation you can imagine what WaitForPendingFinalizers does: it blocks until all the Finalizers in the finalization queue have finished executing.

    • Marked as answer by Sudip_inn Saturday, April 27, 2019 3:48 PM
    Sunday, April 14, 2019 12:43 PM
    Moderator
  • Hi Studip_inn,

    Thank you for posting here.

    Alberto Poblacion gives you a good explanation. As a supplement, the suggestion in StackOverFlow gives more details.

    https://stackoverflow.com/questions/12265598/is-correct-to-use-gc-collect-gc-waitforpendingfinalizers

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, April 27, 2019 3:48 PM
    Monday, April 15, 2019 5:47 AM
    Moderator

All replies

  • When you execute GC.Collect, any objects that are collected are examined to see if they have a Finalizer. If they do, then GC.Collect does not free their memory. Instead, they are moved to a place that is called the "finalization queue". A separate thread is launched internally that executes the Finalize method of each object in the queue. After each Finalize is executed, the object is once again made available to the Garbage Collector, and the memory of the object will be freed the next time that the Garbage Collector runs.

    From the preceding explanation you can imagine what WaitForPendingFinalizers does: it blocks until all the Finalizers in the finalization queue have finished executing.

    • Marked as answer by Sudip_inn Saturday, April 27, 2019 3:48 PM
    Sunday, April 14, 2019 12:43 PM
    Moderator
  • Hi Studip_inn,

    Thank you for posting here.

    Alberto Poblacion gives you a good explanation. As a supplement, the suggestion in StackOverFlow gives more details.

    https://stackoverflow.com/questions/12265598/is-correct-to-use-gc-collect-gc-waitforpendingfinalizers

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Saturday, April 27, 2019 3:48 PM
    Monday, April 15, 2019 5:47 AM
    Moderator