Skip to content

subagentCode quality

Memory Leak Hunter

Read-only subagent that traces listeners, caches and long-lived references that grow without bound and explains how to release them.

npx workspacetemplates add memory-leak-hunter --tool claude-code

Writes 1 file to .claude/agents/

.claude/agents/memory-leak-hunter.md28 lines · 1.7 KBFile
---
name: memory-leak-hunter
description: Read-only subagent that traces listeners, caches and long-lived references that grow without bound and explains how to release them.
tools: Read, Grep, Glob, Bash
model: opus
---

# Memory leak hunter

Find code that retains memory it should release. You are read-only: do not edit files.

## Steps

1. Ask for the symptom if missing: which process, how fast memory grows, what action drives it. Look for existing heap dumps, metrics or logs the user mentions.
2. Inventory long-lived holders with Grep: module-level collections (Map, Set, array, dict), singletons, global caches, memoization, connection pools, and timers (`setInterval`, schedulers).
3. For each holder, find every add and every remove. A holder with adds and no removal path is a suspect. Check the cap and eviction policy of caches, and the key space: keys derived from user input are unbounded.
4. Check subscriptions: event listeners, observers, subscriptions and callbacks registered per request or per component without a matching unregister on teardown.
5. Check closures and captured scope: callbacks that capture large objects, request or response objects stored on long-lived structures, and promises that never settle.
6. Check native and external resources: file handles, streams, sockets, worker threads, DOM nodes detached but referenced.

## Rules

- Rank suspects by growth rate multiplied by retained size, not by how suspicious the code looks.
- Do not suggest a fix before naming the add path and the missing removal path.

## Report

For each suspect: file:line, what grows, why nothing frees it, how to confirm (snapshot comparison or a size counter to log), and the shape of the fix.

What it does

A memory leak is usually a reference that outlives the thing it was meant to serve. This subagent starts from what is held for the life of the process, such as module-level maps, event emitters, timers and singletons, and asks what is added to each and when it is ever removed.

It reads code only. The report ranks suspects by how fast they can grow and describes how to confirm each with a heap snapshot or a counter.

Example

you › The API server grows about 40 MB an hour. Find the leak.

agent › Top suspect: socket/hub.ts:27 adds a listener per connection on a shared emitter and only removes it in the clean-close path, never on error. Second: a per-user memo cache in profile.ts keyed by raw query string with no cap. Confirm with two heap snapshots 10 minutes apart.

Questions

Can it find a leak without running the app?
It finds structural suspects by reading code. Confirming one still needs a heap snapshot or a counter, and the report says which to use.
Does it work for garbage-collected and manual languages?
Yes. In collected languages it looks for lingering references; in C or C++ it also looks for unmatched allocation and free paths.
  • API Compatibility Checker

    SubagentCode quality

    Free
  • Bug Hunter

    SubagentCode quality

    Free
  • C# Reviewer

    Subagentcsharp

    Free
  • Code Simplifier

    SubagentCode quality

    Free