Garbage Collection after calling and returning from 2nd form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TDHall
    New Member
    • Jul 2008
    • 6

    #1

    Garbage Collection after calling and returning from 2nd form

    Hi,
    Just a quick question - what should I do for garbage collection after calling and returning from a class using a 2nd form? From what I have read the app will take care of it for me, but....

    Any help appreciated!!

    Regards,

    TD
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    For the most part, it will. Memory will get flagged to be garbage collected if there's no longer a reference to it, so as long as nothing in referencing any of your 2nd Form's memory, it will get cleaned up when the garbage collector decides it's an appropriate time.

    Unless you are in a low memory environment or that 2nd form allocates a huge amount of memory and you're worried about your main form (or another form) not having enough after that 2nd form closes, you're probably fine to not worry about it. Let the garbage collector do it's job, supposedly it does it fairly well on a windows environment.

    If you absolutely do need to clean up that memory, you can call GC.Collect() after your form closes. This can be a rather expensive operation though because not only will it do garbage collection on your own program, it will do it on any other managed program currently running. The result is you can inadvertently tie up your processor for an unknown period of time (depending on how much memory needs to be garbage collected). Additionally, I believe it will also interrupt the processing of those other managed programs.

    As I've understood it, you shouldn't ever call GC.Collect()... but sometimes you have to :D There are also properties you can set on the garbage collector to increase the frequency of collection, or various other conditions for garbage collection. Have a read about the GC class...

    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

    Comment

    • TDHall
      New Member
      • Jul 2008
      • 6

      #3
      Thanks for the response. You pretty much confirmed what I thought, but it's nice to get some backup confirmation.

      Regards,
      TD

      Comment

      Working...