How to free up memory in C# apps?

hi..
In my application so many variables and XML files are used ..
eventhough after finishing off the work with the file, im closing that.
It works fine with few number of files but when i uses more than 50 files
the application crashes...
So. is there any way to resolve this by clearing the memory.
need help !!!


Answer this question

How to free up memory in C# apps?

  • Little John

    I still disagree that calling GC.Collect() is a bad idea. Yes, the GC is highly optimised, but still things come at a cost. I've read some articles on MSDN, and it is clear that the GC will remain inactive in the background, and eventually kick in when things are beginning to get too messy. When you dispose of an object in becomes unavailable to you, but the memory is still allocated until the next GC cycle. So, coming back to my game example, one map might contain, say, some 200 objects, then a new map is loaded with another 200 objects. All the old objects are disposed of, and assuming the GC may not kick in yet, there is a high probability that another object could become spawned in the game, and the GC would find out that the limit was just reached and has to garbage collect right there and then.
    GC.Collect() is perhaps a nice way to avoid 'the embarrasing pause'. It should not be used often though, but the GC can't possibly know when is a good time to garbage collect if your program is a constant flow in the background, but there are moments when the user do not expect any immediate response.

  • DBAcrobat

    Wouldnat an enforced garbage collect be wise to do before a period of intensive processing that'd might require smooth user response I.e. a game written in C# would perhaps call GC.Collect() after a map has been loaded, right before the gameplay starts....
  • UncleRedZ

    You need to make sure you call Dispose on any variable that has that method, before it goes out of scope. But, the garbage collector should ensure that your app never crashes from lack of memory.



  • Rameshb106

    No. You'd call Dispose on the objects in the map, which is what you should do, and then there would not be a problem. The garbage collector is optimised, you should let it do it's job.



  • Fat Dragon

    I was using an Image object, and reading/updating EXIF properties on the object. I processed a batch of images (eg. JPEG), each around 3-4Mb in size, one after the other, and used an image stream to open the image. Sometimes I loaded the whole image, sometimes I just opened it in a way that allowed me to modify the EXIF data without loading the whole image. After performing my changes, and re-writing the image (sometimes after making an internal rotation back/forward of 90deg. to avoid re-encoding the compressed image), I disposed of, both the image variable, and the file stream. For some reason, I found after trying many different ways of opening/processing the files, that unless I called GC.Collect(), the memory would run out after processing a couple of hundred files (eg. after processing a batch of files, the memory might be released, but not immediately).


  • MSMC

    "Should" I wish it did.

    You can force the garbage collector to do an immediate clean-up by calling it's collect method:
    GC.Collect()
    I was working with an image program before, which loaded images as bitmaps, and cycled over many images in a directory, processing them, saving them, and then moving on (and yes I called .Dispose() after finishing with my imaging objects). I found that if I didn't call the Collect() method, it'd just be thrashing for ages (ie virtual memory paging).

    Have a look at task manager's Performance tab, or use Performance Monitor to have a look at the effects of calling this method.


  • CarlDemelo

    GC.Collect is almost always a bad idea. I write image processing code in C# daily and I've never had memory problems.



  • Michael Junkin

    well in theory yes... but not always it is possible. i have a problem with CLR and GC which i describe here: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=356533&SiteID=1
    any ideas


  • vidman

    i'm also working alot with bitmaps and never had problems with disposing. note, that task manager does not tell you the whole truth. sometimes even if you dispose an object the memory is not always returned to the system at once - sometimes the CLR holds it for ages until it is really needed somewhere else. Forcing GC.Collect() is rather not a good idea unless you know exactly what you are doing. Putting the GC.Collect() line in a wrong place may kill the performance of your app.

  • How to free up memory in C# apps?