none
Calling the GC.Collect method

    Question

  •  

    I have a code like this :

    public void SetFrmLoginNull()

    {

    frmLogin = null;

    GC.Collect();

    }

    This method is in FrmMain. But when i run a code analyzer from some tools i have, it's said that i shouldn't explicitly call GC.Collect(); b'cos it had side effect.

    I just need some comment from you all regarding this topic.

    Any Comment?

    Tuesday, February 14, 2006 5:43 PM

Answers

  • Hi compaks,

    As Frederik mentioned, it's not a good idea to call GC.Collect. I think the blog post that Frederik pointed to should help understand why.

    Regarding marking objects for collection - in most cases, you don't even have to set the object to null to make it elligible for garbage collection. If you create an object within a method and your application thread is done executing that method, that object is automatically considered elligible for garbage collection. For class level variables, if you want to let the GC know that the object can be collected, you set the variable to null and it'll be considered elligible for garbage collection.

    However, things get a bit complicated if the class of which you have an instance overrides the Finalize method. If you want more information related to Finalization and Collection, here's an article that gives a pretty good explanation:
    http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

    hope that helps,
    Imran.
    Tuesday, February 14, 2006 8:33 PM

All replies

  • You should not call GC.Collect() yourself, because it has indeed a negative impact on performance.
    I think you should only call it, if you know that you've allocated a lot of memory that is not used / referenced any more at the time you call gc.collect, and, you should also do not call it in a loop or so.

    Rico Mariani has a blog post about it:
    http://blogs.msdn.com/ricom/archive/2003/12/02/40780.aspx
    Tuesday, February 14, 2006 6:44 PM
  •  

    Thanks for the answer, Frederik

    Ok, now one conclusion : We shouldn't call GC.Collect() explicitly.

    But, if we sure that we've allocated a lof of memory, then we're sure we can call GC.Collect();

    From what i had read, we don't know when the Garbage Collector will work. Suppose we need to call GC.Collect(), how to mark that "Ok, this object can be collected, you (GC) can take it"?

    By using " = null" like this :

    frmLogin = null;

    Is this true? i'm not sure about this. Any comment?

    Tuesday, February 14, 2006 8:08 PM
  • Hi compaks,

    As Frederik mentioned, it's not a good idea to call GC.Collect. I think the blog post that Frederik pointed to should help understand why.

    Regarding marking objects for collection - in most cases, you don't even have to set the object to null to make it elligible for garbage collection. If you create an object within a method and your application thread is done executing that method, that object is automatically considered elligible for garbage collection. For class level variables, if you want to let the GC know that the object can be collected, you set the variable to null and it'll be considered elligible for garbage collection.

    However, things get a bit complicated if the class of which you have an instance overrides the Finalize method. If you want more information related to Finalization and Collection, here's an article that gives a pretty good explanation:
    http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

    hope that helps,
    Imran.
    Tuesday, February 14, 2006 8:33 PM
  • Hi Imra

    Just a little correction:

    "If you create an object within a method and your application thread is done executing that method, that object is automatically considered elligible for garbage collection"

    The garbage collector only runs on the heap. The variables created on your method are not stored on the heap, they are stored on the stack. As soon as your method finishes the stack pointers moves.

    Also for the rest of the thread, the GC.Collect() calls a partial collection, not a full collection. This means that not all the objects will be collected. Full collection is a very expensive operations and rarelly happens.

    Regards

    Tuesday, February 21, 2006 10:57 AM
  •  Salvador wrote:

    "If you create an object within a method and your application thread is done executing that method, that object is automatically considered elligible for garbage collection"

    The garbage collector only runs on the heap. The variables created on your method are not stored on the heap, they are stored on the stack. As soon as your method finishes the stack pointers moves.

    Salvador: Although the variables (or references) to objects are stored on the stack, the object instances themselves are created on the heap.

    Imran: The Garbage Collector does not have to wait until a method ends before objects created within that method are available for collection (that is, as long as you compile with optimizations on).

    Regards

    David

    Wednesday, June 07, 2006 7:27 AM
    Moderator
  • Salvador,

    Also for the rest of the thread, the GC.Collect() calls a partial collection, not a full collection. This means that not all the objects will be collected. Full collection is a very expensive operations and rarelly happens.

    Actually, GC.Collect() does cause a full collection (full collection meaning generations 0-2 and ther large object heap, as well as triggering the finalizer thread). You are correct that full collections are expensive perf-wise, and should be minimized.

    Thanks
    -Chris

    Wednesday, June 07, 2006 10:14 PM