Skip to content

Commit f6fe81c

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) decides whether to run its memory reducer from the young, old and embedder (cppgc) allocation rates. Ghost never allocates on the cppgc heap, so the embedder rate is exactly zero, which V8 treats as the lowest possible mutator utilisation, i.e. "high allocation". The reducer therefore never runs after a burst of traffic and post-load heap is only released by its 100s watchdog, or not at all while full GCs keep resetting it. On the Ghost(Pro) performance benchmark, Node 24 held 519MB RSS 60s after load versus 202MB on Node 22, with the same live heap. vm.Script is cppgc-backed in Node 24+, so allocating one at boot and every 30s keeps the embedder rate non-zero; with that, Node 24 settled at 205MB with no change in load CPU or latency. This avoids forced GCs (188-270ms pauses on the same benchmark) and V8 flag changes such as --memory-saver-mode (60-75% more CPU under load). It only runs on V8 13+, so Node 22 is unaffected, and should be removed once the upstream V8 fix reaches our supported Node versions.
1 parent 82d2b08 commit f6fe81c

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎ghost/core/core/boot.js‎

Lines changed: 4 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);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Script } from 'node:vm';
2+
3+
// V8 13+ reads a cppgc allocation rate of exactly zero as high allocation, so its memory
4+
// reducer never releases heap after load. vm.Script is cppgc-backed; allocating one
5+
// periodically keeps the rate non-zero. Remove once fixed upstream:
6+
// 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+
}

0 commit comments

Comments
 (0)