Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions src/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because hasMaxHeap() relies on the one-way explicitMaxHeap flag, a VM started without -Xmx after one with -Xmx incorrectly enters this branch and installs an allocation check using the stale MAX_HEAP. The else clearAllocationCheck() branch is never reached for subsequent VMs. This should be fixed together with the explicitMaxHeap reset.

const gc = (globalThis as { gc?: () => void }).gc;
setAllocationCheck(MAX_HEAP, () => {
const m = process.memoryUsage();
return m.heapUsed + m.arrayBuffers;
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
}, gc);
} else clearAllocationCheck();
const bridge = new Bridge({
call: (_, req) => vm.library.request(req),
release: (_, ids) => vm.library.release(ids),
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 29 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class JObject {
constructor(cls: JClass, f: any[]) {
this.cls = cls;
this.f = f;
if (allocationCheck?.enabled) allocated(32);
}
}

Expand All @@ -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 * 8);
}
}

Expand Down Expand Up @@ -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;
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitMaxHeap is set to true in setMaxHeap (line 383) but never reset to false. If a process creates a VM with -Xmx (setting explicitMaxHeap = true) and later creates a VM without -Xmx, hasMaxHeap() still returns true and nodeVm enables the allocation check with the previous MAX_HEAP value. This violates the documented behavior that "No -Xmx: no check". Reset explicitMaxHeap when setMaxHeap is called without a cap, or add a clearMaxHeap function called when a VM starts without -Xmx.

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.min(cap, 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;
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
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<string, number> = { Z: 1, B: 1, C: 2, S: 2, I: 4, F: 4, J: 8, D: 8, L: 4 };

Expand Down
18 changes: 18 additions & 0 deletions test/java/HeapCap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// java: -Xmx32m
import java.util.ArrayList;
import java.util.List;

public class HeapCap {
public static void main(String[] args) {
List<byte[]> 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");
}
}
38 changes: 23 additions & 15 deletions test/programs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading