To write high-performance C#, you must understand how the Common Language Runtime (CLR) manages memory and how the Garbage Collector (GC) decides when to clean it up.
The **Stack** is for short-lived data (Local variables). It is fast and self-cleaning. The **Heap** is for long-lived data (Objects). It is slower and requires the GC to manage it. **Architect Tip:** High-performance code tries to keep as much data on the Stack as possible to avoid 'GC Pressure'.
Objects larger than 85,000 bytes go directly to the LOH. The LOH is NOT compacted by default, which can lead to memory fragmentation. Be careful when working with massive byte arrays or strings!
Q: "What is a 'Stop-the-World' garbage collection?"
Architect Answer: "A Stop-the-World GC (usually a Gen 2 collection) pauses all application threads so the GC can safely move objects and update references. For high-throughput apps, this causes 'Latency Spikes'. We combat this by using **Server GC mode** and **Concurrent GC**, which allow the GC to run on its own dedicated threads, minimizing the pause time for the actual application logic."