Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent .NET Garbage collection for short period of time

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;

  1. Is it possible to briefly pause Garbage Collection for the entire program?
  2. 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?
  3. 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.

like image 396
drobertson Avatar asked May 15 '11 01:05

drobertson


People also ask

How can we reduce long garbage collection time?

There are two commonly used methods to reduce GC pause time: Reducing suspension time by adjusting the mark-and-sweep algorithm. Limiting the number of objects that need to be marked.

Does .NET have automatic garbage collection?

Automatic memory management is made possible by Garbage Collection in . NET Framework. When a class object is created at runtime, certain memory space is allocated to it in the heap memory.

What is pause time in garbage collection?

A garbage collection pause, also known as a stop-the-world event, happens when a region of memory is full and the JVM requires space to continue. During a pause all operations are suspended. Because a pause affects networking, the node can appear as down to other nodes in the cluster.

How often does NET garbage collector run?

NET Garbage Collector (GC) runs, disposing of objects which are no longer needed and moving everything else up to Gen 1. If Gen 1 becomes full the GC runs again, but also moves objects in Gen 1 up to Gen 2. A full GC run happens when Gen 2 becomes full.


1 Answers

.NET 4.6 added two new methods: GC.TryStartNoGCRegion and GC.EndNoGCRegion just for this.

like image 125
xanatos Avatar answered Sep 24 '22 02:09

xanatos