Skip to content
Skills Library
Programming · Review & audit

memory-allocation-analyzer

/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.

Licence
MIT
Last repository change
09.08.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 09.08.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

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.

Triggers from the description

s memory, or asks
Original author description

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.

How it works

  1. Input — point it at your code
  2. Allocation checklist
  3. Rewrite example
  4. Principles
  5. Output
  6. How to use it & best prompts
Original SKILL.md headings · 46 lines, 342 words
SKILL.md preview
---
name: memory-allocation-analyzer
description: 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.
category: Performance
version: 1.0.0
---

# 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
1. **LINQ in hot loops** — `Where/Select/ToList` allocate iterators + lists per call. In a tight loop, a plain `for` with no intermediate collection avoids it.
2. **Boxing** — value type → `object`/interface (e.g. `int` into a non-generic API, struct enumerator boxing). Use generics; avoid `object`.
3. **Closures** — lambdas capturing locals allocate a display class. Hoist captured state or use static lambdas (`static () => ...`).
4. **String building** — concatenation in loops → `StringBuilder` or `string.Create`/interpolation handler.
5. **`params` arrays** — hidden array allocation per call; provide non-params overloads on hot APIs.
6. **Unnecessary `async`** — a method that often completes synchronously → `ValueTask` to avoid the `Task` allocation.
7. **Defensive copies** — large structs passed by value; use `in`/`ref readonly`.
8. **Collections sized wrong** — pre-size `List`/`Dictionary` capacity to avoid re-allocation.

## Rewrite example
```csharp
// 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`.
Files in this package (2)
  • SKILL.md 3 KB
  • USAGE.md 1 KB

Versions

The database has no other skills named memory-allocation-analyzer.

Database snapshot: 13.11.2026 · package version d910827a85a3 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100memtrace
    OCaml memtrace profiling for allocation hotspot analysis. Use when Claude needs to: (1) Add memtrace instrumentation to OCaml executables, (2) Run targeted benchmarks with tracing enabled, (3) Identify allocation hotspots from trace output, (4) Optimize code to reduce boxing and…
  • 90fast-string-allocations
    Zero-allocation string building strategies. Use when formatting strings, generating ANSI codes, or writing hot loops to avoid heap allocations and Formatter state machine overhead.
  • 100145-java-refactoring-high-performance
    Use when you need to refactor Java code for high performance — including memory/allocation reduction, CPU hot-path optimization, and syntax/API/control-flow improvements. This should trigger for requests such as Review Java code for high performance; Optimize Java hot path;…
  • 100cpp-performance
    Detect and fix performance smells in C++ code in this repository: O(n^2) loops, unnecessary copies and allocations (outside hot paths, redundant copies of small trivial types are permitted for clarity), std::map vs std::unordered_map, virtual calls and std::function in hot…
  • 100span-memory-refactor
    Refactor hot C# code to use Span<T>, ReadOnlySpan<T>, and Memory<T> to cut allocations in parsing, slicing, and buffer work. Use this skill whenever the user wants to use Span/Memory, avoid substring/array-copy allocations, optimize parsing or string slicing, use stackalloc, or…
  • 90dotnet-csharp-runtime-review
    Use this skill when reviewing C# language and runtime correctness — nullable reference types, async/await, cancellation, disposal, allocations on hot paths, LINQ misuse, and Native AOT / trimming hazards. Trigger when a user provides C# source or project files and asks whether…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.