none
Memory Usage RRS feed

  • Question

  • Why is it when you click on a forum with just one button that does nothing, the memory usage keeps adding up.  Any program in c# so far I seen keeps adding o the memory usage.  Ctrl alt del to see the mem usage of any program. 

    I am making a game and if it keeps increasing like that it will eventually make the computer not be able to handle that much usage.

    Any ideas?  I dont think it is memory leaks in my programming because it does the same on a basic applacation with a single button on the form.

    Sunday, January 14, 2007 12:41 AM

Answers

  • In C# memory usage is cleaned up by the Garbage Collector,  this will only execute when it thinks it is necessary i.e. when there is some kind of memory pressure to use more memory and unused memory needs to be reclaimed.  The Garbage Collector will only clean up memory used by objects that are no longer reachable by any roots in your application (roots are things like local variables, static variables, processor register values), so it finds all the roots then all of the objects the roots refer to and all the objects those objects refer to and so on, all objects that do not have any references from a root are eligable to be cleaned up.

    So you may create many objects in your program and see that memory usage keeps going up and up and up, but as long as you do not hold references to old objects at some point to GC will kick in and clean up the unused objects, however this is not deterministic since the GC will dcide when this happens.  You can force it to happen but that is not normally necessary, by calling GC.Collect();

    If you want to see exactly where the memory in our program is being used then look at the "CLR Profiler" tool by Microsoft, that will show you all of the memory being used and by which types of objects.

     

    Mark.

    Sunday, January 14, 2007 12:50 AM

All replies

  • In C# memory usage is cleaned up by the Garbage Collector,  this will only execute when it thinks it is necessary i.e. when there is some kind of memory pressure to use more memory and unused memory needs to be reclaimed.  The Garbage Collector will only clean up memory used by objects that are no longer reachable by any roots in your application (roots are things like local variables, static variables, processor register values), so it finds all the roots then all of the objects the roots refer to and all the objects those objects refer to and so on, all objects that do not have any references from a root are eligable to be cleaned up.

    So you may create many objects in your program and see that memory usage keeps going up and up and up, but as long as you do not hold references to old objects at some point to GC will kick in and clean up the unused objects, however this is not deterministic since the GC will dcide when this happens.  You can force it to happen but that is not normally necessary, by calling GC.Collect();

    If you want to see exactly where the memory in our program is being used then look at the "CLR Profiler" tool by Microsoft, that will show you all of the memory being used and by which types of objects.

     

    Mark.

    Sunday, January 14, 2007 12:50 AM
  • Sunday, January 14, 2007 12:52 AM
  • Thank you very much I was a little worried there for a minute.  :D I love this forum.
    Sunday, January 14, 2007 12:55 AM