|
| 1 | +# GC observability and the LOH — what is worth adopting, and what stays runtime-only |
| 2 | + |
| 3 | +Working note. **Trigger:** the *GCExperiment* write-up ("Making .NET GC |
| 4 | +behaviour observable"), proposed as material to fold into our runtime layer. |
| 5 | +This note records what is genuinely new in it, corrects one number our own docs |
| 6 | +imply, and pins the boundary that must not move. |
| 7 | + |
| 8 | +Companion to [`llvm-codegen-feasibility.md`](llvm-codegen-feasibility.md) (the |
| 9 | +other half of the same discussion) and to |
| 10 | +[P-034](../proposals/P-034-runtime-lifetime-guard.md), whose "ClrMD-free |
| 11 | +complement" argument this reuses verbatim. |
| 12 | + |
| 13 | +## The one factual correction worth taking |
| 14 | + |
| 15 | +Our docs treat the Large Object Heap threshold as **85,000 bytes** of payload. |
| 16 | +That is the documented **default** constant, but it is not the predicate. Two |
| 17 | +things are wrong with the folklore reading: |
| 18 | + |
| 19 | +1. **The threshold is configurable.** `System.GC.LOHThreshold` |
| 20 | + (`runtimeconfig.json`) and `DOTNET_GCLOHThreshold` (environment, hex) raise |
| 21 | + it, so 85,000 describes a default configuration, not a law. |
| 22 | +2. **The comparison is against full object size**, not payload — payload + |
| 23 | + object header + method-table pointer + the array length field + alignment |
| 24 | + padding — so an array whose *payload* sits comfortably under the limit can |
| 25 | + still land on the LOH. |
| 26 | + |
| 27 | +Worked on the environment this was checked against (**.NET CoreCLR, x64, |
| 28 | +default GC configuration**), where that overhead is 24 bytes: |
| 29 | + |
| 30 | +> `byte[84_999]` → 85,023 bytes → rounds to 85,024 → **allocated on the LOH.** |
| 31 | +
|
| 32 | +The header size is a **platform and runtime detail**, not a portable constant — |
| 33 | +object layout and alignment differ by architecture and runtime version, so the |
| 34 | +24 above should not be copied into another context as a given. |
| 35 | + |
| 36 | +The practical consequence, and the reason it is worth writing down: the familiar |
| 37 | +"keep buffers under 85,000" folklore is **off by roughly one header**, and a |
| 38 | +pool sized to exactly `85_000 - 1` is on the wrong heap under the default |
| 39 | +configuration. The portable advice is not a corrected arithmetic constant — it |
| 40 | +is **measure on your target runtime**, since both the threshold and the overhead |
| 41 | +can move. |
| 42 | + |
| 43 | +Where this touches our docs: [`ROADMAP.md`](../ROADMAP.md) and |
| 44 | +[`Plan.md`](../../Plan.md) both list LOH fragmentation in the detectability |
| 45 | +matrix. Neither states a threshold, so **neither is wrong** — but if a threshold |
| 46 | +is ever quoted in a rule, a diagnostic message, or a talk, it must be the |
| 47 | +full-object-size version, not the payload one. |
| 48 | + |
| 49 | +## What GCExperiment is, and what is actually adoptable |
| 50 | + |
| 51 | +Four self-contained experiments (LOH placement; generation promotion; |
| 52 | +allocation pressure; LOH fragmentation) built on ordinary public APIs — |
| 53 | +`GC.GetGeneration`, `GC.Collect` with forced modes, |
| 54 | +`GC.WaitForPendingFinalizers`, `GCSettings.LargeObjectHeapCompactionMode`, |
| 55 | +plus small `GCMonitor`/`GCInfo` helpers for snapshots and size estimation. It |
| 56 | +also flags a real measurement trap: without `GC.KeepAlive`, the JIT can shorten |
| 57 | +an object's lifetime and skew the result. |
| 58 | + |
| 59 | +The adoptable idea is **not** the GC content, which is well-trodden. It is the |
| 60 | +**delivery shape**, and it is the same shape P-034 already argued for from a |
| 61 | +different direction: |
| 62 | + |
| 63 | +> a lifetime/GC observation that runs in an ordinary `dotnet test`, on any OS, |
| 64 | +> with no PerfView, no ETW, no Windows stand, and no ClrMD heap walk. |
| 65 | +
|
| 66 | +Today our runtime layer ([`Plan.md`](../../Plan.md) §2, category 12) routes |
| 67 | +*everything* GC-shaped to PerfView + ETW. That is correct for **evidence** and |
| 68 | +badly overweight for **orientation** — it means no GC fact can be established in |
| 69 | +CI, on Linux, or in a unit test. A `GC.GetGCMemoryInfo` / |
| 70 | +`GC.CollectionCount(n)` snapshot around a scenario costs nothing and needs no |
| 71 | +stand — with one caveat that has to travel with it: **both are process-wide, not |
| 72 | +scenario-scoped.** `CollectionCount(n)` counts every collection since process |
| 73 | +start (and a higher-generation collection bumps the lower ones too); |
| 74 | +`GetGCMemoryInfo()` describes the *last* collection and returns an all-zero |
| 75 | +struct with `Index == 0` when none of the requested kind has happened. Parallel |
| 76 | +tests or background GC move both from outside the scenario. So a probe used as |
| 77 | +an assertion has to record counts at the scenario boundary and compare |
| 78 | +`GetGCMemoryInfo()` only when the GC index matches — otherwise it is a heuristic |
| 79 | +wearing an assertion's clothes. |
| 80 | + |
| 81 | +So: a cheap in-test GC probe is a reasonable sibling to P-034's disposal |
| 82 | +quarantine, under the same honest caveat P-034 already states — it proves *what |
| 83 | +the counters did during this test*, bounded by test coverage, and it is blind to |
| 84 | +*why*. It is a debug assertion, not an auditor. |
| 85 | + |
| 86 | +## The boundary that does not move |
| 87 | + |
| 88 | +**None of this makes LOH fragmentation statically detectable.** The matrix rows |
| 89 | +stay exactly as written: |
| 90 | + |
| 91 | +- `ROADMAP.md`: LOH fragmentation → ❌ **impossible** (depends on runtime data |
| 92 | + volume / GC timing) |
| 93 | +- `Plan.md` category 12: heavy dictionaries / LOH fragmentation / Gen2 bloat → |
| 94 | + **impossible** static → **RUNTIME** |
| 95 | + |
| 96 | +A GC probe is a **runtime witness**, and it lives on the runtime side of the |
| 97 | +line the detectability matrix draws. The matrix exists specifically to stop |
| 98 | +runtime-shaped problems from being hung on a static checker |
| 99 | +([`Plan.md`](../../Plan.md) §1: it "*forbids*" exactly that, killing a class of |
| 100 | +false positives in advance). Making GC behaviour cheaper to *observe* is not an |
| 101 | +argument for making it *inferred*, and this note must not be cited as one. |
| 102 | + |
| 103 | +The one thing genuinely on the static side is unchanged and already ours: the |
| 104 | +`ArrayPool`/`Span` misuse family (`POOL001`–`003`, P-007). Rent/return balance |
| 105 | +is structurally visible; fragmentation is not. |
| 106 | + |
| 107 | +## Status |
| 108 | + |
| 109 | +**Recorded, not scheduled.** Per the `research-landscape-2026.md` discipline, |
| 110 | +notes record and the ROADMAP schedules. Concretely, if anything is ever picked |
| 111 | +up from here: |
| 112 | + |
| 113 | +1. **The LOH threshold correction** — free, and the only item with a |
| 114 | + correctness argument behind it. Applies wherever a number gets quoted. |
| 115 | +2. **A GC-counter probe as a P-034 sibling** — small, attaches to the open |
| 116 | + question P-034 already asks (a new `Own.Diagnostics` package vs living in |
| 117 | + OwnAudit's `runtime/`). Do not file it separately; it is the same decision. |
| 118 | +3. **Nothing else.** The generation-promotion and allocation-pressure |
| 119 | + experiments are educational rather than diagnostic, and we do not need to |
| 120 | + re-derive published GC behaviour to ship a checker. |
0 commit comments