Memory allocation analyzer
Skill StefanTheCode/dotnet-ai-toolkit/skills/memory-allocation-analyzer
Find unnecessary heap allocations in C# hot paths — LINQ in loops, boxing, closures, string concatenation, params arrays, async state machines — and rewrite to reduce GC pressure. Use this skill whenever the user wants to reduce allocations, cut GC pressure, find boxing/closures, optimize a hot path's memory, or asks "why does this allocate so much / reduce garbage". Always use this skill for allocation-reduction requests; it runs a fixed checklist and rewrites the code.From its SKILL.md
npx -y skills add StefanTheCode/dotnet-ai-toolkit --skill memory-allocation-analyzerAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
3.0 KB, as published. Nobody here has run it
Memory Allocation Analyzer
Spot avoidable heap allocations on hot paths and rewrite them to cut GC pressure — without sacrificing readability where it doesn't matter.
Input — point it at your code
Works on a target: a file, folder, project, or GitHub URL. Focus on hot paths (request handlers, loops, serialization). Find candidates: grep -rn "\.Select(\|\.Where(\|\.ToList()\|+ \"\|string.Format\|new \[\]" --include=*.cs <target>.
Allocation checklist
- LINQ in hot loops —
Where/Select/ToListallocate iterators + lists per call. In a tight loop, a plainforwith no intermediate collection avoids it. - Boxing — value type →
object/interface (e.g.intinto a non-generic API, struct enumerator boxing). Use generics; avoidobject. - Closures — lambdas capturing locals allocate a display class. Hoist captured state or use static lambdas (
static () => ...). - String building — concatenation in loops →
StringBuilderorstring.Create/interpolation handler. paramsarrays — hidden array allocation per call; provide non-params overloads on hot APIs.- Unnecessary
async— a method that often completes synchronously →ValueTaskto avoid theTaskallocation. - Defensive copies — large structs passed by value; use
in/ref readonly. - Collections sized wrong — pre-size
List/Dictionarycapacity to avoid re-allocation.
Rewrite example
// allocates: closure + iterator + list, every call
var ids = items.Where(i => i.IsActive).Select(i => i.Id).ToList();
// hot path: no intermediate allocations
var ids = new List<int>(items.Count);
foreach (var i in items) if (i.IsActive) ids.Add(i.Id);
Principles
- Only optimize hot paths. LINQ readability wins everywhere else — don't uglify cold code to save nanoseconds.
- Measure with
[MemoryDiagnoser](benchmarkdotnet-setup) before and after — guesses mislead. - Allocation reduction is about steady-state GC pressure, not one-off startup cost.
Output
## Findings (hot paths only)
- <file:line> [closure] ... → ...
## Rewritten
<code>
## Verify
Benchmark with [MemoryDiagnoser]; compare Allocated/Gen0.
How to use it & best prompts
"Why does this allocate so much", "reduce GC pressure in this handler", "find boxing/closures here", "cut allocations in this hot loop". Pairs with benchmarkdotnet-setup and gc-pressure-auditor.