none
How to tell if .NET garbate collection is in progress? RRS feed

  • Question

  • I have to start GC manually. However the .NET runtime can also start GC at its liesure. I seem to randomly encounter deadlock if I try to start GC and then call WaitForPendingFinalizers. When I examine the threads of the process I can see two threads that are both waiting for objects. Meanwhile my OleMessageFilter keeps poping up the "busy dialog" (that call is traced from .NET too).

    Is there a way to tell if .NET GC is already in progress before I start it up? Will the runtime try to start GC on its own while I am running it?


    R.D. Holland
    Thursday, June 9, 2011 4:47 PM

Answers

  • In general, you should never "start the GC manually".  The garbage collector runs continually in the background.  Calling GC.Collect yourself is almost never a good idea - it messes up the GC's heuristics, and actually tends to make your program's runtime behavior far worse than if you leave it alone.

     

    That being said, you can track the GC behavior by calling GC.RegisterForFullGCNotification.  I would argue that a normal program should never do this.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, June 9, 2011 5:41 PM
    Moderator

All replies

  • In general, you should never "start the GC manually".  The garbage collector runs continually in the background.  Calling GC.Collect yourself is almost never a good idea - it messes up the GC's heuristics, and actually tends to make your program's runtime behavior far worse than if you leave it alone.

     

    That being said, you can track the GC behavior by calling GC.RegisterForFullGCNotification.  I would argue that a normal program should never do this.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, June 9, 2011 5:41 PM
    Moderator
  • Agree with Reed, it is never advisable to call explicit garbage collectioc.

    Moreover you seem to yourself mentioned the fact that you are Clr also is invoking the garbage collection while you are making the explicit calls, so that clearly shows that you dont have to make an explicit call.

    Let us know if we are missing anything that you have in mind, that makes you still call GC.Collect from your code.

    Tuesday, June 14, 2011 5:19 AM
  • It's not entirely true that it's never advisable to do this. There are some cases where it may be appropriate, but they are incredibly rare. There are even a couple of cases where you have to make sure garbage collection has occurred.

    My philosophy in this is like most of the programming rules. Yes, there are exceptions. If you have to ask, your situation is probably not one of them. Follow the rule until it breaks and you have a very good reason for violating it.

    Tuesday, June 14, 2011 2:22 PM
  • Hi R.D. Holland,

    I am marking this issue as "Answered". If you have any new questions or concerns about this issue, please feel free to let me know.

    Thank you and have a nice day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 20, 2011 3:29 AM
    Moderator