C# Memory Management

Started by
6 comments, last by Nypyren 18 years, 5 months ago
I'm writing an app in C# for the first time and I'm finding it pretty easy since it is essentially the same as C++. However I have some a question about garbage collection. Say I do something like this... ArrayList arr = new ArrayList(); arr = new ArrayList(); Is there a way to force garbage collection on the original allocation? I'm just skeptical about the whole garbage collection working. I have memory getting newed all over but theres no delete keyword and most classes I find do not inherit from IDisposable.
Advertisement
You can force a garbage collection with GC.Collect()

This will clean up all objects that are not referenced by other objects (i.e. the first Arraylist you allocated).

BUT, The garbage collector does work (all by itself, no delete keyword) and is very good, and it still works pretty well even if you are doing something horribly wrong. It is normally better to just let it do its thing and collect when it needs to. Forcing garbage collections can cause a lot of objects to be promoted to Gen 1 or Gen 2 when they really shouldn't.

MSDN has a few white papers and a lot more information about it, as well as GC performance consideration (generations, object lifetimes, etc.).
Be careful with this. Driving the GC with GC collect has the side effect of moving memory that can not be collected into one of the three upper stages of the GC. This memory is not checked as often to be flaged for deletion, and so you could actually end up using up more memory by driving the GC with the collect call.
Instead of telling the garbage collector to remove all unreferenced objects, you could call the dispose() function on the object. If I remember correctly, the dispose function will notify the garbage collector that the object is ready to be removed and it should take care of it the next time a collection happens.

Edit: I completely skipped that last sentence in your post. So, I guess you couldn't call dispose() on the object. My bad.
MSDN Article on the .Net GC

More garbage collector info

Important comments about forcing the garbage collection yourself

Quote:MSDN
You should also be careful not to place code that calls GC.Collect at a point in your program where users could call it frequently. This would defeat the optimizing engine in the garbage collector, which determines the best time to run a garbage collection.
Quote:Original post by Dorvo
Instead of telling the garbage collector to remove all unreferenced objects, you could call the dispose() function on the object. If I remember correctly, the dispose function will notify the garbage collector that the object is ready to be removed and it should take care of it the next time a collection happens.

Edit: I completely skipped that last sentence in your post. So, I guess you couldn't call dispose() on the object. My bad.
Actually, that is incorrect. Dispose is simply a way to release resources early (typically unmanaged resources), and Dispose usually only notifies the GC that the finalizer for the object should not be called. AFAIK there's no way to indicate an object should be garbage-collected aside from removing it from the object graph, which would mean setting a reference to null somewhere.

As a general rule of thumb, if your code includes "GC.Collect()", you're doing it wrong. Once a managed object (e.g. an ArrayList) no longer has any valid references to it (or is set to null), your responsibility for cleaning up is done.

In the example you give, the first ArrayList will be garbage-collected the next time the GC runs (it's actually somewhat more complicated than that because of the multi-stage generation cycle, but I digress). The second one would be collected if it's no longer reachable from your program (for instance, if you set arr = null).
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
The situation that MSDN says "might" be a good time to GC.Collect() is at a point in code where you KNOW that you've just stopped using a very large number of objects. (such as cleaning up after finishing compiling something, if you're writing a compiler) Any other time will not be a good idea.

This topic is closed to new replies.

Advertisement