Maintenance in progress: we are indexing a large batch of new skills. Some pages may load slowly or briefly show no results. Nothing is lost, and everything is back to normal within the hour.

Optimizing Memory Allocation Lite

Provides essential Zero Allocation patterns using Span and ArrayPool. Use when quickly referencing core memory optimization techniques without detailed explanations.

majiayu000 cf5a7d3 2 files · 1.7 KB Updated 567 repo stars

File contents

Zero Allocation Essentials

1. Span Basics

// String slicing (no Heap allocation)
ReadOnlySpan<char> part = text.AsSpan(0, 10);

// Array slicing
Span<int> span = data.AsSpan();
Span<int> firstHalf = span[..^(span.Length / 2)];

2. ArrayPool

// Rent array
var buffer = ArrayPool<byte>.Shared.Rent(size);

try
{
    ProcessBuffer(buffer.AsSpan(0, size));
}
finally
{
    ArrayPool<byte>.Shared.Return(buffer);
}

3. stackalloc

// Allocate small buffer on Stack
Span<byte> buffer = stackalloc byte[256];

4. Important Notes

  • Span cannot be used with async-await
  • ArrayPool: Must call Return after Rent

For details: See /dotnet-zero-allocation skill

majiayu000/claude-skill-registry-data/tree/main/performance/optimizing-memory-allocation-lite commit cf5a7d3d06

Frequently asked questions

npx skillmds add majiayu000/optimizing-memory-allocation-lite