I'm analyzing the outcome of some simple C# code, in terms of GC performance. I've used PerfView with GC Collect Only
ticked to gather meaningful info.
Consider the following relevant output from PerfView's GCStats, particularly GC #1106 and #1108:
Both #1106 and #1108 are listed as gen1 Non-concurrent GCs. Both have a reason of AllocLarge
.
Now consider the associated Condemned Reasons table:
In a blogpost by Maoni Stephens, the owner for the .NET GC, we know that once a GC is started, it can act against a higher gen, as follows:
Now after the GC starts, we then decide what generation we would actually collect. It might stay as a gen0 GC, or get escalated to a gen1 or even a gen2 GC – this is something we decide as one of the very first things we do in a GC. And factors that would cause us to escalate to a high generation GC are what we called “condemned reasons” (so for a GC there’s only one trigger reason but can be multiple condemned reasons).
Based on the Condemned Reasons table, it turns out both #1106 and #1108 "elevate" themselves from gen0 GCs to gen2 GCs due to both breaking the threshold for allocations in gen2.
But how come:
1) Both #1106 and #1108 start off as gen0 GCs, get eventually "escalated" to gen2, but show up in GCStats as gen1 GCs ? Please note that this is not some PerfView issue, as the detailed ETW events show the same exact data in terms of generations.
2) Both #1106 and #1108 get registered as gen1 GCs, but with a reason of AllocLarge
? AllocLarge
refers to the large object allocation threshold exceeded, and the LOH can only be processed as part of gen2 collections.
The data above was captured on a .NET Framework 4.7.2 running on a Windows 10 x64. I'm assuming though that the concepts behind the GC decisions would be largely the same for other, relatively recent versions of .NET Framework, or even .NET Core, so this should make little difference to what's being asked here.
I've also not burdened the question with the actual code, since what's causing the GCs to occur in the form explained above is not triggered by any special instruction, but by the inner decisions the GC takes.
Update 1: I've provided more data to the initial question. I've also specifically looked at and couldn't find any other Microsoft-Windows-DotNETRuntime*
ETW event around GCs #1106 and #1108 referenced that could shed more light on either question.
Update 2: I've changed the name of the question, since there are no gen2 GCs occurring after all. The CLR is performing only gen1 GCs for #1106 and #1108, as the column for the gen2 survival rate states NaN
, which implies no gen2 GC has run.
Looking over the .NET Core CLR source code (for inspiration as what might happen in .NET Framework), the function that does the actual GC work gc_heap::gc1
reads early on the condemned generation info (below), and provided this is a non-concurrent GC (similar to our #1106 and #1108) it proceeds with the mark phase against the respective generation.
As for the chosen condemned generation, within
gc_heap::generation_to_condemn
the check for the highest gen budget allocations exceeded is done before the check for low ephemeral segment, and the latter doesn't overwrite the decision, but simply selects the max between whatever generation was deemed condemned before and its own selection. Nonetheless we know the final generation is chosen correctly, as the number of the condemned (final) generation is 2
in GCStats. But why isn't the GC acting on this decision ?