Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Garbage Collection Latency

I'm profiling memory in a C# application using a memory profiler, dotTrace, and perfmon counters. The one question that I have not been able to answer is: What is the maximum latency that garbage collection causes in my application? I can get an approximate value for this by using the % time in Garbage collection, but is there any way to time individual collections?

like image 927
Patrick Avatar asked Jan 27 '11 20:01

Patrick


People also ask

Is garbage collection slow or fast?

Impacts On Performance If it was, every language would use it. GC is slow, mostly because it needs to pause program execution to collect garbage. Think of it like this — your CPU can only work on one thing at a time. With C++, it's always working on your code, including the bits that delete memory.

Does garbage collection affect performance?

The most common performance problem associated with Java™ relates to the garbage collection mechanism. If the size of the Java heap is too large, the heap must reside outside main memory. This causes increased paging activity, which affects Java performance.

How can I speed up my garbage collection?

Short of avoiding garbage collection altogether, there is only one way to make garbage collection faster: ensure that as few objects as possible are reachable during the garbage collection. The fewer objects that are alive, the less there is to be marked.

Is garbage collection CPU intensive?

CPU usage will be high during a garbage collection. If a significant amount of process time is spent in a garbage collection, the number of collections is too frequent or the collection is lasting too long. An increased allocation rate of objects on the managed heap causes garbage collection to occur more frequently.


1 Answers

It looks like you are asking 2 questions 1- Is there a max latency for the GC? 2- How to time individual GC collection?

For #1, the GC throughput is roughly 200MB/sec/heap. This means the GC is able to collect a heap of size 100MB in 1 second. The perheap notion is because server GC, since we are creating 1 heap per CPU.

So, if you have a huge heap, you can see big latencies. Keep in mind that when GC collect the memory, it does this in an ephemeral way, so Gen0/Gen1 collections are very cheap compared to full GC collections.

In .NET 4.0 we added a feature to minimize the GC latency for client applications, this feature is enabled by default under the GC Concurrent mode. In this mode, we try to collect the heap in a background thread while the application is running. This results in better pause time for client applications

For#2: We have a very powerful tracing in the .net framework called ETW (it is available in Windows, and we take advantage of it in the CLR ). ETW stands for Event Tracing For Windows (. We fire ETW events when a GC is about to start, and when a GC finish. using these ETW events, you can calculate the time spent in each GC.

For more information you can refer to the CLR ETW reference. Also, for a nice managed library that allows you to deal with ETW events, check out TraceEvent

Hope this help. Thanks

like image 192
mfawzymkh Avatar answered Sep 28 '22 09:09

mfawzymkh