Memory & GC
Reveal answer and reasoning
GC.GetAllocatedBytesForCurrentThread reports allocations attributed to the current thread. It does not measure native allocations, the live heap, or every allocation caused by a request that resumes on a different thread after await. A before/after subtraction across arbitrary asynchronous work can therefore mix unrelated activity and miss some of the operation's allocations. Keep a thread-local experiment synchronous or use an appropriate process-wide measurement and controlled workload.
Warmup, JIT compilation, lazy initialization, logging and result formatting can contaminate the measured interval. Print after taking the final counter. Preserve enough work that optimization does not eliminate the behavior under investigation, but recognize that adding an escape or retaining all results changes the workload. A benchmark that stores every result measures a different lifetime pattern from a production path that discards each result immediately.
Boxing a value produces a boxed representation with independent value contents. The language-level identity and mutation behavior can be demonstrated without claiming an exact allocation count for every optimized program. Compilers and runtimes can remove or transform work when observable behavior permits, and different generic, interface and formatting paths can introduce additional operations. Investigate the compiled representative case rather than memorizing that one source construct always costs one fixed number of bytes.
An object array that stores boxes forces those values to remain available through that owner. A generic collection of a value type can avoid those particular boxes while still allocating its own backing array and resizing storage. A string formatting change can alter both temporary objects and final output. Compare complete behavior and verify equivalent results before attributing an improvement solely to a chosen syntax change.
What a strong answer covers
- Allocated bytes accumulate even when objects die quickly.
- Per-thread allocation counters cannot span arbitrary async thread switches.
- JIT warmup, measurement allocations and escape behavior can change results.
Common pitfall
Using a per-thread counter across arbitrary awaits, or calling cumulative allocation a live-heap measurement.
Follow-up questions
- What allocations belong outside the timed interval?
- How would a no-retention variant change live bytes without eliminating allocation?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.