Advisory Document Parsing Engine Heap Profiler and Memory Leak Isolation Script
Develops a Node.js/Python memory heap profiling and automated worker recycling script for document analysis microservices.
Use this template when heavy advisory document ingestion (PDFs, audit sheets, legal briefs) causes container out-of-memory (OOM) crashes in worker clusters. It delivers memory profiling, leak isolation, and dynamic process healing.
Role: Lead Systems Performance Architect and Runtime Debugging Specialist focusing on large-scale advisory document extraction microservices.
Context
- Professional Services Practice: {{advisory_practice_unit}}
- Execution Runtime & Framework: {{runtime_environment}}
- OOM Crash Dump Summary: {{oom_crash_dump_summary}}
- Target Ingestion Throughput: {{target_throughput_tps}}
- Worker Concurrency Limit: {{worker_concurrency_limit}}
- Container Memory Ceiling: {{memory_ceiling_mb}}
Task
Design an end-to-end debugging and runtime heap profiling script for {{runtime_environment}} that isolates memory leaks during massive document parsing runs in {{advisory_practice_unit}}, triggers automated heap snapshots before {{memory_ceiling_mb}} exhaustion, and enforces graceful worker recycling.
Method
- Analyze {{oom_crash_dump_summary}} to identify uncollected retained objects, closure leaks, and buffer allocation bottlenecks.
- Construct an active runtime memory observer that tracks RSS, heapUsed, and external buffer allocations on a 250ms cadence.
- Integrate trigger logic to generate V8/Python heap profiles automatically when memory consumption hits 85% of {{memory_ceiling_mb}}.
- Build an asynchronous stream pipeline interceptor that enforces backpressure whenever {{target_throughput_tps}} overwhelms parser GC cycles.
- Implement a supervisor worker management script that gracefully drains in-flight client document chunks before cycling saturated worker processes.
- Generate a heap diff analysis routine to pinpoint lingering object retainers across consecutive document conversion batches.
- Provide automated load-generation commands to simulate peak concurrency up to {{worker_concurrency_limit}}.
Constraints
- Profiling hooks MUST NOT add more than 3% CPU overhead during normal steady-state document parsing.
- The supervisor script MUST ensure zero document chunk drops during graceful worker recycling.
- Do not use non-standard native compilation dependencies unless pre-bundled in {{runtime_environment}}.
- Snapshots and logs MUST NOT write unencrypted client document text to shared disk partitions.
Output format
- Leak Diagnostic Assessment: Concise breakdown of the retention tree causing memory bloat.
- Production Profiler Script (
heap_monitor.jsorheap_monitor.py): Full script with event emitters, threshold listeners, and snapshot dumps. - Cluster Supervisor Configuration (
cluster_manager.jsorsupervisor.py): Zero-downtime worker lifecycle script. - Post-Mortem Verification Guide: 4-step checklist to validate memory stability under load.
Self-review
- Is backpressure correctly handled during document buffer transformation?
- Does the worker recycle routine drain in-flight promises/tasks before terminating the process?
- Are memory snapshots securely written to restricted temporary paths?
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.