Skip to content

Memory<T> for heap or async

Context: Memory<T> is a struct that represents a contiguous region of memory that can live on the heap (e.g., an array) and be used in asynchronous methods. Unlike Span<T>, Memory<T> is not a ref struct, so it can be stored in fields, captured in async lambdas, and used as a parameter in async methods. You obtain a Span<T> from a Memory<T> via the Span property.

using System;
using System.Threading.Tasks;
public class MemoryDemo
{
public static async Task ProcessAsync(Memory<byte> memory)
{
// Work with the memory (e.g., write to a stream)
// For actual operations, get a Span (but be careful: Span cannot be used across await)
// Instead, use Memory<byte> directly with stream.WriteAsync
await Task.Delay(10);
Console.WriteLine($"Processing {memory.Length} bytes");
}
public static async Task Example()
{
byte[] buffer = new byte[1024];
Memory<byte> memory = buffer.AsMemory();
await ProcessAsync(memory);
}
}
Terminal window
dotnet run
Processing 1024 bytes
  • Use Memory<T> when you need to store the reference across await boundaries.
  • Convert to Span<T> only for synchronous operations within a method.

Asynchronous file copyStream.ReadAsync and WriteAsync accept Memory<byte> to read/write chunks without extra allocations.
See .NET docs on Memory.