Skip to content

Commit 46a2f50

Browse files
committed
Fixed heap not being released after load on Node 24+
ref https://issues.chromium.org/issues/570738027 ref nodejs/node#66564 V8 13+ (Node 24 and 26) only takes the memory reducer's normal low-allocation path when the young, old and embedder (cppgc) allocation rates are all low, and it treats a sampled cppgc throughput of exactly zero as high allocation. Ghost's workload leaves that throughput at zero, so after a burst of traffic the reducer waits for its 100s watchdog instead of releasing heap within seconds as it does on Node 22. While full GCs keep resetting the watchdog, it may not run at all. vm.Script is cppgc-backed in Node 24+, so allocating one at boot and every 30s keeps the sampled throughput non-zero and restores the normal path. On one Ghost(Pro) performance benchmark run, Node 24 settled at 205MB RSS 60s after load with this in place, versus 519MB without it and 202MB on Node 22, with no load CPU or latency regression observed. Forced idle GCs (188-270ms pauses on the same benchmark) and --memory-saver-mode (60-75% more CPU under load) were rejected as alternatives. The keepalive only runs on V8 13+, so Node 22 is unaffected, stops with the server's cleanup tasks, and should be removed once the V8 fix lands in our supported Node versions.
1 parent 82d2b08 commit 46a2f50

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎ghost/core/core/boot.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ async function bootGhost({ backend = true, frontend = true, server = true } = {}
677677
logging.error('Unhandled rejection:', error);
678678
});
679679
debug('End: Add unhandled rejection handler');
680+
681+
debug('Begin: Start memory reducer keepalive');
682+
require('./server/lib/memory-reducer-keepalive').startMemoryReducerKeepalive();
683+
debug('End: Start memory reducer keepalive');
680684
} catch (error) {
681685
console.error(error); // eslint-disable-line no-console
682686
process.exit(1);
@@ -714,6 +718,10 @@ async function bootGhost({ backend = true, frontend = true, server = true } = {}
714718
prometheusClient.stop();
715719
}
716720
}, 'Prometheus client');
721+
722+
ghostServer.registerCleanupTask(async () => {
723+
require('./server/lib/memory-reducer-keepalive').stopMemoryReducerKeepalive();
724+
}, 'Memory reducer keepalive');
717725
debug('End: load server + minimal app');
718726
}
719727

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Script } from 'node:vm';
2+
3+
// V8 13+ blocks the memory reducer's normal low-allocation path when sampled cppgc
4+
// throughput is exactly zero, which Ghost's workload leaves it at. vm.Script is
5+
// cppgc-backed; allocating one periodically keeps that throughput non-zero.
6+
// Remove once fixed upstream: https://issues.chromium.org/issues/570738027
7+
const INTERVAL_MS = 30_000;
8+
const FIRST_AFFECTED_V8_MAJOR = 13;
9+
10+
let timer: ReturnType<typeof setInterval> | undefined;
11+
12+
const allocateCppgcObject = () => new Script('0');
13+
14+
export function startMemoryReducerKeepalive(): void {
15+
if (timer || Number(process.versions.v8.split('.')[0]) < FIRST_AFFECTED_V8_MAJOR) {
16+
return;
17+
}
18+
19+
allocateCppgcObject();
20+
timer = setInterval(allocateCppgcObject, INTERVAL_MS).unref();
21+
}
22+
23+
export function stopMemoryReducerKeepalive(): void {
24+
clearInterval(timer);
25+
timer = undefined;
26+
}

0 commit comments

Comments
 (0)