Senior · Design · LOH allocation, fragmentation and compaction evidence

How would you evaluate large-buffer reuse without hiding retained memory?

Think through your answer before opening the explanation.

Memory & GC

Reveal answer and reasoning

Compare reuse, streaming and bounded chunking against peak concurrency. A pool can reduce repeated large allocations while retaining a high-water mark indefinitely. Streaming can reduce payload residency if downstream code does not immediately materialize the whole stream again. Chunking can increase metadata and copies. The right experiment measures throughput, allocation rate, retained capacity and tail latency under the same consumer behavior.

For an interview, explain why a full GC cannot fix a cache that intentionally retains every large response. Then discuss a workload with many dead variable-sized arrays: layout and allocation shape may matter after ownership is corrected. A strong answer names the measured symptom first, chooses an intervention second and specifies a repeatable comparison. It does not prescribe periodic CompactOnce calls as a universal cure.

Change the portable chunk size while preserving the total logical payload. Compare number of objects, list entries and payload bytes, without guessing the resulting physical memory from payload alone. Add a consumer that requires one contiguous buffer and account for the final concatenation copy. The optimization may simply move the large allocation later.

In the region lab, remove the large-array root and run Gen 0 first. The LOH object remains outside that collection's reclamation set. Run Gen 2 and inspect the difference. Finish by writing separate definitions for live payload, fragmentation, committed GC bytes and resident process memory, then identify which metric would answer each question in a real memory incident.

What a strong answer covers

  • The usual CoreCLR LOH threshold refers to total object size.
  • Free fragments, live bytes and committed memory measure different things.
  • Compaction changes layout, not the application ownership contract.

Common pitfall

Comparing an array payload length directly with the LOH object-size threshold, or equating free gaps with RSS.

Follow-up questions

  1. Why can chunking still lead to a final large concatenation allocation?
  2. Which GC index and fragmentation observations would you compare?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions