Reputation: 2597
I have situation when GC has no time to delete free objects. The code loads a large document into memory and processes it in a loop. If I stop in this loop (in debug mode) or add GC.Collect() the memory usage drops to less then 70 MB.
How do I tune GC? Are there any best practices for tuning GC?
Upvotes: 4
Views: 4635
Reputation: 7908
First you need to explore what is happening inside the garbage collector when your program is running. Usually garbage collector is good at deciding when to remove objects without your intervention. However, it is not error prone. For example, you may be keeping a list of small objects that hold references to large objects that you expect to die soon. To understand the bottlenecks in GC you need to profile GC memory allocation. To do this...
1 ) Search "performance monitor" in windows start
2)Under monitoring tools tab select performance monitor. Clear any counters you already have by "right click-> remove all counters"
3)right click ->add counters
4) Select all the counters under ".NET CLR memory". When you add them make sure you start your program and select the program from the list to your left side. This will make sure your counters are going to report the performance of your program but not anything else
5) Once you have your counters running please read following articles to find the bottle necks in your program.
Garbage collector basics http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
What performance monitor counters tell you http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx http://netrsc.blogspot.com.es/2008/01/net-clr-memory-performance-monitoring.html
Once you are familiar with basics look for following issues 1) Large object heap is fragmenting too quickly
2) Objects you want to die fast end up in generation 2 E.g. mid-life crisis http://blogs.msdn.com/b/ricom/archive/2003/12/04/41281.aspx
3) Total committed bytes keep increasing
4) You spend too much time garbage collecting
5) and many more... (google for GC memory issues)
Since you can't identify which objects live in different generations you can use something like ANST memory profile to see whether you are inadvertently promoting short lived objects into generation 2. The profiler allows you to see who holds references to large objects and peak into different garbage collector storages (e.g who live in generation 2 who live is large object heap). But before doing any advance performance tuning use windows performance monitor to identify whether your bottlenecks are in fact with GC.
Upvotes: 4
Reputation: 273244
The best tuning here is to leave it alone.
But if your process is really blocking the GC all the time (it shouldn't), it could help to call GC.Collect()
after some memory consuming part finishes.
But we need a lot more info here. What type of application, why (do you think) the GC has no time to do it normally?
Upvotes: 2