Applications often move data through several layers before a response is returned to the client. A request may enter through an ASP.NET Core endpoint, pass through an application service, be processed by a repository, and finally be returned as JSON.
The code can look clean while the application quietly creates multiple copies of the same data.
This becomes more important when the data is large. A list containing a few dozen objects usually does not create a noticeable problem. A collection containing hundreds of thousands of records, a large byte buffer, an image, a CSV file, or a generated report can make unnecessary copies expensive.
Every copy consumes memory and usually requires CPU time to create. If several copies are created during one request, the application can experience additional garbage collection, higher memory usage, and longer response times.
.NET provides several tools for reducing unnecessary copies. ReadOnlySpan<T>, Span<T>, Memory<T>, ReadOnlyMemory<T>, arrays, pooled buffers, and carefully designed APIs can help an application work with existing memory instead of repeatedly creating new objects.
The important part is understanding when sharing memory is safe and when making a copy is actually the better choice.
What Does Copying Data Mean?
Consider a simple array:
var numbers = new int[] { 10, 20, 30, 40, 50 };
If another method needs the values, you might create a new array:
var copy = numbers.ToArray();
Now there are two arrays containing the same values:
numbers
|
+-- 10
+-- 20
+-- 30
+-- 40
+-- 50
copy
|
+-- 10
+-- 20
+-- 30
+-- 40
+-- 50
The second array requires additional memory.
For five integers, this does not matter.
For a large buffer:
var copy = largeBuffer.ToArray();
the cost can be significant.
The application has to allocate another block of memory and copy every element into it.
Why Unnecessary Copies Become Expensive
Imagine an API receives a 10 MB file.
The request pipeline could accidentally create several copies:
10 MB request buffer
|
v
10 MB application buffer
|
v
10 MB processing buffer
|
v
10 MB response buffer
The application may temporarily hold much more than the original 10 MB.
This does not necessarily mean that every application should avoid copying at all costs. Copies are sometimes necessary for ownership, safety, or lifetime reasons.
The problem is unnecessary copying.
A useful rule is:
If the next layer only needs to read the existing data,
consider sharing the existing memory instead of copying it.
Arrays Are Already Reference Types
One of the easiest ways to avoid unnecessary copies is to understand how arrays behave in .NET.
When you pass an array to a method, you are passing a reference to the array object.
For example:
static void Process(int[] values)
{
Console.WriteLine(values.Length);
}
var numbers = new[] { 10, 20, 30 };
Process(numbers);
The method does not receive a complete copy of the array.
Both the caller and the method refer to the same array:
Caller
|
v
[10, 20, 30]
^
|
Method
This is already efficient when the method only needs to inspect the array.
The problem usually appears when developers create new collections unnecessarily:
Process(numbers.ToArray());
If numbers is already the required data, the extra ToArray() provides no benefit and creates another allocation.
Use ReadOnlySpan<T> for Synchronous Read-Only Access
ReadOnlySpan<T> is useful when a method needs to read a contiguous region of memory without taking ownership of it.
For example:
static int CalculateTotal(ReadOnlySpan<int> values)
{
var total = 0;
foreach (var value in values)
{
total += value;
}
return total;
}
The caller can pass an array:
var numbers = new[] { 10, 20, 30, 40 };
var total = CalculateTotal(numbers);
No new array is required.
The method receives a view over the existing data.
The important word here is view.
ReadOnlySpan<T> does not represent a new copy of the elements. It describes a region of existing contiguous memory.
Slicing Without Copying
This becomes particularly useful when only part of a buffer is required.
Suppose an application has:
var data = new byte[1024 * 1024];
and only the first 100 bytes need to be processed.
A common approach might be:
var portion = data.Take(100).ToArray();
That creates another array.
A span can instead represent the required section:
ReadOnlySpan<byte> portion = data.AsSpan(0, 100);
The memory looks conceptually like this:
Original buffer
[------------------------------------------------------]
^ ^
| |
start remaining data
[ first 100 bytes ]
^
|
ReadOnlySpan<byte>
The span does not need to copy those 100 bytes.
This can be useful in parsers, protocol processing, serialization, file processing, and other high-throughput code.
Span<T> When Modification Is Required
ReadOnlySpan<T> should be used when the caller's data must not be changed.
If a method needs to modify the existing memory, Span<T> can be used.
For example:
static void Normalize(Span<char> buffer)
{
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = char.ToUpperInvariant(buffer[i]);
}
}
The caller can pass an existing character array:
var chars = "hello".ToCharArray();
Normalize(chars);
Console.WriteLine(chars);
The original array is modified.
No second character array is created.
This is efficient, but it also means the method has access to the caller's memory. That ownership decision should be intentional.
Span<T> Has an Important Limitation
Span<T> and ReadOnlySpan<T> are stack-only types.
They are represented by ref struct types and cannot be stored in places where their lifetime could outlive the underlying stack context.
For example, you should not design an asynchronous API like this:
async Task ProcessAsync(ReadOnlySpan<byte> data)
{
await Task.Delay(10);
}
A span cannot safely cross an await boundary.
This is where Memory<T> and ReadOnlyMemory<T> become useful.
Use ReadOnlyMemory<T> Across Async Boundaries
ReadOnlyMemory<T> provides a similar concept to ReadOnlySpan<T> but can be stored and passed through asynchronous code.
For example:
static async Task ProcessAsync(
ReadOnlyMemory<byte> data,
CancellationToken cancellationToken)
{
await Task.Delay(10, cancellationToken);
var span = data.Span;
Console.WriteLine($"Received {span.Length} bytes.");
}
The important difference is that ReadOnlyMemory<T> can survive across asynchronous operations.
A simplified comparison is:
Type | Read-only | Can represent existing memory | Works across await |
|---|
Span<T> | No | Yes | No |
ReadOnlySpan<T> | Yes | Yes | No |
Memory<T> | No | Yes | Yes |
ReadOnlyMemory<T> | Yes | Yes | Yes |
Array | Depends | Yes | Yes |
For synchronous processing, spans are often convenient.
For asynchronous APIs, memory types are usually more appropriate.
Avoid Creating Strings Just to Extract Small Parts
Strings are immutable in .NET.
That means operations that create new strings allocate new objects.
For example:
var input = "[email protected]";
var domain = input.Substring(
input.IndexOf('@') + 1);
The resulting domain is a new string.
Sometimes that is exactly what the application needs.
But if the operation only needs to inspect the characters temporarily, a span can avoid creating another string:
ReadOnlySpan<char> input =
"[email protected]";
var at = input.IndexOf('@');
var domain = input[(at + 1)..];
The resulting domain is a ReadOnlySpan<char> pointing into the original string data.
This can be useful in parsing-heavy code.
However, do not force spans into ordinary application code just to eliminate every allocation. A normal string is often clearer and perfectly adequate.
Parsing Without Creating Intermediate Strings
Suppose an API receives a value such as:
customer:12345
A naive parser might use:
var parts = input.Split(':');
var type = parts[0];
var id = int.Parse(parts[1]);
This can create additional objects.
A span-based parser can work directly against the original characters:
static bool TryParseCustomer(
ReadOnlySpan<char> input,
out int id)
{
id = default;
var separator = input.IndexOf(':');
if (separator < 0)
{
return false;
}
var type = input[..separator];
if (!type.SequenceEqual("customer"))
{
return false;
}
return int.TryParse(
input[(separator + 1)..],
out id);
}
The method can inspect sections of the input without creating a collection from Split.
This approach is especially useful when parsing is performed frequently.
Sharing Memory With Collections
Not every collection can be treated as a contiguous block of memory.
For example:
List<int>
is not the same abstraction as:
int[]
A list internally manages an array, but its API represents a resizable collection.
If a method only needs to enumerate values, accepting an appropriate read-only abstraction can be better than forcing callers to create another collection.
For example:
static int CalculateTotal(
IReadOnlyList<int> values)
{
var total = 0;
for (var i = 0; i < values.Count; i++)
{
total += values[i];
}
return total;
}
The caller can pass an existing list without creating another list.
The correct abstraction depends on what the method actually needs.
Do Not Copy Just to Make an API Look Clean
This pattern appears frequently:
public IReadOnlyList<Product> GetProducts()
{
return products.ToList();
}
Developers sometimes create a new list because they do not want to expose the original collection.
That may be correct if the caller must receive an independent snapshot.
But if the data is already immutable or safely encapsulated, the copy may be unnecessary.
A better API design might be:
public IReadOnlyList<Product> GetProducts()
{
return products;
}
or use an immutable collection when ownership and mutation need to be explicit.
The key question is not "Can I return this collection?"
The better question is:
Does the caller need its own copy,
or does the caller only need safe read access?
ArrayPool<T> for Temporary Large Buffers
Sometimes an application really needs a writable buffer, but allocating a new large array for every operation creates unnecessary garbage.
ArrayPool<T> can help in these situations.
For example:
var buffer = ArrayPool<byte>.Shared.Rent(64 * 1024);
try
{
// Use the buffer.
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
The pool allows the application to reuse arrays instead of allocating a new one every time.
The finally block is important.
A rented buffer must be returned even if processing fails.
The code also needs to treat the rented array carefully because its actual length can be larger than the requested size.
For example:
var buffer = ArrayPool<byte>.Shared.Rent(4096);
try
{
var requestedSize = 4096;
// Only use the required region.
var memory = buffer.AsMemory(0, requestedSize);
// Process memory.
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
Pooling is powerful, but it introduces ownership rules that normal allocations do not.
Do Not Hold Pooled Memory Too Long
A pooled buffer should normally have a short and clearly defined lifetime.
A poor design is:
Rent buffer
|
v
Store buffer in service
|
v
Return buffer much later
This can reduce the amount of memory available to other operations.
A better design is:
Rent
|
v
Process
|
v
Return
The shorter the ownership window, the easier the code is to reason about.
Copying Can Still Be the Right Choice
Avoiding copies should not become a rule that every method must follow.
A copy is often the correct choice when:
The caller must be able to modify its data independently.
Data needs to survive after the source memory is released.
A stable snapshot is required.
Data crosses a lifetime boundary.
Security requires isolation from mutable input.
The copied representation improves clarity enough to justify the allocation.
For example:
var snapshot = source.ToArray();
may be completely appropriate if snapshot must remain unchanged even when source changes.
Performance engineering is about removing unnecessary work, not removing all work.
Common Mistakes
Returning a Mutable Internal Buffer
If a class exposes its internal array directly, callers may modify internal application state.
Use a read-only abstraction or return a copy when ownership must be separated.
Using Span in Async Code
Span<T> and ReadOnlySpan<T> cannot be carried across asynchronous suspension points.
Use Memory<T> or ReadOnlyMemory<T> when the operation needs to cross await.
Pooling Without Returning Buffers
Forgetting to return an ArrayPool<T> buffer can create memory pressure and defeat the purpose of pooling.
Copying Every Collection
Calling ToList(), ToArray(), or similar methods repeatedly can create avoidable allocations.
First determine whether the receiving method actually requires a separate collection.
Optimizing Tiny Allocations
Do not introduce complicated span or pooling code to optimize a tiny allocation that has no measurable impact.
The resulting code may be harder to maintain without providing a meaningful performance improvement.
Best Practices
Start With Measurement
Use allocation profiling, memory diagnostics, and realistic workloads before changing APIs for performance.
Prefer Clear Ownership
Every shared memory API should make it reasonably clear who owns the data and who is allowed to modify it.
Use Read-Only Types When Possible
If a method only needs to inspect data, prefer read-only abstractions such as:
ReadOnlySpan<T>
ReadOnlyMemory<T>
IReadOnlyList<T>
depending on the lifetime and access requirements.
Keep Buffer Lifetimes Short
For pooled memory, rent only when needed and return the buffer as soon as processing is complete.
Optimize Hot Paths
Memory-sharing techniques provide the most value in code that runs frequently or processes large amounts of data.
Typical examples include:
Parsers
Serialization
Networking
File processing
Image processing
Compression
Logging pipelines
High-throughput APIs
Advantages
Reduces unnecessary allocations.
Lowers temporary memory usage.
Can reduce garbage collection pressure.
Improves performance in allocation-heavy hot paths.
Allows efficient processing of portions of existing buffers.
Supports high-throughput applications.
Disadvantages
Introduces more complex lifetime and ownership rules.
Span<T> cannot cross asynchronous boundaries.
Pooled memory requires careful cleanup.
Shared mutable memory can create difficult bugs.
Overuse can make simple application code harder to understand.
Performance improvements depend on the workload.
When Should You Share Memory?
A useful decision process is:
Is the data large or frequently processed?
|
Yes
|
v
Does the method need only read access?
|
Yes
|
v
Can it use a view safely?
|
Yes
|
v
Use Span / ReadOnlySpan / Memory
If the data is small and the operation happens rarely, a normal object or collection is often the better choice.
For example, there is little reason to replace:
var name = customer.Name;
with a complicated span-based design.
The goal is not to make every line allocation-free.
The goal is to remove meaningful unnecessary work where it matters.
Troubleshooting Checklist
When investigating memory usage caused by data processing, check:
Are ToArray() or ToList() being called repeatedly?
Are large strings being created during parsing?
Are temporary buffers allocated for every request?
Could a ReadOnlySpan<T> or ReadOnlyMemory<T> safely represent existing data?
Is a pooled buffer always returned?
Is mutable internal state being exposed?
Are unnecessary collection conversions happening between application layers?
Is the code running frequently enough for allocation reduction to matter?
Have allocations been measured with a profiler?
Is a copy actually required for lifetime or ownership reasons?
Summary
Sharing existing memory can reduce allocation pressure and improve performance in .NET applications, particularly when processing large buffers or running the same operation thousands of times.
The key types have different purposes. ReadOnlySpan<T> is useful for fast synchronous read-only access to existing contiguous memory. Span<T> allows controlled modification. ReadOnlyMemory<T> and Memory<T> are better suited to APIs that need to carry memory across asynchronous operations.
ArrayPool<T> is useful when applications repeatedly need temporary large buffers, but pooled memory requires careful ownership and cleanup.
The most important lesson is not to eliminate every copy. Some copies are necessary and can make an application safer and easier to understand. Instead, identify where the application is copying large or frequently processed data without a real reason.
Start with measurements, understand who owns the memory, choose the simplest abstraction that solves the problem, and optimize the hot paths where the savings will actually matter.
Join the conversation! Your thoughts help the community grow.