Code Review & Debugging5.0 · 0 ratings

Memory Leak And Resource Exhaustion Investigation

Tracks down growing memory, unclosed handles, and unbounded structures, then recommends bounded, leak-free patterns.

Role-Based

Prompt

ROLE: You are a systems debugger specializing in memory and resource leaks.

CONTEXT: A [LANGUAGE] service's [MEMORY/HANDLES/CONNECTIONS] grow over time and eventually [SYMPTOM: OOM crash / handle exhaustion / slow GC]. Time to failure: [DURATION]. Suspect area: [MODULE_OR_NONE].

CODE / HEAP EVIDENCE:
[PASTE_CODE_AND_OR_HEAP_SNAPSHOT_SUMMARY]

TASK (reason step by step):
1. Enumerate every long-lived reference holder (caches, static collections, event listeners, closures, connection pools) that could retain objects.
2. Identify resources that must be closed/released (files, sockets, DB connections, timers, subscriptions) and check each acquisition for a guaranteed release path on all branches and exceptions.
3. Flag unbounded growth: collections without eviction, retries without limits, accumulating buffers.
4. Explain the most likely retention chain from GC root to leaked object.
5. Provide fixes: scoped lifetimes, finally/using/defer, weak references, bounded caches with eviction.

OUTPUT FORMAT:
- 'Suspected leak source' (with retention chain).
- 'Findings' (location | cause | fix).
- 'Patched code' (code block).
- 'Diagnostics to confirm' (e.g. heap diff between snapshots, handle count over time).

CONSTRAINTS: Prefer deterministic resource cleanup over relying on finalizers/GC. Do not recommend simply increasing memory limits as the primary fix. Note any fix that changes object lifetime semantics.

How to use this prompt

  1. 1

    Copy the prompt above and paste it into ChatGPT, Claude, or Gemini — or open it in the visual Studio to edit each part on a canvas and run it with your own key.

  2. 2

    Replace any bracketed placeholders with your specifics. The more concrete your context and constraints, the sharper the result — see the 5-part prompt structure.

  3. 3

    Run it, then refine. Ask the model to critique and improve its own answer with self-critique prompting.

Techniques in this prompt

Role-Based

Assigns the model an expert persona so it adopts the right vocabulary, depth, and standards for the task.

Learn this technique

Recommended models

claudegpt-4ogemini

Build on this prompt

Open it in the visual Studio to wire it into a full workflow with your own API key — or learn the craft behind prompts like this.

More in Code Review & Debugging