FreeAllUnusedJunk() ? any such method?

I've got a program that runs through a loop, and during this loop, consumes a lot of RAM. of course, .NET is smart, and auto-disposes.. so the RAM usage goes something like.. 40MB, 80MB, 115MB, 230MB, 307MB, then it drops back down to something small again.

The thing is, when this loop is over with.. the process could be eating up 40MB RAM, or 300MB. Is there some kind of global method I can call that basically frees up ANYthing that's not in use



Answer this question

FreeAllUnusedJunk() ? any such method?

  • joshua926

    So why wouldn't you recommend calling the gc.Collect() I want to use it in my application.
  • lawhornk

    its bad practice and should leave the GC to do its own thing without interferring with it otherwise sometimes you will have undesirable effects

  • OniShiro

    still wouldn't recommend calling GC yourself.

  • Williamq

    Definitely will be different behavior. GC works really tight with free memory. It will start more often when free memory is really low.
    I suggest to test your code with FxCop, maybe will help you find some better cleanup solution.

  • pierreg

    Thanks. I suppose I'll just leave it to my app to garbage collect. Probably the only reason it saw fit to cap off at 300MB'ish is because I have 2GB RAM and plenty available. I'm sure on a machine with only 512MB RAM or so, it would've done the garbage collect a lot sooner. (right I hope) :)

  • Mateus1223

    best practice would be to enclose in the using {} statement, the disposible objects which will try to be automatically freed up/disposed of but no guarentee it will at the time of the call. Example, streams:

    using (StreamReader sr = new StreamReader(file))

    { ... do stuff ... }

     

     

     



  • Marie-Thérèse

    Hi,

    Well, NO
    there is no such method which takes the memory count from where it began. Even I was wondering why .NET consumes memory and never frees it.
    Take a form for example and put a button on it. Now, on this button click do something like this.

    using(TextBox tb = new TextBox()) { }
    What we expect from this code is that on each click the memory goes up and comes down to some level, but it dosen't. You'll notice that the memory keeps increasing, (maybe in small amount but still) and never gets to the point where it started. Well, this is because the Textbox() object is Disposed but not freed. Acceptable, yet it should show some sign of freeing the objects if I click 100 times.

    We cannot expect a +20KB on new object goes back to -20KB when an object is Disposed(). Whatever +8KB(or so) is left keeps increasing the used memory count. But once it reaches the pressure limit, GC frees it. You can also increse the memory pressure manuall by using AddMemoryPressure call to GC.

    regards


  • chakravarthy_b

    Yes, there is.. GC.Collect() will run a garbage collection for you. The (good) advice is not to run it yourself - instead, let the GC run when memory pressure and system availability trigger an automatic collection - this will let the GC tune itself. If your app is using 300MB, but no one else needs it, who cares!

    Of course, there's always a time when you (the developer) know better than the GC... so GC.Collect is there for you. You can specify a generation or let it do a full collection. But remember - people used to think they could optimize better than a good compiler, and now those optimizations just get in the way. The GC is fair now and will get better and better, so keep that in mind :)


  • FreeAllUnusedJunk() ? any such method?