The Span<T> and Memory<T> types in .NET are
high-performance, memory-safe abstractions introduced to work with slices of data without allocations or copying. They improve performance especially in
low-level, high-throughput, and memory-sensitive scenarios, such as parsing, serialization, or buffer manipulation.
What Are Span<T> and Memory<T>?
Type
Description
Stack/Heap
Span<T>
A stack-only, non-allocating view over contiguous memory (arrays, strings, etc.)
Stack
Memory<T>
A heap-safe, sliceable memory abstraction that can be stored and used asynchronously
Heap
Performance Benefits
1. Avoids Memory Allocations
Span<T> lets you work with slices of arrays or buffers without copying the data.
Example: Instead of creating a new array or string slice, you just point to part of the original memory.
Reduces GC pressure and heap allocations.
2. Improved Cache Locality
Because Span<T> works on contiguous memory, operations on it tend to have
better CPU cache performance than working with scattered objects.
3. Safe, Zero-Cost Slicing
You can slice Span<T> like this:
Span<int> slice = originalSpan.Slice(2, 5);
This is zero-cost: no new array is created — just a new view.
4. Safe Alternative to Unsafe Code
Span<T> gives C-like pointer performance, but with
type safety and bounds checking.
You avoid using unsafe pointers or fixed blocks for performance-sensitive code.
5. Async Support with Memory<T>
Span<T> can’t be stored or returned from async methods (stack-only).
Memory<T> solves this: it can be stored, passed around, and awaited.
It still supports slicing and efficient access via .Span.
Real-World Scenarios
Scenario
Benefit
String parsing
Avoid string allocations (e.g., use Span<char>)
File/network I/O
Use Memory<byte> buffers without copying
High-speed JSON/XML parsing
Work on byte arrays directly
Buffer pools
Manage memory efficiently in reusable chunks
Limitations
Span<T> can’t be:
Stored in fields of reference types
Captured in async/iterator methods
Memory<T> is more flexible but slightly less performant
Example
Span<byte> buffer = stackalloc byte[256];
var slice = buffer.Slice(0, 128); // no allocation
ReadOnlyMemory<char> memory = "Hello World".AsMemory();
DoAsyncStuff(memory); // safe to pass into async methods
Summary
Feature
Span<T>
Memory<T>
Alloc-free slicing
Yes
Yes
Stack-only (high perf)
Yes
NO
Safe in async
NO
Yes
GC pressure
Low
Low
Replacement for arrays/strings
Yes
No
Span<T> and Memory<T> give you the power of unsafe pointers with the
safety and performance of modern .NET.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
Span<T>andMemory<T>types in .NET are high-performance, memory-safe abstractions introduced to work with slices of data without allocations or copying. They improve performance especially in low-level, high-throughput, and memory-sensitive scenarios, such as parsing, serialization, or buffer manipulation.What Are
Span<T>andMemory<T>?Span<T>Memory<T>Performance Benefits
1. Avoids Memory Allocations
Span<T>lets you work with slices of arrays or buffers without copying the data.2. Improved Cache Locality
Because
Span<T>works on contiguous memory, operations on it tend to have better CPU cache performance than working with scattered objects.3. Safe, Zero-Cost Slicing
You can slice
Span<T>like this:This is zero-cost: no new array is created — just a new view.
4. Safe Alternative to Unsafe Code
Span<T>gives C-like pointer performance, but with type safety and bounds checking.5. Async Support with
Memory<T>Span<T>can’t be stored or returned from async methods (stack-only).Memory<T>solves this: it can be stored, passed around, and awaited..Span.Real-World Scenarios
Span<char>)Memory<byte>buffers without copyingLimitations
Span<T>can’t be:Memory<T>is more flexible but slightly less performantExample
Summary
Span<T>Memory<T>Span<T>andMemory<T>give you the power of unsafe pointers with the safety and performance of modern .NET.