C# without the GC: memory, Span and zero allocation
11 steps · about 3 h 17 min · Intermediate
- Fundamentals of garbage collection - .NET (doc, Microsoft Learn, 25 min)
Start with the official account of what the GC does, so you know what you are trying to avoid.
- How .NET Garbage Collector works (and when you should care) (article, elmah.io blog, 12 min)
A plain-language second explanation of generations and when GC cost actually matters.
- Garbage Collection Internals in .NET: How Memory Management Really Works (article, C# Corner, 15 min)
Adds the heap layout, allocation pointer, roots and background GC behind the pauses.
- Workstation vs. server garbage collection (GC) - .NET (doc, Microsoft Learn, 10 min)
Shows the GC modes you can tune before you decide to bypass the GC.
- Memory- and span-related types (doc, Microsoft Learn, 15 min)
Introduces Span<T>, Memory<T> and pools, the building blocks of allocation-free code.
- stackalloc expression - Allocate variable storage on the stack instead of the heap - C# reference (reference, Microsoft Learn, 15 min)
Teaches the first real GC-free allocation: stack memory that is freed automatically on return.
- Pointers in C# and Memory Safety: Span vs. C# 16 unsafe (article, NDepend Blog, 20 min)
Explains when raw pointers are needed and how Span keeps the same speed in safe code.
- C# Span<T> Tutorial: Zero-Allocation Performance Guide (tutorial, CSharp-Coder, 25 min)
Hands-on zero-allocation examples that also cover pitfalls such as stackalloc in loops and returning spans.
- Memory<T> and Span<T> usage guidelines - .NET (doc, Microsoft Learn, 25 min)
Gives the ownership and lifetime rules you need to keep spans and buffers correct.
- ArrayPool and stackalloc — Avoiding Heap Allocations (article, StackLesson, 20 min)
Applies the rent-and-return pattern for large buffers that are too big for the stack.
- Bending .NET: How to Stack-Allocate Reference Types in C# (article, DEV Community (maximiliysiss), 15 min)
An advanced look at hiding whole objects from the GC, and why it is dangerous.