From 8a3e8704bbd1a98133fa6af9fb0de29f1d9de35d Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:42:10 -0600 Subject: [PATCH 1/2] Enforce the Node heap cap across live allocations. --- docs/node.md | 2 +- src/jvm.ts | 2 ++ src/node.ts | 10 +++++++++- src/runtime.ts | 31 +++++++++++++++++++++++++++++-- test/java/HeapCap.java | 18 ++++++++++++++++++ test/programs.test.ts | 38 +++++++++++++++++++++++--------------- 6 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 test/java/HeapCap.java diff --git a/docs/node.md b/docs/node.md index f7ea5d8..71cd8df 100644 --- a/docs/node.md +++ b/docs/node.md @@ -19,7 +19,7 @@ npx @project516/java-web [options] -jar app.jar [args] | `--enable-preview` | preview features | | `--add-exports`, `--add-opens`, `--add-reads`, `--add-modules`, `--module-path`, `-p` | module options, as with `java` | | `--locale-data` | locales other than English (see [support.md](support.md#locales)) | -| `-Xmx` | the largest array, and what `Runtime.maxMemory()` reports (default and at most 1g, rounded up to 2 MB steps, below 2m an error); the total heap is not counted | +| `-Xmx` | the maximum heap reported by `Runtime.maxMemory()` (at most 1g, rounded up to 2 MB steps, below 2m an error). Under Node, java-web checks measured heap use periodically during allocation and throws `OutOfMemoryError: Java heap space` when live use stays over the cap after collection. Without an exposed garbage collector, it checks after estimated allocations exceed the cap. The browser still checks only individual array sizes. | | `--max-rss MB` | exit when resident memory passes this (default 2048) | | `--jdk DIR` | a java-web JDK image instead of the packaged one | | `--debug` | log VM events | diff --git a/src/jvm.ts b/src/jvm.ts index 589c381..7d93308 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -86,6 +86,7 @@ export interface JvmOptions { drop: (key: string) => void; }; warmSnapshot?: boolean; + afterBoot?: () => void; } // what a resumed thread body yields first, before the run under it returns @@ -1326,6 +1327,7 @@ export class JVM { const set = system.methodMap.get("setProperty(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")!; yield t.call(set, this.str("sun.java.command"), this.str(cmd)); } + this.opts.afterBoot?.(); } private *mainGen(t: JThread, mode: number, what: string, args: string[], restored: boolean): NativeGen { diff --git a/src/node.ts b/src/node.ts index 2cd89fc..69a1efd 100644 --- a/src/node.ts +++ b/src/node.ts @@ -18,7 +18,7 @@ import { ImageBackend, imageFd } from "./image.ts"; import { NodeNet } from "./net-node.ts"; import { processNatives } from "./process-node.ts"; import { PendingThrow } from "./thread.ts"; -import { setMaxHeap } from "./runtime.ts"; +import { clearAllocationCheck, hasMaxHeap, MAX_HEAP, resetAllocationCheck, setAllocationCheck, setMaxHeap } from "./runtime.ts"; import type { NetBackend } from "./sockets.ts"; // A host directory exposed read-only (or writable) inside the VFS. @@ -409,6 +409,13 @@ function nodeVm(o: NodeRunOptions): JVM { const toVfs = (p: string) => path.resolve(p); const classpath = o.jar ? [toVfs(o.jar)] : o.classpath.map(toVfs); const natives = o.natives ?? {}; + if (hasMaxHeap()) { + const gc = (globalThis as { gc?: () => void }).gc; + setAllocationCheck(MAX_HEAP, () => { + const m = process.memoryUsage(); + return m.heapUsed + m.arrayBuffers; + }, gc); + } else clearAllocationCheck(); const bridge = new Bridge({ call: (_, req) => vm.library.request(req), release: (_, ids) => vm.library.release(ids), @@ -450,6 +457,7 @@ function nodeVm(o: NodeRunOptions): JVM { process.stdin.on("end", onEnd); process.stdin.resume(); }), + afterBoot: resetAllocationCheck, }); vm.registerNatives(processNatives(fileURLToPath(import.meta.url), jdk, packaged, o.input === undefined)); bridges.set(vm, bridge); diff --git a/src/runtime.ts b/src/runtime.ts index 2803362..e070508 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -161,6 +161,7 @@ export class JObject { constructor(cls: JClass, f: any[]) { this.cls = cls; this.f = f; + if (allocationCheck?.enabled) allocated(32); } } @@ -182,6 +183,7 @@ export class JArray extends JObject { constructor(cls: JClass, a: ArrayData) { super(cls, EMPTY); this.a = a; + if (allocationCheck?.enabled) allocated(ArrayBuffer.isView(a) ? a.byteLength : a.length * 4); } } @@ -372,11 +374,36 @@ export function primName(c: string) { throw new Error(`not a primitive: ${c}`); } -// Reported by Runtime.maxMemory; no array may be bigger. -Xmx lowers it; the -// total of live objects is not counted. +// Reported by Runtime.maxMemory and the per-array limit. Node adds a sampled +// live-heap check when -Xmx is set; browser allocations keep the per-array cap. export let MAX_HEAP = 1 << 30; +let explicitMaxHeap = false; export function setMaxHeap(bytes: number) { MAX_HEAP = Math.min(bytes, 1 << 30); + explicitMaxHeap = true; +} +export function hasMaxHeap() { return explicitMaxHeap; } +let allocationCheck: { cap: number; step: number; allocated: number; baseline: number; enabled: boolean; usage: () => number; gc?: () => void } | null = null; +export function setAllocationCheck(cap: number, usage: () => number, gc?: () => void) { + allocationCheck = { cap, step: Math.max(4 << 20, cap / 16), allocated: 0, baseline: 0, enabled: false, usage, gc }; +} +export function clearAllocationCheck() { allocationCheck = null; } +export function resetAllocationCheck() { + if (!allocationCheck) return; + allocationCheck.baseline = allocationCheck.usage(); + allocationCheck.allocated = 0; + allocationCheck.enabled = true; +} +function allocated(bytes: number) { + const check = allocationCheck; + if (!check?.enabled || bytes <= 0) return; + check.allocated += bytes; + if (check.gc ? check.allocated < check.step : check.allocated <= check.cap) return; + check.allocated = 0; + if (check.usage() - check.baseline > check.cap) { + check.gc?.(); + if (check.usage() - check.baseline > check.cap) throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); + } } const ELEM_SIZE: Record = { Z: 1, B: 1, C: 2, S: 2, I: 4, F: 4, J: 8, D: 8, L: 4 }; diff --git a/test/java/HeapCap.java b/test/java/HeapCap.java new file mode 100644 index 0000000..bc7b569 --- /dev/null +++ b/test/java/HeapCap.java @@ -0,0 +1,18 @@ +// java: -Xmx32m +import java.util.ArrayList; +import java.util.List; + +public class HeapCap { + public static void main(String[] args) { + List keep = new ArrayList<>(); + try { + while (true) keep.add(new byte[256 * 1024]); + } catch (OutOfMemoryError e) { + System.out.println("OutOfMemoryError: " + e.getMessage()); + keep.clear(); + System.gc(); + } + for (int i = 0; i < 16; i++) keep.add(new byte[256 * 1024]); + System.out.println("continued"); + } +} diff --git a/test/programs.test.ts b/test/programs.test.ts index fba1488..62dd60a 100644 --- a/test/programs.test.ts +++ b/test/programs.test.ts @@ -32,22 +32,30 @@ for (const file of files) { const ref = spawnSync(hotspot("java"), [...vmFlags, "-cp", out, main], { input, encoding: "utf8" }); const stdout: Uint8Array[] = []; const stderr: Uint8Array[] = []; - // module options go in as the properties HotSpot's launcher makes of them - const opts = { classpath: [out], props: {}, enablePreview: false }; - const assertions: string[] = []; - for (let i = 0; i < vmFlags.length; i++) { - if (vmFlags[i] === "--enable-preview") opts.enablePreview = true; - else if (!moduleFlag(opts, vmFlags[i], vmFlags[i + 1])) assertions.push(vmFlags[i]); - else if (!vmFlags[i].includes("=")) i++; + let code: number; + if (vmFlags.some((f) => f.startsWith("-Xmx"))) { + const got = spawnSync(process.execPath, [join(root, "src", "node.ts"), ...vmFlags, "-cp", out, main], { input, encoding: "utf8" }); + stdout.push(new TextEncoder().encode(got.stdout)); + stderr.push(new TextEncoder().encode(got.stderr)); + code = got.status ?? 1; + } else { + // module options go in as the properties HotSpot's launcher makes of them + const opts = { classpath: [out], props: {}, enablePreview: false }; + const assertions: string[] = []; + for (let i = 0; i < vmFlags.length; i++) { + if (vmFlags[i] === "--enable-preview") opts.enablePreview = true; + else if (!moduleFlag(opts, vmFlags[i], vmFlags[i + 1])) assertions.push(vmFlags[i]); + else if (!vmFlags[i].includes("=")) i++; + } + code = await runNode({ + ...opts, + mainClass: main, + assertions, + stdout: (b) => stdout.push(b), + stderr: (b) => stderr.push(b), + input: input === undefined ? undefined : new TextEncoder().encode(input), + }); } - const code = await runNode({ - ...opts, - mainClass: main, - assertions, - stdout: (b) => stdout.push(b), - stderr: (b) => stderr.push(b), - input: input === undefined ? undefined : new TextEncoder().encode(input), - }); const text = (parts: Uint8Array[]) => Buffer.concat(parts).toString("utf8"); // one comparison, so a failure shows stderr and the exit code along with stdout assert.deepEqual( From ea564b26aeb628170db40dffdd1a77994f967c25 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:04:10 -0600 Subject: [PATCH 2/2] Count references at 8 bytes and check at least once per cap of allocation --- src/runtime.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime.ts b/src/runtime.ts index e070508..4e0ca53 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -183,7 +183,7 @@ export class JArray extends JObject { constructor(cls: JClass, a: ArrayData) { super(cls, EMPTY); this.a = a; - if (allocationCheck?.enabled) allocated(ArrayBuffer.isView(a) ? a.byteLength : a.length * 4); + if (allocationCheck?.enabled) allocated(ArrayBuffer.isView(a) ? a.byteLength : a.length * 8); } } @@ -385,7 +385,7 @@ export function setMaxHeap(bytes: number) { export function hasMaxHeap() { return explicitMaxHeap; } let allocationCheck: { cap: number; step: number; allocated: number; baseline: number; enabled: boolean; usage: () => number; gc?: () => void } | null = null; export function setAllocationCheck(cap: number, usage: () => number, gc?: () => void) { - allocationCheck = { cap, step: Math.max(4 << 20, cap / 16), allocated: 0, baseline: 0, enabled: false, usage, gc }; + allocationCheck = { cap, step: Math.min(cap, Math.max(4 << 20, cap / 16)), allocated: 0, baseline: 0, enabled: false, usage, gc }; } export function clearAllocationCheck() { allocationCheck = null; } export function resetAllocationCheck() {