@hyperfrontend/builder/memory

memory

Opt-in memory monitor and the always-on recover() event-loop yield.

createMemoryMonitor(options?) instruments long-running build phases with snapshot history, threshold-based warnings (high heap, critical heap, step-over-step growth), and a summary line covering peak heap, peak RSS, snapshot count, and elapsed wall-clock time. The companion recover() utility yields control to the event loop and triggers a manual GC cycle when the host process is started with --expose-gc; call it between memory-heavy phases to drain pending I/O microtasks and reclaim transient allocations before the next phase begins.

API Reference

ƒ Functions

§function

createMemoryMonitor(options: MemoryMonitorOptions): MemoryMonitor

Creates an opt-in memory monitor backed by process.memoryUsage().
The monitor records snapshot history, emits threshold warnings when check() is called, and is intended for instrumenting long-running build phases. All thresholds default to safe values when omitted.

Parameters

NameTypeDescription
§options
MemoryMonitorOptions
Optional threshold overrides. Each field defaults to a value appropriate for builder workloads (warning 512 MB, critical 768 MB, growth 50 MB).
(default: {})

Returns

MemoryMonitor
A frozen MemoryMonitor with snapshot, check, logDebug, logSummary, and getSnapshots methods.

Example

Recording snapshots between phases

const monitor = createMemoryMonitor({ warningMB: 256, criticalMB: 512, growthMB: 32 })
monitor.check('bundle:start')
await runBundlePhase(ctx, config)
monitor.check('bundle:end')
monitor.logSummary()
§function

recover(): Promise<void>

Yields control to the event loop and triggers a manual garbage-collection cycle when globalThis.gc is available (Node.js started with --expose-gc).
This is the always-on free utility companion to the opt-in memory monitor. Call it between memory-heavy phases to drain pending I/O microtasks and reclaim transient allocations before the next phase begins.

Returns

Promise<void>
A promise that resolves after one macrotask tick, post-GC if available.

Example

Yielding between heavy build phases

await runBundlePhase(ctx, config)
await recover()
await runPackagePhase(ctx, config)

Interfaces

§interface

MemoryMonitor

Monitor instance returned by createMemoryMonitor.

Properties

§check:(label: string) => MemorySnapshot
Capture a snapshot and emit warnings when configured thresholds are crossed.
§getSnapshots:() => MemorySnapshot[]
Returns a copy of every snapshot captured so far.
§logDebug:(label: string) => MemorySnapshot
Capture a snapshot and emit a debug-level log line summarizing it.
§logSummary:() => void
Emit an info-level summary covering every snapshot taken so far.
§snapshot:(label: string) => MemorySnapshot
Capture a snapshot, append it to the history, and return it.
§interface

MemorySnapshot

Single memory measurement captured by the monitor.
Sizes are normalized to megabytes (1 MB = 1024 * 1024 bytes) so callers can compare directly against the configured thresholds.

Properties

§externalMB:number
process.memoryUsage().external in MB.
§heapTotalMB:number
process.memoryUsage().heapTotal in MB.
§heapUsedMB:number
process.memoryUsage().heapUsed in MB.
§label:string
Caller-supplied label identifying the capture site.
§rssMB:number
process.memoryUsage().rss in MB.
§timestampMs:number
Wall-clock timestamp (ms since epoch) when the snapshot was taken.