I have a high performance application that is handling a very large amount of data. It is receiving, analysing and discarding enormous amounts of information over very short periods of time. This causes a fair amount of object churn that I am currently trying to optimize, but it also causes a secondary problem. When Garbage Collection kicks in it can cause some long delays as it cleans things up (by long I mean 10s to 100s of milliseconds). 99% of the time this is acceptable, but for brief windows of time about 1-2 minutes long I need to be absolutely sure that Garbage Collection does not cause a delay. I know when these periods of time will occur beforehand and I just need a way to make sure that Garbage collection doesn't happen during this period. The application is written in C# using .NET 4.0 Framework and uses both managed and unmanaged code if that matters.
My questions are;
- Is it possible to briefly pause Garbage Collection for the entire program?
- Is it possible to use System.GC.Collect() to force garbage collection before the window I need free of Garbage Collection and if I do how long will I be Garbage Collection free?
- What advice do people have on minimizing the need for Garbage Collection overall?
Note - this system is fairly complex with lots of different components. I am hoping to avoid going to a approach where I have to implement a custom IDisposable interface on every class of the program.
.NET 4.6 added two new methods:
GC.TryStartNoGCRegion
andGC.EndNoGCRegion
just for this.That will allow you to disable the GC as much as you can. It won't do any large collections of objects until:
GC.Collect()
GCSettings.LatencyMode
to something other thanLowLatency
Please be careful when doing this, because memory usage can climb extremely fast while you're in that
try
block. If the GC is collecting, it's doing it for a reason, and you should only seriously consider this if you have a large amount of memory on your system.In reference to question three, perhaps you can try reusing objects like byte arrays if you're receiving information through filesystem I/O or a network? If you're parsing that information into custom classes, try reusing those too, but I can't give too much good advice without knowing more about what exactly you're doing.
Here are some MSDN articles that can help too:
PrepareConstrainedRegions()
)Note:
GCSettings.LatencyMode = GCLatencyMode.LowLatency
can only be set ifGCSettings.IsServerGC == false
.IsServerGC
can be changed inApp.config
: