Memory Management & Performance
Learn .NET Core's memory management and performance optimization techniques. This is a foundational concept in cross-platform .NET development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world .NET Core experience. Take your time with each section and practice the examples
75 min•By Priygop Team•Last updated: Feb 2026
Span<T> and Memory<T>
Example
// Span<T> for stack-allocated memory
public static int SumSpan(Span<int> numbers)
{
int sum = 0;
foreach (var number in numbers)
{
sum += number;
}
return sum;
}
// Memory<T> for heap-allocated memory
public static async Task ProcessMemoryAsync(Memory<byte> data)
{
// Process data without copying
for (int i = 0; i < data.Length; i++)
{
data.Span[i] = (byte)(data.Span[i] ^ 0xFF);
}
}
// Usage examples
var array = new int[] { 1, 2, 3, 4, 5 };
var span = array.AsSpan();
var sum = SumSpan(span);
// String processing with Span<char>
public static ReadOnlySpan<char> GetSubstring(ReadOnlySpan<char> text, int start, int length)
{
return text.Slice(start, length);
}Performance Best Practices
- Use Span<T> and Memory<T> for zero-copy operations
- Prefer stackalloc for small arrays — a critical concept in cross-platform .NET development that you will use frequently in real projects
- Use ArrayPool<T> for temporary arrays
- Avoid unnecessary allocations in hot paths — a critical concept in cross-platform .NET development that you will use frequently in real projects
- Use StringBuilder for string concatenation — a critical concept in cross-platform .NET development that you will use frequently in real projects
- Implement IDisposable for resource management — a critical concept in cross-platform .NET development that you will use frequently in real projects
- Use ValueTask<T> for frequently called async methods
Try It Yourself — Advanced C# & .NET Core Features
Try It Yourself — Advanced C# & .NET Core FeaturesHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|32 lines|1593 chars|✓ Valid syntax
UTF-8