AIdeazz Blog About Portfolio

Detecting Production Memory Creep Across PM2 Supervised Node Services

· by

A process running with zero restarts is not proof of stability. On my host, pm2 jlist reports 8 online processes out of 9, but the memory footprint across those instances tells a story that restart counts actively conceal. If you only alert on process crashes, you miss the silent accumulation of state until your host runs out of room.

I run background workers and agent dispatchers on a single cloud instance. When I pull runtime figures, I compare memory footprints directly against supervisor restart counters to identify silent state retention before it takes down an instance.

Comparing Worker Memory Footprints Under Supervisor Monitoring

When inspecting our supervisor table via pm2 jlist, the raw numbers expose massive divergence across workloads with identical uptimes:

A service like dragontrade-dashboard holds flat at 77 MB with 0 restarts after 1d online. Lean pollers such as algom-poll remain steady at 79 MB. Meanwhile, n8n runs at 396 MB with 0 restarts. The high footprint in visual automation engines often reflects the baseline weight of execution environments rather than immediate failure, but it establishes a baseline that requires constant tracking.

The more pressing signal appears in cto-aipa. It registers 227 MB alongside 6 restarts over the same 1d span. A service accumulating restarts while still holding 227 MB points toward retained state, in-memory buffers, or active churn during event handling. In contrast, algom-stream collapsed into a failure loop with 4343 restarts, showing what happens when supervisor boundaries fail to recover normal execution.

Tracking State Retention and Git Activity in Active Dispatchers

The cto-aipa worker manages interactive operations, outbound drafts, and channel learning. Over the last 48h, the repository logged 12 commits. Frequent code updates alter operational memory dynamically, especially when background workers do not clean up listener references or state dictionaries across hot deployments.

Recent commits in cto-aipa reveal why state management is brittle:

When features like draft persistence across restarts and real-time channel learning are introduced, in-memory representations of messages and metadata can slip outside garbage collection scopes. If drafts persist on disk, yet active node processes cache pending objects in memory without aggressive TTLs, memory drifts upward. The 227 MB footprint in cto-aipa after 6 restarts demonstrates that supervisor restarts act as an emergency valve, but they do not solve root-level retention.

Pipeline Downstream Logs and Upstream Saturation

Memory leaks and unbounded process growth do not occur in isolation; they are often catalyzed by backpressure from external APIs and stalled job queues. When upstream requests block or get throttled, intermediate data structures pile up in memory.

Log inspections across our services demonstrate this operational pressure:

When a rate limiter fires an HTTP 429 or an external socket returns an EOF error, the calling code must avoid buffering retries indefinitely. If an outbound polling loop queues events during an API stall without hard capacity caps, node process memory expands silently.

Implementing Concrete Process Memory Creep Production Monitoring

To prevent services from consuming all available memory while appearing healthy with 0 restarts, monitoring must evaluate drift alongside restarts.

First, establish memory ceiling limits within supervisor configurations. For node workers managed under PM2, configure an explicit max_memory_restart ceiling. A worker like algom-poll requires under 80 MB, while dragontrade-main operates around 182 MB. Allowing workers to run unbounded means a service like cto-aipa can climb past 227 MB without generating an alert.

Second, separate expected platform baselines from operational creep. An instance of n8n idling at 396 MB with 0 restarts is functioning within its expected runtime profile for visual workflow orchestration. However, a custom TypeScript agent that starts at 80 MB and drifts toward 227 MB across 6 restarts indicates unhandled event listeners, disk draft caching overlaps, or unreleased object pools.

Third, monitor queue exhaustion and disk handoffs. In our environment, commit fbaf3d21 explicitly moved pending drafts to disk across restarts. Offloading transient state to the filesystem prevents an in-memory accumulation pattern when external services block or rate limits pause transmission.

Fourth, track scheduled operator jobs that run outside the supervisor. Our shared operational queue in NOW.md records scheduled ad-hoc scripts:
⏰ 25 Nov ~11:00 UTC Claude Code: a one-off scheduled job is WAITING on Oracle — DO NOT KILL. A plain bash -c sleep … ; node news_sweep_20261126.cjs in ~/nexus-gap/ (not PM2, no cron) fires once on 27 Nov 02:00 UTC, runs one Grok news search and Telegrams Elena via the existing bot token, then exits.

Running unscheduled or one-off scripts outside supervisor oversight consumes memory that supervisor tables cannot account for. System metrics must inspect total host capacity rather than only summing supervised process memory.

Frequently Asked Questions

Q: How does a supervisor restart hide an active memory leak?
A: When a supervisor restarts a crashing or oversized process, the immediate resident set size drops back down to the baseline. In cto-aipa, observing 6 restarts alongside 227 MB indicates the process is cyclically recovering from terminations rather than maintaining steady-state execution.

Q: Why does n8n show 396 MB with 0 restarts while algom-poll consumes only 79 MB?
A: The difference stems from runtime scope and architecture. The algom-poll process is a lean, focused background worker, whereas n8n loads an entire workflow engine, UI backplane, and execution canvas within its single Node environment.

Q: What operational risks emerge when external APIs return HTTP 429 during automated runs?
A: As seen in hs-watch-manual-emails.log with the ten_secondly_rolling limit error, repeated 429 responses force workers to hold active retry objects in memory. Without strict buffer eviction, retry queues cause memory drift in the client process.

Q: When should an operator offload state from process memory to disk?
A: As implemented in cto-aipa commit fbaf3d21, state such as drafts or channel metadata should persist directly to disk whenever workers are expected to restart or recover cleanly across maintenance cycles.

— Elena Revicheva · AIdeazz AI Lab · Portfolio · Professional Outlook (PDF)