Node.js Memory Leak Diagnostic Prompt from Heap Profiler Dumps
30 Production Challenge Scenarios • Real-Time Automated Quality Scoring

Interactive Prompt Engineering Lab

Test and score your prompts against real-world enterprise scenarios. Get immediate 4-axis quality telemetry, negative guardrail verification, and golden reference prompts.

Read Syllabus
1. Authoritative Persona
Expert role + zero fluff
25 pts
2. Deterministic Schema
XML tags + JSON / keys
25 pts
3. Negative Guardrails
NEVER / fallback rules
25 pts
4. Token Efficiency
150–1,800 chars concise
25 pts
Web & System ArchitectureStaff

Node.js Memory Leak Diagnostic Prompt from Heap Profiler Dumps

Analyze a V8 Chrome DevTools heap snapshot summary showing runaway memory consumption in a high-throughput Node.js microservice. Identify the leak source and propose the exact architectural code fix.

Target Input Context / Untrusted Data:
Process: Express.js API gateway (V8 Heap grew from 180MB to 1.8GB over 6 hours before OOM kill)
Heap Snapshot Top Retainers:
1. Object (Closure Context) - 62% of allocated heap
   - Retained by: EventEmitter.listeners('order_processed')
   - Root: Global EventBus singleton holding arrow functions referencing req.user and req.headers
2. (string) - 18% of heap (JWT tokens and authorization headers retained in closure).
Quality Requirements & Guardrails:
Define a Staff Node.js Runtime & Performance Engineer persona.
Diagnose the exact leak mechanism: unremoved event listeners holding closure scope of request objects.
Explain how retaining req.user prevents the entire HTTP request context from being garbage collected.
Provide both the fix (eventBus.off / once / AbortSignal) and a leak detection unit test.
Your System Prompt Draft
75 chars~19 tokens
Golden Reference Architecture (100% Quality Benchmark)
<system_instructions>
You are a Staff V8 Engine & Node.js Core Diagnostics Specialist.
Analyze the provided heap snapshot telemetry and formulate an architectural remediation report.

DIAGNOSTIC PROTOCOL:
- Identify why `EventEmitter.listeners` is retaining 62% of heap memory: anonymous arrow function listeners registered per request are never unbound, creating permanent references in the global EventBus retainer tree.
- Explain the closure cascade: because the listener references `req.headers`, V8 is unable to garbage collect the entire HTTP IncomingMessage context.
- ARCHITECTURAL REMEDIATION:
  1. Show the anti-pattern vs drop-in replacement using `EventEmitter.once()` or passing Node.js `AbortSignal` (`{ signal: req.signal }`).
  2. Implement an automated leak test using `process.memoryUsage()` to assert stable memory after 10,000 synthetic events.
- Output analysis inside <memory_diagnostic_report> tags.
</system_instructions>