optimizing-memory-allocation

Solid

Implements Zero Allocation patterns using Span, ArrayPool, and ObjectPool for memory efficiency in .NET. Use when reducing GC pressure or optimizing high-performance memory operations.

Data & Documents 40 stars 6 forks Updated 6 days ago MIT

Install

View on GitHub

Quality Score: 78/100

Stars 20%
54
Recency 20%
100
Frontmatter 20%
40
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# .NET Memory Efficiency, Zero Allocation A guide for APIs that minimize GC pressure and enable high-performance memory management. **Quick Reference:** See [QUICKREF.md](QUICKREF.md) for essential patterns at a glance. ## 1. Core Concepts - .NET CLR GC Heap Memory Optimization - Understanding Stack allocation vs Heap allocation - Stack-only types through ref struct ## 2. Key APIs | API | Purpose | NuGet | |-----|---------|-------| | `Span<T>`, `Memory<T>` | Stack-based memory slicing | BCL | | `ArrayPool<T>.Shared` | Reduce GC pressure through array reuse | BCL | | `DefaultObjectPool<T>` | Object pooling | Microsoft.Extensions.ObjectPool | | `MemoryCache` | In-memory caching | System.Runtime.Caching | --- ## 3. Span<T>, ReadOnlySpan<T> ### 3.1 Basic Usage ```csharp // Zero Allocation when parsing strings public void ParseData(ReadOnlySpan<char> input) { // String manipulation without Heap allocation var firstPart = input.Slice(0, 10); var secondPart = input.Slice(10); } // Array slicing public void ProcessArray(int[] data) { Span<int> span = data.AsSpan(); Span<int> firstHalf = span[..^(span.Length / 2)]; Span<int> secondHalf = span[(span.Length / 2)..]; } ``` ### 3.2 String Processing Optimization ```csharp // ❌ Bad example: Substring allocates new string string part = text.Substring(0, 10); // ✅ Good example: AsSpan has no allocation ReadOnlySpan<char> part = text.AsSpan(0, 10); ``` ### 3.3 Using with stackalloc ```csharp public void...

Details

Author
christian289
Repository
christian289/dotnet-with-claudecode
Created
7 months ago
Last Updated
6 days ago
Language
C#
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category

Code & Development Listed

performance-review

Review .NET code for allocation pressure, string handling, Span/pooling opportunities, LINQ costs, and caching - with explicit guidance on when performance work is and is not justified. Use when reviewing hot paths, optimizing .NET code, or evaluating performance claims.

0 Updated today
Sarmkadan
Data & Documents Solid

optimizing-io-operations

Optimizes standard I/O and file operations for high-performance data processing in .NET. Use when building high-throughput file processing or competitive programming solutions.

40 Updated 6 days ago
christian289
AI & Automation Listed

memory-contexts

Allocate memory in PostgreSQL backend C — pick the right MemoryContext and use palloc / palloc0 / pstrdup / psprintf correctly. Covers CurrentMemoryContext / TopMemoryContext / per-query / per-tuple / ExecutorState context choice, MemoryContextSwitchTo discipline, the OOM-throws-ereport contract (no NULL checks), pfree vs MemoryContextReset vs MemoryContextDelete, the AllocSet vs Slab vs Generation vs Bump context-type cheat sheet, and leak-scoping in long-running backends. Use whenever a PG patch or extension calls palloc / palloc0 / MemoryContextAlloc, creates or switches a MemoryContext, picks AllocSet vs Slab vs Generation vs Bump, or debugs a context-shaped leak. Skip for plain malloc / free / jemalloc / mimalloc / tcmalloc, JVM / Go / .NET GC tuning, Rust Box / Rc / Arc / lifetimes, shared_buffers / work_mem production tuning, valgrind / heaptrack on non-PG programs, and C++ smart pointers.

0 Updated 4 days ago
matejformanek