Working with large amounts of data in memory can create performance problems that are not always obvious from application code.
A common example is passing an existing byte array to an API that accepts a Stream. Developers often create a MemoryStream, copy the data into it, and then pass the stream to the next operation. The code works, but the application may allocate additional memory and spend time copying data that already exists in memory.
This becomes more expensive when an application processes large files, HTTP payloads, configuration documents, or data produced by System.IO.Pipelines.
.NET already provides types such as Span, Memory, and ReadOnlySequence to work with existing memory without unnecessary copying. However, many existing APIs still expect a Stream.
.NET 11 addresses this gap with four new in-memory stream adapters:
- ReadOnlyMemoryStream
- WritableMemoryStream
- ReadOnlySequenceStream
- StringStream
These types allow developers to expose existing in-memory data through the Stream abstraction without first copying everything into a MemoryStream.
This article explains how these adapters work, where each one is useful, and how they can help reduce unnecessary memory allocations in production applications.
Why Creating a MemoryStream Can Introduce Unnecessary Copies
Consider an application that receives a byte array from an HTTP request or another service.
The data is already available in memory, but the next API expects a Stream.
A common implementation looks like this:
byte[] payload = GetPayload();
using var stream = new MemoryStream();
await stream.WriteAsync(payload);
stream.Position = 0;
await ProcessDataAsync(stream);
This implementation creates a new MemoryStream and writes the existing byte array into its internal buffer.
The original payload remains in memory, while the MemoryStream maintains another copy of the data.
For a small payload, the additional allocation may not matter. However, when an application processes large payloads or handles many requests concurrently, these copies can increase memory pressure and garbage collection activity.
Developers sometimes use MemoryStream's constructor to avoid this explicit write operation:
byte[] payload = GetPayload();
using var stream = new MemoryStream(
payload,
writable: false);
await ProcessDataAsync(stream);
This constructor can wrap the existing array rather than copying its contents. It is already a useful option when the source is a byte array.
However, it does not solve every scenario. The source might be a ReadOnlyMemory<byte>, a segmented ReadOnlySequence<byte>, or a string that needs to be exposed as a stream.
.NET 11 introduces dedicated stream adapters for these cases.
Understanding the New In-Memory Stream Adapters
The new types provide a bridge between existing in-memory data structures and APIs that consume Stream.
Type | Source data | Purpose |
|---|
ReadOnlyMemoryStream | ReadOnlyMemory<byte> | Read existing byte memory through a read-only stream |
WritableMemoryStream | Memory<byte> | Read and write through a fixed-size memory stream |
ReadOnlySequenceStream | ReadOnlySequence<byte> | Read segmented memory without flattening it |
StringStream | string or ReadOnlyMemory<char> | Read text through a stream using a specified encoding |
These adapters do not require developers to copy the complete source data into a new MemoryStream first.
They also preserve the behavior expected by APIs that already accept Stream, making them useful when modernizing existing applications without changing every downstream component.
Using ReadOnlyMemoryStream to Expose Existing Byte Data
ReadOnlyMemoryStream is useful when an application already has a ReadOnlyMemory<byte> value and needs to pass it to an API that expects a Stream.
For example, consider an application that stores a document payload in memory and sends it to another service.
Without the new adapter, developers might convert the memory into a byte array or copy it into a MemoryStream.
With .NET 11, the existing memory can be exposed directly.
using System.IO;
using System.Net.Http;
ReadOnlyMemory<byte> payload = GetPayload();
using Stream body = new ReadOnlyMemoryStream(payload);
using var content = new StreamContent(body);
using var client = new HttpClient();
using var response = await client.PostAsync(
"https://example.com/api/documents",
content);
response.EnsureSuccessStatusCode();
The ReadOnlyMemoryStream wraps the existing memory and exposes it through the Stream interface.
There is no need to create an intermediate byte array or copy the complete payload into another MemoryStream.
This is useful for applications that already use Memory<T> or ReadOnlyMemory<T> as part of their data-processing pipeline.
For example, a service might receive a payload from a buffer pool, perform some processing, and then pass the resulting memory to an HTTP client.
The adapter allows the application to reuse the existing memory representation.
Because the stream is read-only, consumers can read the data but cannot modify the underlying buffer through the stream.
The application must still ensure that the underlying memory remains valid and unchanged for as long as the stream is being used.
Using WritableMemoryStream for Existing Buffers
ReadOnlyMemoryStream is appropriate when the source data should not be modified.
However, some APIs need a writable Stream.
WritableMemoryStream provides this capability by exposing an existing Memory<byte> as a fixed-size stream.
Consider an application that needs to write generated binary data into an existing buffer.
using System.IO;
byte[] buffer = new byte[4096];
using Stream stream = new WritableMemoryStream(buffer);
byte[] data = [10, 20, 30, 40, 50];
await stream.WriteAsync(data);
Console.WriteLine(stream.Position);
Console.WriteLine(buffer[0]);
The stream writes directly into the supplied memory instead of maintaining a separate expandable MemoryStream buffer.
One important difference is that WritableMemoryStream is fixed-size.
It does not behave like a normal expandable MemoryStream. If an operation attempts to write beyond the available capacity, the operation cannot simply grow the underlying buffer.
This makes it useful when the application already knows the available memory size and wants to reuse it.
A practical example is a serialization pipeline that writes into a preallocated buffer.
using System.IO;
using System.Text.Json;
byte[] buffer = new byte[8192];
using Stream stream = new WritableMemoryStream(buffer);
await JsonSerializer.SerializeAsync(
stream,
new
{
Id = 101,
Name = "Laptop",
Category = "Electronics"
});
Console.WriteLine($"Bytes written: {stream.Position}");
The serializer writes directly into the existing buffer through the Stream interface.
In production code, make sure the destination buffer is large enough for the expected output. If the required size is unknown, use an expandable destination or a suitable buffer-writer abstraction instead.
Using ReadOnlySequenceStream Without Flattening Segmented Data
ReadOnlySequence<byte> is particularly useful in high-performance applications that use System.IO.Pipelines.
Unlike a normal byte array, a ReadOnlySequence<byte> can represent data spread across multiple memory segments.
This design allows pipelines to process data incrementally without requiring every segment to be combined into one large contiguous array.
For example, a network application might receive data through a PipeReader. The resulting sequence can contain several memory segments.
A traditional approach might convert the sequence into a single byte array:
ReadOnlySequence<byte> sequence = GetSequence();
byte[] payload = sequence.ToArray();
using var stream = new MemoryStream(payload);
await ProcessDataAsync(stream);
The ToArray call creates a contiguous copy of the complete sequence.
For large payloads, this can introduce unnecessary memory allocation and copying.
.NET 11 provides ReadOnlySequenceStream to expose the sequence directly:
using System.Buffers;
using System.IO;
ReadOnlySequence<byte> sequence = GetSequence();
using Stream stream = new ReadOnlySequenceStream(sequence);
await ProcessDataAsync(stream);
The stream reads from the existing sequence without first flattening all its segments into a single contiguous buffer.
This is especially useful when integrating pipelines with older libraries that accept Stream rather than ReadOnlySequence<byte>.
For example, a custom network protocol processor may use PipeReader to receive data but rely on an existing parser that accepts Stream.
ReadOnlySequenceStream allows both components to work together without introducing an additional full-buffer copy.
It is important to remember that ReadOnlySequenceStream does not make the underlying sequence independent of its owner.
When working with System.IO.Pipelines, follow the buffer ownership rules carefully. Do not advance the PipeReader or release the underlying memory while a consumer is still reading from the stream.
Using StringStream to Pass Text Without an Intermediate Byte Array
Many APIs accept Stream even when the original data is text.
For example, a configuration parser might accept a YAML or XML document through a Stream.
A traditional implementation may convert a string into a byte array first:
using System.Text;
string yamlText = """
name: application
environment: production
""";
byte[] bytes = Encoding.UTF8.GetBytes(yamlText);
using var stream = new MemoryStream(bytes);
var settings = ParseConfiguration(stream);
This creates an intermediate byte array before the parser can process the document.
.NET 11 introduces StringStream, which allows an existing string or ReadOnlyMemory<char> to be exposed as a Stream using a specified encoding.
using System.IO;
using System.Text;
string yamlText = """
name: application
environment: production
""";
using Stream stream = new StringStream(
yamlText,
Encoding.UTF8);
var settings = ParseConfiguration(stream);
The StringStream adapter makes the text available through the Stream interface without requiring the application to create an intermediate byte array itself.
This is useful when integrating text-based data with APIs that were originally designed around streams.
For example, a configuration service may store configuration content as a string but use an existing parser that expects Stream.
StringStream provides a direct bridge between these two representations.
The selected encoding matters because a string contains UTF-16 characters in .NET, while a Stream exposes bytes. The adapter performs the required encoding as the stream is read.
Avoid assuming that the output will always use UTF-8. Specify the encoding expected by the receiving API.
Understanding How These Adapters Reduce Memory Pressure
The main benefit of these new types is not that they eliminate every memory allocation in an application.
Their value comes from avoiding unnecessary intermediate buffers when data already exists in memory.
Consider a service that receives a large document, stores it in a ReadOnlyMemory<byte>, and passes it to another API.
Without a suitable adapter, the application may create an additional MemoryStream buffer and copy the document into it.
With ReadOnlyMemoryStream, the existing memory can be exposed directly through the Stream abstraction.
The same principle applies to segmented data and strings.
These adapters can reduce:
- Additional full-payload copies.
- Temporary byte-array allocations.
- Memory pressure during large data transfers.
- Unnecessary buffer flattening in pipeline-based applications.
- Integration overhead between memory-based APIs and stream-based libraries.
However, they do not automatically make the entire application zero-copy.
A downstream library may still allocate its own buffers, deserialize the complete document into objects, or copy data internally.
For example, passing a ReadOnlyMemoryStream to an API does not guarantee that the receiving API will process the content without allocations.
The improvement depends on the complete processing path.
Choosing Between MemoryStream and the New Adapters
MemoryStream remains useful when an application needs a growable in-memory stream.
The new adapters address a different requirement: exposing existing data through Stream without first creating a separate expandable buffer.
Scenario | Suitable approach |
|---|
Building new content dynamically in memory | MemoryStream |
Reading an existing ReadOnlyMemory<byte> | ReadOnlyMemoryStream |
Writing into an existing fixed-size buffer | WritableMemoryStream |
Reading segmented pipeline data | ReadOnlySequenceStream |
Exposing existing text as a stream | StringStream |
Do not replace every MemoryStream in an application with one of the new adapters.
If the application needs to expand the stream dynamically, MemoryStream is still an appropriate choice.
If the data already exists in a compatible memory representation, the new adapters can avoid an unnecessary conversion step.
The goal is to select the simplest abstraction that matches the data ownership and processing requirements.
Production Considerations
Reducing memory copies can improve application efficiency, but developers should pay attention to memory ownership and lifecycle management.
Keep the underlying memory alive
A stream adapter does not automatically take ownership of the underlying memory.
The source buffer must remain valid while the stream is being consumed.
This is particularly important when using ArrayPool, MemoryPool, or buffers owned by System.IO.Pipelines.
Do not return a rented array to the pool or release a pipeline buffer while another component is still reading from it.
Avoid modifying memory while it is being read
ReadOnlyMemoryStream prevents modification through the stream itself, but it does not make the underlying memory immutable.
Another part of the application could still modify the original array.
If multiple components access the same buffer concurrently, establish clear ownership and synchronization rules.
Understand fixed-size stream behavior
WritableMemoryStream uses existing fixed-size memory.
It is not a replacement for MemoryStream when the output size is unknown or needs to grow dynamically.
Choose the buffer size carefully and handle insufficient capacity appropriately.
Measure actual allocations
Do not assume that using a memory adapter automatically improves the entire application.
Use BenchmarkDotNet, dotnet-counters, or a memory profiler to compare the original implementation with the new one.
Measure allocation volume, garbage collection activity, throughput, and processing time using realistic payload sizes.
A useful benchmark should include both small and large payloads because the benefit may be more noticeable when the data size increases.
Summary
.NET 11 introduces four in-memory stream adapters that make it easier to pass existing data to APIs that expect Stream without first copying everything into a MemoryStream.
ReadOnlyMemoryStream exposes existing byte memory for reading. WritableMemoryStream provides a fixed-size writable stream over existing memory. ReadOnlySequenceStream allows segmented pipeline data to be consumed without flattening it into a contiguous array. StringStream exposes existing text through a stream using a specified encoding.
These additions are particularly useful when modernizing applications that combine newer memory-based APIs with existing stream-based libraries.
They do not eliminate every allocation or make every operation zero-copy, but they remove a common source of unnecessary intermediate buffers.
For applications that process large payloads, pipeline data, or text documents, choosing the appropriate adapter can simplify integration and reduce memory pressure.
The best approach is to understand how the data is owned, identify where unnecessary copies occur, and measure the complete processing path before making performance changes.
Join the conversation! Your thoughts help the community grow.