TechEd - Garbage Collection - How to Make it Work for You
A very interesting session about the garbage collector and how to make it work more efficient.
Let's first recapture some stuff that will be coming the next version of the CLR. At this moment, you only have the option to call GC.Collect to force the garbage collector to do it's work. In the next version, you will be able to tell the GC not to collect, for example when you're collecting large amounts of data. Or maybe you want to tell the GC that you've released all your stuff and that you think it might be good time to collect, but have the GC decide if that's really the case.
The interesting part of this session was how you can investigate how the garbage collector is working if you run into out-of-memory problems. The first thing to look at is the GC performance counters. You should use a very high sampling rate, for example every second. Also keep in mind that most performance counters are only updated at the end of a sampling session. The ones to look for are, in corresponding order:
- # bytes allocated/sec
- # pinned objects
- # of gen0/1/2 collections
- Promoted memory from gen0/1
- Gen2 heap size
- Promoted finalization memory.
Another tip from this session, one I also blogged about before, is to always call the Dispose (or Close) method on an object if an object exposes that method.
The recap of this session:
- Learn how to read performance counters.
- Understand that there are several factors to memory issues in an application
- Validate the assumptions you make.
- The GC can do a lot for you, but the application design greatly influences how the GC performs.
- Read the "Investigating Memory Issues" article in the November issue of MSDN magazine.