locked
GC.collect() is blocked call or non-blocked call RRS feed

  • Question

  • Hi, DotNet Folks

    Could you tell me that GC.collect() is blocked call or non-blocked call? In other words, if I call GC.collect, does the calling thread is blocked until collect is finished? If not, how many threads are used by collect method?

    Thanks,

     

    Rick

     

     

    Tuesday, July 12, 2011 6:48 PM

Answers

  • From how they use it in the sample it suggest it is blocking call and I assume all calls to be blocking unless it specifically says it is non-blocking in the documention.

    http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Tuesday, July 12, 2011 7:28 PM
  • Hi Rick,

    Both the explicit and implicit calls to GC's collect is a blocking call. Infact the CLR suspends all other threads in the application before starting the collect phase.

    Once the collection is done, the CLR compacts the managed heap in order to bring all the allocated objects together.  This process involves re-referencing the objects being held by the application threads and thats the reason all the application threads need to be blocked during GD collect.

    Hope that helps.

    have fun,

    Sezhiyan

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Wednesday, July 13, 2011 4:47 AM
  • Found an interesting blog about the garbage collector and how it blocks.

    http://geekswithblogs.net/sdorman/archive/2008/11/07/clr-4.0-garbage-collection-changes.aspx

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Wednesday, July 13, 2011 7:03 AM

All replies

  • From how they use it in the sample it suggest it is blocking call and I assume all calls to be blocking unless it specifically says it is non-blocking in the documention.

    http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Tuesday, July 12, 2011 7:28 PM
  • Hi Rick,

    Both the explicit and implicit calls to GC's collect is a blocking call. Infact the CLR suspends all other threads in the application before starting the collect phase.

    Once the collection is done, the CLR compacts the managed heap in order to bring all the allocated objects together.  This process involves re-referencing the objects being held by the application threads and thats the reason all the application threads need to be blocked during GD collect.

    Hope that helps.

    have fun,

    Sezhiyan

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Wednesday, July 13, 2011 4:47 AM
  • Found an interesting blog about the garbage collector and how it blocks.

    http://geekswithblogs.net/sdorman/archive/2008/11/07/clr-4.0-garbage-collection-changes.aspx

    • Marked as answer by Min Zhu Monday, July 18, 2011 3:19 AM
    Wednesday, July 13, 2011 7:03 AM