Writing Allocation Free Code in C#
summary
Shows concrete ways to cut memory traffic: use Span<T> and ref struct to slice strings and arrays without copies, use ref returns/refs and in parameters to avoid copies, prefer ValueTask where tasks allocate, and beware hidden allocators like params arrays, boxing, closures, yield and async state machines. Demo uses DotMemoryUnit and asserts zero allocations when slicing into a span. Practical claim: less allocation reduces GC pauses and smooths latency; tools and runtime support determine whether each technique actually avoids an allocation.
AI-written summary · about
chapters (22)
- 0:00Introduction
- 3:57Using less memory
- 5:02Know your platform
- 6:56Reduce your allocations
- 9:33Reference types and value types
- 12:28Heap vs stack
- 15:39Closures
- 19:22Reference Semantics
- 22:36Ref Return
- 24:33Ref Local
- 25:38Example Code
- 28:28In Parameters
- 29:38Defensive Copy
- 32:21Ref ReadOnly
- 33:50ReadOnly struct
- 34:42Ref struct
- 35:46Span
- 37:05System Memory
- 37:58Performance
- 39:06Thread Safety
- 40:23Demo
- 42:02Useful links
related talks
01h 00mWriting Allocation Free Code in C#
01h 01mTurbocharged: Writing High-Performance C# and .NET Code
56mChange your habits: Modern techniques for modern C#
53mSafety first! Low-level C# without the unsafe keyword
57mWhat's Coming to C#?
56mC# 8 and Beyond
01h 06mThe State of C# - What Have I Missed?
49mEngineering & Technical Highlight: Let's Do Some Crimes. The wonders of abusing C# generics.
