From d705ad91893bc88eb5ed996e07942d0bfa009025 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:51:00 -0600 Subject: [PATCH 01/10] fix(gc): make Runtime.gc return once the engine has delivered the reference callbacks of its collection --- src/natives/lang.ts | 3 ++- src/natives/ref.ts | 25 +++++++++++++++++++++++-- test/java/RefsAfterGc.java | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 test/java/RefsAfterGc.java diff --git a/src/natives/lang.ts b/src/natives/lang.ts index 78b3484..f5ff000 100644 --- a/src/natives/lang.ts +++ b/src/natives/lang.ts @@ -7,6 +7,7 @@ import { normalize } from "../fs.ts"; import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData } from "../runtime.ts"; import type { Monitor } from "../runtime.ts"; import { canUnmount, dropOwned, unblockOnRelease } from "./continuation.ts"; +import { collect } from "./ref.ts"; import { Frame, JThread, JavaThrow, PendingThrow, STATUS_BLOCKED, STATUS_RUNNABLE, STATUS_SLEEPING, STATUS_TIMED_WAITING, STATUS_WAITING, Signal, TERMINATED, @@ -384,7 +385,7 @@ export const natives: Natives = { // values popped off an operand stack are not roots in HotSpot for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp); const t0 = performance.now(); - gc(); + yield t.block(collect(t.vm, gc)); stats.ms += performance.now() - t0; }, "java/lang/Shutdown.beforeHalt()V": () => undefined, diff --git a/src/natives/ref.ts b/src/natives/ref.ts index 2a11a5e..9d55e9c 100644 --- a/src/natives/ref.ts +++ b/src/natives/ref.ts @@ -13,10 +13,13 @@ import type { Natives } from "./util.ts"; const R = "java/lang/ref/Reference"; +// A held function is a collect() probe. +type Held = WeakRef | (() => void); + interface RefState { pending: JObject[]; waiters: (() => void)[]; - registry: FinalizationRegistry>; + registry: FinalizationRegistry; weak: JClass; phantom: JClass; } @@ -30,7 +33,8 @@ function refs(vm: JVM): RefState { // Holds the reference weakly: as in HotSpot, a reference that is itself // unreachable is never enqueued, and holding it would keep its referent // alive whenever the reference's other fields lead back to it. - registry: new FinalizationRegistry((held: WeakRef) => { + registry: new FinalizationRegistry((held: Held) => { + if (typeof held === "function") return held(); const ref = held.deref(); if (!ref || !(ref.x instanceof WeakRef)) return; ref.x = null; @@ -56,6 +60,23 @@ function clear(t: JThread, r: JObject) { t.vm.setField(r, R, "referent", null); } +const PROBE_MS = 50; +// Runs a collection and resolves once the engine has run the callbacks it +// queued: they come in later tasks, and a probe in the same registry arrives +// with them. The timer only bounds the wait. +export function collect(vm: JVM, gc: () => void): Promise { + const s = refs(vm); + return new Promise((done) => { + probe(s, done); + gc(); + setTimeout(done, PROBE_MS); + }); +} +// Its own frame: a temporary in the caller's frame would keep the probe alive. +function probe(s: RefState, done: () => void) { + s.registry.register({}, done); +} + // A weak or phantom reference restored from a snapshot: watch its referent // again, and put back the pending list. export function restoreReference(vm: JVM, self: JObject, obj: JObject) { diff --git a/test/java/RefsAfterGc.java b/test/java/RefsAfterGc.java new file mode 100644 index 0000000..d72db9e --- /dev/null +++ b/test/java/RefsAfterGc.java @@ -0,0 +1,32 @@ +import java.lang.ref.PhantomReference; +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +// Weak and phantom references whose referents System.gc() collects are +// cleared, and both are enqueued. +public class RefsAfterGc { + static WeakReference weak(ReferenceQueue q) { + return new WeakReference<>(new Object(), q); + } + + static PhantomReference phantom(ReferenceQueue q) { + return new PhantomReference<>(new Object(), q); + } + + public static void main(String[] args) throws InterruptedException { + ReferenceQueue q = new ReferenceQueue<>(); + WeakReference w = weak(q); + PhantomReference p = phantom(q); + System.gc(); + System.out.println("weak cleared " + (w.get() == null)); + System.out.println("phantom get " + p.get()); + int n = 0; + while (n < 2) { + Reference r = q.remove(10000); + if (r == null) break; + n++; + } + System.out.println("enqueued " + n); + } +} From 2c985dfdd2888df4f2ad407da75419d7cc957bfd Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:48:04 -0600 Subject: [PATCH 02/10] feat(gc): call finalize() on objects that nothing refers to any more --- docs/node.md | 2 +- src/indy.ts | 2 +- src/interp.ts | 5 +- src/jit.ts | 4 +- src/jvm.ts | 11 +- src/natives/lang.ts | 4 +- src/natives/ref.ts | 158 ++++++++++++++++++++++++---- src/node.ts | 4 + src/runtime.ts | 17 ++- src/snapshot.ts | 2 +- test/java/FinalizationDisabled.java | 27 +++++ test/java/FinalizerCycles.java | 37 +++++++ test/java/Finalizers.java | 90 ++++++++++++++++ test/programs.test.ts | 2 +- types/node.d.ts | 2 + 15 files changed, 337 insertions(+), 30 deletions(-) create mode 100644 test/java/FinalizationDisabled.java create mode 100644 test/java/FinalizerCycles.java create mode 100644 test/java/Finalizers.java diff --git a/docs/node.md b/docs/node.md index 677c9ad..4bb9031 100644 --- a/docs/node.md +++ b/docs/node.md @@ -51,7 +51,7 @@ resolves to the exit code. Its options: - `stdout`, `stderr` (byte callbacks; default the process's), `input` (fixed stdin bytes) - `env` (what `System.getenv()` returns; default the host's environment) - `natives`: Java `native` methods in JS, as in the browser ([browser.md](browser.md)) -- `assertions` (the flags, for example `["-ea"]`), `enablePreview`, `localeData` +- `assertions` (the flags, for example `["-ea"]`), `enablePreview`, `localeData`, `finalization` (`false` is `--finalization=disabled`) - `hostName` (what `InetAddress.getLocalHost()` reports), `maxRssMb`, `jdk`, `debug` - `maxDirectMemory` (`-XX:MaxDirectMemorySize`, in bytes), `onOutOfMemoryError` (`-XX:OnOutOfMemoryError` commands), `vmOutputToStderr` (`-XX:+DisplayVMOutputToStderr`) diff --git a/src/indy.ts b/src/indy.ts index 1afbc6f..76e38ef 100644 --- a/src/indy.ts +++ b/src/indy.ts @@ -566,7 +566,7 @@ export function invokePolymorphic(vm: JVM, t: JThread, f: Frame, m: JMethod): bo let target = memberTarget(t, stack[f.sp - 1]); if (m.name === "linkToStatic" && target.cls.state !== INITIALIZED && !vm.initialize(t, target.cls)) return false; f.sp--; - if (m.name === "linkToVirtual" || m.name === "linkToInterface") { + if ((m.name === "linkToVirtual" || m.name === "linkToInterface") && !target.isPrivate) { const recv = stack[f.sp - target.argSlots] as JObject | null; if (recv === null) throw new PendingThrow("java/lang/NullPointerException"); const sel = recv.cls.selectMethod(target.key); diff --git a/src/interp.ts b/src/interp.ts index 70734eb..db365e3 100644 --- a/src/interp.ts +++ b/src/interp.ts @@ -10,6 +10,7 @@ import type { NativeGen } from "./thread.ts"; import type { JVM } from "./jvm.ts"; import { INTERPRET, PUSHED, compileIfHot, hooks, noteBackedge } from "./jit.ts"; import { unmountNative } from "./natives/continuation.ts"; +import { registerFinalizer } from "./natives/ref.ts"; const MAX_FRAMES = 6000; @@ -403,7 +404,9 @@ export function interpret(t: JThread, f: Frame, budget: number): number { if (c === undefined) { f.pc = ipc; f.sp = sp; return budget; } if (c.state !== INITIALIZED && !vm.initialize(t, c)) { f.pc = ipc; f.sp = sp; return budget; } if (c.access & 0x0600) throw new PendingThrow("java/lang/InstantiationError", c.javaName); - stack[sp++] = new JObject(c, c.template.slice()); + const o = new JObject(c, c.template.slice()); + if (c.finalizable) registerFinalizer(vm, o); + stack[sp++] = o; pc += 3; break; } diff --git a/src/jit.ts b/src/jit.ts index 492b6c3..1fea5ec 100644 --- a/src/jit.ts +++ b/src/jit.ts @@ -230,7 +230,7 @@ const runtime = { virtualTarget(t: JThread, mn: JObject, recv: JObject | null, pc: number): JMethod { const m = memberTarget(t, mn); if (recv === null) npeAt(pc); - const sel = recv.cls.selectMethod(m.key); + const sel = m.isPrivate ? m : recv.cls.selectMethod(m.key); if (!sel) throw new PendingThrow("java/lang/AbstractMethodError", `${recv.cls.javaName}.${m.name}${m.desc}`); return sel; }, @@ -1043,7 +1043,7 @@ function insnCode(g: Gen, b: Body, pc: number) { break; } const k = cls.rcp[idx2] as JClass | undefined; - if (!k || k.state !== INITIALIZED || k.access & 0x0600) { emit(slow(pc, d)); break; } + if (!k || k.state !== INITIALIZED || k.access & 0x0600 || k.finalizable) { emit(slow(pc, d)); break; } emit(`${S(d)}=new rt.JObject(${K(k)},${K(k)}.template.slice());`); break; } diff --git a/src/jvm.ts b/src/jvm.ts index 1438309..4ab07d5 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -21,6 +21,7 @@ import { import type { NativeGen, Request } from "./thread.ts"; import { intrinsics, natives as builtinNatives } from "./natives/index.ts"; import { canUnmount, dropOwned, unmountBlocked } from "./natives/continuation.ts"; +import { registerFinalizer } from "./natives/ref.ts"; import { invokePolymorphic, linkCallSite, resolveConstant } from "./indy.ts"; import { hostNative, jniNames } from "./hostnatives.ts"; import { Library } from "./library.ts"; @@ -67,6 +68,8 @@ export interface JvmOptions { assertions?: Assertions; // run class files that use this release's preview features enablePreview?: boolean; + // false: finalize() is never called and there is no Finalizer thread + finalization?: boolean; // class bytes from --patch-module for a boot loader module, which win over the image's bootPatch?: (module: string, name: string) => Uint8Array | null; // class bytes on the -Xbootclasspath/a path, and the path entry that holds them @@ -382,6 +385,8 @@ export class JVM { c.methods.push(m); c.methodMap.set(m.key, m); } + const fin = c.methodMap.get("finalize()V"); + c.finalizable = superclass?.finalizable === true || (fin !== undefined && !fin.isStatic && (fin.code?.code.length ?? 0) > 1); // Runtime members own the metadata now; discard the parsed member records. cf.fields = []; cf.methods = []; @@ -675,7 +680,9 @@ export class JVM { // ---- objects ------------------------------------------------------------- newObject(c: JClass): JObject { - return new JObject(c, c.template.slice()); + const o = new JObject(c, c.template.slice()); + if (c.finalizable) registerFinalizer(this, o); + return o; } newArray(component: JClass, n: number): JArray { @@ -1487,7 +1494,7 @@ export class JVM { } else { const self = this.newObject(main); yield t.call(main.methodMap.get("()V")!, self); - const m = main.selectMethod(key)!; + const m = main.selectMethod(key) ?? main.methodMap.get(key)!; yield noArgs ? t.call(m, self) : t.call(m, self, jargs); } } catch (e) { diff --git a/src/natives/lang.ts b/src/natives/lang.ts index f5ff000..867e88b 100644 --- a/src/natives/lang.ts +++ b/src/natives/lang.ts @@ -7,7 +7,7 @@ import { normalize } from "../fs.ts"; import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData } from "../runtime.ts"; import type { Monitor } from "../runtime.ts"; import { canUnmount, dropOwned, unblockOnRelease } from "./continuation.ts"; -import { collect } from "./ref.ts"; +import { collect, registerFinalizer, releaseFinalizerCycles } from "./ref.ts"; import { Frame, JThread, JavaThrow, PendingThrow, STATUS_BLOCKED, STATUS_RUNNABLE, STATUS_SLEEPING, STATUS_TIMED_WAITING, STATUS_WAITING, Signal, TERMINATED, @@ -204,6 +204,7 @@ export function cloneObject(t: JThread, o: JObject): JObject { } const copy = new JObject(o.cls, o.f.slice()); copy.x = o.x; + if (copy.cls.finalizable) registerFinalizer(t.vm, copy); return copy; } @@ -384,6 +385,7 @@ export const natives: Natives = { yield t.block(new Promise((r) => setTimeout(r, 0))); // values popped off an operand stack are not roots in HotSpot for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp); + releaseFinalizerCycles(t.vm); const t0 = performance.now(); yield t.block(collect(t.vm, gc)); stats.ms += performance.now() - t0; diff --git a/src/natives/ref.ts b/src/natives/ref.ts index 9d55e9c..397bc73 100644 --- a/src/natives/ref.ts +++ b/src/natives/ref.ts @@ -1,11 +1,13 @@ // java.lang.ref on the JS engine's collector. A weak or phantom reference // holds its referent through a JS WeakRef, so the referent can be collected; // a FinalizationRegistry then puts the reference on the pending list that the -// Reference Handler thread enqueues from. Soft and final references stay -// strong: HotSpot clears soft ones only under memory pressure. +// Reference Handler thread enqueues from. Soft references stay strong. An +// object whose class has a finalizer is watched too: its wrapper is collected, +// and the Finalizer gets a new wrapper over the same storage. -import { INITIALIZED } from "../runtime.ts"; -import type { JClass, JObject } from "../runtime.ts"; +import { INITIALIZED, wrapperOver } from "../runtime.ts"; +import { JObject } from "../runtime.ts"; +import type { JClass } from "../runtime.ts"; import { Signal } from "../thread.ts"; import type { JThread, NativeGen } from "../thread.ts"; import type { JVM } from "../jvm.ts"; @@ -13,15 +15,29 @@ import type { Natives } from "./util.ts"; const R = "java/lang/ref/Reference"; -// A held function is a collect() probe. -type Held = WeakRef | (() => void); +// What a finalizable object's new wrapper needs. It must not lead back to the +// object: f is null once its fields are found to lead back to it (see forgetCycles). +interface Fin { + fin: JObject; + cls: JClass; + f: any[] | null; + hash: number; + x: any; +} + +// A weak or phantom reference (its referent is watched), a finalizer, or a collect() probe. +type Held = WeakRef | Fin | (() => void); interface RefState { pending: JObject[]; waiters: (() => void)[]; + // finalizers whose objects are not known to be on a cycle + fins: Fin[]; + registered: number; registry: FinalizationRegistry; weak: JClass; phantom: JClass; + finalizer: JClass; } function refs(vm: JVM): RefState { @@ -30,25 +46,33 @@ function refs(vm: JVM): RefState { s = { pending: [], waiters: [], + fins: [], + registered: 0, // Holds the reference weakly: as in HotSpot, a reference that is itself // unreachable is never enqueued, and holding it would keep its referent // alive whenever the reference's other fields lead back to it. registry: new FinalizationRegistry((held: Held) => { if (typeof held === "function") return held(); + if ("cls" in held) return resurrect(vm, s!, held); const ref = held.deref(); if (!ref || !(ref.x instanceof WeakRef)) return; ref.x = null; - s!.pending.push(ref); - for (const w of s!.waiters.splice(0)) w(); + addPending(s!, ref); }), weak: vm.loadBoot("java/lang/ref/WeakReference")!, phantom: vm.loadBoot("java/lang/ref/PhantomReference")!, + finalizer: vm.loadBoot("java/lang/ref/Finalizer")!, }; vm.state.set("refs", s); } return s; } +function addPending(s: RefState, ref: JObject) { + s.pending.push(ref); + for (const w of s.waiters.splice(0)) w(); +} + function referent(t: JThread, r: JObject): JObject | null { if (r.x instanceof WeakRef) return r.x.deref() ?? null; return t.vm.getField(r, R, "referent"); @@ -60,6 +84,13 @@ function clear(t: JThread, r: JObject) { t.vm.setField(r, R, "referent", null); } +// A weak or phantom reference to obj; a reference with a queue is enqueued +// when obj is collected. +function watch(s: RefState, self: JObject, obj: JObject, queued: boolean) { + self.x = new WeakRef(obj); + if (queued) s.registry.register(obj, new WeakRef(self), self); +} + const PROBE_MS = 50; // Runs a collection and resolves once the engine has run the callbacks it // queued: they come in later tasks, and a probe in the same registry arrives @@ -77,13 +108,104 @@ function probe(s: RefState, done: () => void) { s.registry.register({}, done); } -// A weak or phantom reference restored from a snapshot: watch its referent -// again, and put back the pending list. +// The object is gone: the Finalizer's referent becomes a new wrapper over its +// storage, and the Reference Handler passes it on to finalize(). +function resurrect(vm: JVM, s: RefState, h: Fin) { + const fin = h.fin; + if (!(fin.x instanceof WeakRef)) return; + if (h.f === null) { + // nothing to finalize: the Finalizer thread unlinks it, and its finalize(null) fails quietly + fin.x = null; + return addPending(s, fin); + } + const o = wrapperOver(h.cls, h.f); + o.hash = h.hash; + o.x = h.x; + fin.x = null; + vm.setField(fin, R, "referent", o); + addPending(s, fin); +} + +// Links a Finalizer for obj into the unfinalized list, as Finalizer.register +// does for an object of a class with a finalizer. +export function registerFinalizer(vm: JVM, obj: JObject) { + const s = refs(vm); + if (s.finalizer.state !== INITIALIZED || vm.opts.finalization === false) return; + const F = "java/lang/ref/Finalizer"; + const fin = vm.newObject(s.finalizer); + vm.setField(fin, R, "queue", vm.getStatic(s.finalizer, "queue")); + const head = vm.getStatic(s.finalizer, "unfinalized") as JObject | null; + if (head) { + vm.setField(fin, F, "next", head); + vm.setField(head, F, "prev", fin); + } + vm.setStatic(s.finalizer, "unfinalized", fin); + watchFinalizer(vm, s, fin, obj); + if (++s.registered % CHECK_EVERY === 0) forgetCycles(s, SCAN_WHILE_RUNNING); +} + +// The registry holds a finalizable object's fields, so an object they lead back +// to would never be collected, finalized or not. JS cannot finalize such an +// object (that takes tracing from the roots), so it is released instead, as if +// it had no finalizer: the fields of an object found on a cycle are dropped. +const CHECK_EVERY = 2048, SCAN_PER_OBJECT = 2000, SCAN_WHILE_RUNNING = 20000, SCAN_AT_GC = 200000; + +function leadsBack(w: JObject, f: any[], budget: { left: number }): boolean { + const seen = new Set(); + const todo: any[][] = [f]; + let scanned = 0; + while (todo.length) { + for (const v of todo.pop()!) { + if (!(v instanceof JObject)) continue; + if (v === w) return true; + if (seen.has(v)) continue; + seen.add(v); + if (++scanned > SCAN_PER_OBJECT) { + budget.left -= scanned; + return false; + } + todo.push(v.f); + const a = (v as { a?: unknown }).a; + if (Array.isArray(a)) todo.push(a); + } + } + budget.left -= scanned; + return false; +} + +function forgetCycles(s: RefState, scan: number) { + const budget = { left: scan }; + const list = s.fins; + const kept: Fin[] = []; + let i = 0; + for (; i < list.length && budget.left > 0; i++) { + const h = list[i]; + const w = h.f !== null && h.fin.x instanceof WeakRef ? h.fin.x.deref() : undefined; + if (w === undefined) continue; + if (leadsBack(w, h.f!, budget)) h.f = null; + else kept.push(h); + } + s.fins = list.slice(i).concat(kept); +} + +export function releaseFinalizerCycles(vm: JVM) { + forgetCycles(refs(vm), SCAN_AT_GC); +} + +function watchFinalizer(vm: JVM, s: RefState, fin: JObject, obj: JObject) { + fin.x = new WeakRef(obj); + const h: Fin = { fin, cls: obj.cls, f: obj.f, hash: vm.identityHash(obj), x: obj.x }; + s.fins.push(h); + s.registry.register(obj, h, fin); +} + +// A weak, phantom or final reference restored from a snapshot: watch its +// referent again. export function restoreReference(vm: JVM, self: JObject, obj: JObject) { const s = refs(vm); - self.x = new WeakRef(obj); + if (self.cls === s.finalizer) return watchFinalizer(vm, s, self, obj); const none = vm.getStatic(vm.loadBoot("java/lang/ref/ReferenceQueue")!, "NULL_QUEUE"); - if (vm.getField(self, R, "queue") !== none) s.registry.register(obj, new WeakRef(self), self); + watch(s, self, obj, vm.getField(self, R, "queue") !== none); } export function restorePending(vm: JVM, pending: JObject[]) { @@ -97,13 +219,11 @@ export const intrinsics: Natives = { const rq = vm.loadBoot("java/lang/ref/ReferenceQueue")!; if (rq.state !== INITIALIZED) yield* vm.initGen(t, rq); const none = vm.getStatic(rq, "NULL_QUEUE"); - const q = queue ?? none; - vm.setField(self, R, "queue", q); + vm.setField(self, R, "queue", queue ?? none); const s = refs(vm); - if (obj !== null && (self.cls.isSubclassOf(s.weak) || self.cls.isSubclassOf(s.phantom))) { - self.x = new WeakRef(obj); - if (q !== none) s.registry.register(obj, new WeakRef(self), self); - } else vm.setField(self, R, "referent", obj); + const c = self.cls; + if (obj !== null && (c.isSubclassOf(s.weak) || c.isSubclassOf(s.phantom))) watch(s, self, obj, queue !== null && queue !== none); + else vm.setField(self, R, "referent", obj); }, [`${R}.get()Ljava/lang/Object;`]: (t, [self]) => referent(t, self), }; @@ -124,6 +244,6 @@ export const natives: Natives = { const s = refs(t.vm); while (s.pending.length === 0) yield t.block(new Signal((r) => s.waiters.push(r))); }, - "java/lang/ref/Finalizer.isFinalizationEnabled()Z": () => 0, + "java/lang/ref/Finalizer.isFinalizationEnabled()Z": (t) => (t.vm.opts.finalization === false ? 0 : 1), "java/lang/ref/Finalizer.reportComplete(Ljava/lang/Object;)V": () => undefined, }; diff --git a/src/node.ts b/src/node.ts index 6a17023..ed29faa 100644 --- a/src/node.ts +++ b/src/node.ts @@ -343,6 +343,8 @@ export interface NodeRunOptions { // the process exits when its resident memory goes past this maxRssMb?: number; enablePreview?: boolean; + // --finalization=disabled leaves finalize() uncalled (true by default) + finalization?: boolean; // -XX:OnOutOfMemoryError commands, run through /bin/sh on the first OutOfMemoryError the VM throws onOutOfMemoryError?: string[]; // -XX:+DisplayVMOutputToStderr: the VM's own messages go to stderr, not stdout @@ -615,6 +617,7 @@ export function nodeVm(o: LauncherOptions): JVM { debug: o.debug, assertions: o.assertions && parseAssertions(o.assertions), enablePreview: o.enablePreview, + finalization: o.finalization, bootClassPath: bootClassPath(o.props?.["jdk.boot.class.path.append"]), bootPatch: bootPatches(o.props), osVersion: os.release(), @@ -767,6 +770,7 @@ export function main(argv: string[]) { else if (moduleFlag(o, a, argv[i + 1])) i += a.includes("=") ? 0 : 1; else if (isAssertionFlag(a)) (o.assertions ??= []).push(a); else if (a === "--enable-preview") o.enablePreview = true; + else if (a === "--finalization=enabled" || a === "--finalization=disabled") o.finalization = a === "--finalization=enabled"; else if (a === "--locale-data") o.localeData = true; else if (a === "-version") version = true; else if (a.startsWith("-Xmx")) { diff --git a/src/runtime.ts b/src/runtime.ts index d8e114c..065b69e 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -260,6 +260,9 @@ export class JClass { // descriptor char for primitive classes prim: string | null = null; hidden = false; + // instances are registered for finalization: a non-empty finalize() here or + // in a superclass + finalizable = false; // members the VM added to a class it spun, which reflection does not show vmMembers: Set | null = null; nestHost: JClass | null = null; @@ -374,7 +377,7 @@ export class JClass { m = null; for (let c: JClass | null = this; c; c = c.superclass) { const d = c.methodMap.get(key); - if (d && !d.isStatic && !(d.isPrivate && c !== this)) { m = d; break; } + if (d && !d.isStatic && !d.isPrivate) { m = d; break; } } if (!m || m.isAbstract) { const d = this.maximallySpecific(key); @@ -432,6 +435,18 @@ export function setAllocationCheck(cap: number, usage: () => number, o: { gc?: ( allocationCheck = { cap, step: Math.min(cap, Math.max(4 << 20, cap / 16)), allocated: 0, baseline: 0, enabled: false, over: false, usage, gc: o.gc, lazy: !!o.lazy }; } export function clearAllocationCheck() { allocationCheck = null; } +// A JObject made while a collection hands a finalizable object to its Finalizer: +// it is not an allocation that can fail. +export function wrapperOver(cls: JClass, f: any[]): JObject { + const check = allocationCheck; + const was = check?.enabled; + if (check) check.enabled = false; + try { + return new JObject(cls, f); + } finally { + if (check) check.enabled = was!; + } +} export function resetAllocationCheck() { if (!allocationCheck) return; allocationCheck.baseline = allocationCheck.usage(); diff --git a/src/snapshot.ts b/src/snapshot.ts index 0d1670d..8b6f05a 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -95,7 +95,7 @@ export function snapshotKey(vm: JVM, build: string): string { if (i >= 0 && i % 2 === 0) vmProps[i + 1] = ""; const o = vm.opts; const modules = [...new Set(vm.bootPackages.values())].sort(); - return JSON.stringify([build, modules, platformProperties(vm), vmProps, o.assertions ?? null, !!o.enablePreview, MAX_HEAP]); + return JSON.stringify([build, modules, platformProperties(vm), vmProps, o.assertions ?? null, !!o.enablePreview, o.finalization !== false, MAX_HEAP]); } const WAIT0 = "java/lang/Object.wait0(J)V"; diff --git a/test/java/FinalizationDisabled.java b/test/java/FinalizationDisabled.java new file mode 100644 index 0000000..9b79fe0 --- /dev/null +++ b/test/java/FinalizationDisabled.java @@ -0,0 +1,27 @@ +// java: --finalization=disabled +// With finalization disabled there is no Finalizer thread and finalize() is not called. +public class FinalizationDisabled { + static volatile boolean finalized; + + @Override + protected void finalize() { + finalized = true; + } + + static void make() { + new FinalizationDisabled(); + } + + public static void main(String[] args) throws Exception { + make(); + for (int i = 0; i < 20; i++) { + System.gc(); + System.runFinalization(); + Thread.sleep(10); + } + boolean thread = false; + for (Thread t : Thread.getAllStackTraces().keySet()) if (t.getName().equals("Finalizer")) thread = true; + System.out.println("finalizer thread " + thread); + System.out.println("finalized " + finalized); + } +} diff --git a/test/java/FinalizerCycles.java b/test/java/FinalizerCycles.java new file mode 100644 index 0000000..04a0263 --- /dev/null +++ b/test/java/FinalizerCycles.java @@ -0,0 +1,37 @@ +// Garbage with a finalizer on a reference cycle does not pile up. +public class FinalizerCycles { + static class Child { + Object parent; + byte[] pad = new byte[200]; + } + + static class Node { + Child child = new Child(); + Node() { + child.parent = this; + } + + @Override + protected void finalize() {} + } + + static long used() { + Runtime r = Runtime.getRuntime(); + return r.totalMemory() - r.freeMemory(); + } + + public static void main(String[] args) throws Exception { + long first = 0, last = 0; + for (int round = 0; round < 5; round++) { + for (int i = 0; i < 30000; i++) new Node(); + for (int k = 0; k < 5; k++) { + System.gc(); + System.runFinalization(); + Thread.sleep(20); + } + if (round == 1) first = used(); + last = used(); + } + System.out.println("flat " + (last - first < (16 << 20))); + } +} diff --git a/test/java/Finalizers.java b/test/java/Finalizers.java new file mode 100644 index 0000000..6582887 --- /dev/null +++ b/test/java/Finalizers.java @@ -0,0 +1,90 @@ +import java.lang.ref.WeakReference; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +// Objects whose class has a finalizer are finalized once unreachable, with +// their fields and identity hash, after weak references to them are cleared. +// An object that finalize() makes reachable again is finalized only once. +public class Finalizers { + static final Map hashes = new ConcurrentHashMap<>(); + static final Map finalized = new ConcurrentHashMap<>(); + static final Map> weaks = new ConcurrentHashMap<>(); + static final AtomicInteger runs = new AtomicInteger(); + static volatile Revived resurrected; + + static class Item { + final int id; + final String name; + + Item(int id, String name) { + this.id = id; + this.name = name; + weaks.put(id, new WeakReference<>(this)); + } + + @Override + protected void finalize() { + String hash = hashes.get(id) == System.identityHashCode(this) ? "kept" : "changed"; + finalized.put(id, name + " weak " + weaks.get(id).get() + " hash " + hash); + runs.incrementAndGet(); + } + } + + static class Child extends Item { + Child(int id) { + super(id, "child"); + } + + @Override + protected void finalize() { + super.finalize(); + runs.incrementAndGet(); + } + } + + static class Empty extends Item { + Empty(int id) { + super(id, "empty"); + } + + @Override + protected void finalize() {} + } + + static class Revived { + final int id = 4; + + @Override + protected void finalize() { + resurrected = this; + finalized.put(id, "revived"); + runs.incrementAndGet(); + } + } + + // garbage is made in a helper so no live frame still holds it + static void make() { + for (Item i : new Item[] {new Item(1, "plain"), new Child(2), new Empty(3)}) hashes.put(i.id, System.identityHashCode(i)); + new Revived(); + } + + public static void main(String[] args) throws Exception { + make(); + for (int i = 0; i < 200 && runs.get() < 4; i++) { + System.gc(); + System.runFinalization(); + Thread.sleep(20); + } + System.out.println("revived " + resurrected.id); + resurrected = null; + for (int i = 0; i < 100; i++) { + System.gc(); + System.runFinalization(); + Thread.sleep(10); + } + for (Map.Entry e : new TreeMap<>(finalized).entrySet()) System.out.println(e.getKey() + " " + e.getValue()); + System.out.println("runs " + runs.get()); + } +} diff --git a/test/programs.test.ts b/test/programs.test.ts index 5649691..4ac383c 100644 --- a/test/programs.test.ts +++ b/test/programs.test.ts @@ -34,7 +34,7 @@ for (const file of files) { const stdout: Uint8Array[] = []; const stderr: Uint8Array[] = []; let code: number; - if (vmFlags.some((f) => /^-(Xmx|XX:(OnOutOfMemoryError|MaxDirectMemorySize)=)/.test(f))) { + if (vmFlags.some((f) => /^-(Xmx|XX:(OnOutOfMemoryError|MaxDirectMemorySize)=)|^--finalization=/.test(f))) { 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)); diff --git a/types/node.d.ts b/types/node.d.ts index fa6e26a..af7f5fd 100644 --- a/types/node.d.ts +++ b/types/node.d.ts @@ -24,6 +24,8 @@ export interface NodeRunOptions { // the process exits when its resident memory goes past this maxRssMb?: number; enablePreview?: boolean; + // --finalization=disabled leaves finalize() uncalled (true by default) + finalization?: boolean; // -XX:OnOutOfMemoryError commands, run through /bin/sh on the first OutOfMemoryError the VM throws onOutOfMemoryError?: string[]; // -XX:+DisplayVMOutputToStderr: the VM's own messages go to stderr, not stdout From 7c584c6a47a58f7dabd04684180fdc25666aba43 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:48:04 -0600 Subject: [PATCH 03/10] feat(gc): clear soft references before an OutOfMemoryError and when memory is short --- src/natives/lang.ts | 3 +- src/natives/management.ts | 12 ++++++++ src/natives/ref.ts | 63 ++++++++++++++++++++++++++++++++------- src/node.ts | 32 ++++++++++++++------ src/runtime.ts | 31 +++++++++++++++++-- test/java/SoftRefs.java | 45 ++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+), 24 deletions(-) create mode 100644 test/java/SoftRefs.java diff --git a/src/natives/lang.ts b/src/natives/lang.ts index 867e88b..e0a43c0 100644 --- a/src/natives/lang.ts +++ b/src/natives/lang.ts @@ -7,7 +7,7 @@ import { normalize } from "../fs.ts"; import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData } from "../runtime.ts"; import type { Monitor } from "../runtime.ts"; import { canUnmount, dropOwned, unblockOnRelease } from "./continuation.ts"; -import { collect, registerFinalizer, releaseFinalizerCycles } from "./ref.ts"; +import { collect, registerFinalizer, releaseFinalizerCycles, releaseSoftReferences } from "./ref.ts"; import { Frame, JThread, JavaThrow, PendingThrow, STATUS_BLOCKED, STATUS_RUNNABLE, STATUS_SLEEPING, STATUS_TIMED_WAITING, STATUS_WAITING, Signal, TERMINATED, @@ -385,6 +385,7 @@ export const natives: Natives = { yield t.block(new Promise((r) => setTimeout(r, 0))); // values popped off an operand stack are not roots in HotSpot for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp); + releaseSoftReferences(t.vm); releaseFinalizerCycles(t.vm); const t0 = performance.now(); yield t.block(collect(t.vm, gc)); diff --git a/src/natives/management.ts b/src/natives/management.ts index 86b6ec1..e555bcd 100644 --- a/src/natives/management.ts +++ b/src/natives/management.ts @@ -73,6 +73,18 @@ export function javaHeap(): { used: number; committed: number } | null { return { used: Math.min(used, committed), committed }; } +// The engine's own heap use and limit, unclamped; null where it cannot tell. +export function engineHeap(): { used: number; limit: number } | null { + const g = globalThis as any; + const node = g.process?.memoryUsage?.(); + if (node) { + const v8 = g.process.getBuiltinModule?.("node:v8"); + return v8 ? { used: node.heapUsed + node.arrayBuffers, limit: v8.getHeapStatistics().heap_size_limit } : null; + } + const chrome = g.performance?.memory; + return chrome ? { used: chrome.usedJSHeapSize, limit: chrome.jsHeapSizeLimit } : null; +} + function* beans(t: JThread): NativeGen { const vm = t.vm; let b = vm.state.get("mgmtBeans") as Beans | undefined; diff --git a/src/natives/ref.ts b/src/natives/ref.ts index 397bc73..9ae6269 100644 --- a/src/natives/ref.ts +++ b/src/natives/ref.ts @@ -1,16 +1,18 @@ -// java.lang.ref on the JS engine's collector. A weak or phantom reference +// java.lang.ref on the JS engine's collector. A weak, soft or phantom reference // holds its referent through a JS WeakRef, so the referent can be collected; // a FinalizationRegistry then puts the reference on the pending list that the -// Reference Handler thread enqueues from. Soft references stay strong. An -// object whose class has a finalizer is watched too: its wrapper is collected, -// and the Finalizer gets a new wrapper over the same storage. +// Reference Handler thread enqueues from. A soft referent is also held by a +// table that is emptied when memory runs short. An object whose class has a +// finalizer is watched too: its wrapper is collected, and the Finalizer gets a +// new wrapper over the same storage. -import { INITIALIZED, wrapperOver } from "../runtime.ts"; +import { INITIALIZED, MAX_HEAP, freeHeap, hasMaxHeap, setReclaim, wrapperOver } from "../runtime.ts"; import { JObject } from "../runtime.ts"; import type { JClass } from "../runtime.ts"; import { Signal } from "../thread.ts"; import type { JThread, NativeGen } from "../thread.ts"; import type { JVM } from "../jvm.ts"; +import { engineHeap } from "./management.ts"; import type { Natives } from "./util.ts"; const R = "java/lang/ref/Reference"; @@ -25,7 +27,7 @@ interface Fin { x: any; } -// A weak or phantom reference (its referent is watched), a finalizer, or a collect() probe. +// A reference whose referent is watched, a finalizer, or a collect() probe. type Held = WeakRef | Fin | (() => void); interface RefState { @@ -37,7 +39,11 @@ interface RefState { registry: FinalizationRegistry; weak: JClass; phantom: JClass; + soft: JClass; finalizer: JClass; + // soft reference -> referent: held while the reference is alive, until memory runs short + softs: WeakMap; + softCount: number; } function refs(vm: JVM): RefState { @@ -61,8 +67,12 @@ function refs(vm: JVM): RefState { }), weak: vm.loadBoot("java/lang/ref/WeakReference")!, phantom: vm.loadBoot("java/lang/ref/PhantomReference")!, + soft: vm.loadBoot("java/lang/ref/SoftReference")!, finalizer: vm.loadBoot("java/lang/ref/Finalizer")!, + softs: new WeakMap(), + softCount: 0, }; + setReclaim(() => clearSoftReferences(vm)); vm.state.set("refs", s); } return s; @@ -79,18 +89,49 @@ function referent(t: JThread, r: JObject): JObject | null { } function clear(t: JThread, r: JObject) { - if (r.x instanceof WeakRef) refs(t.vm).registry.unregister(r); + if (r.x instanceof WeakRef) { + const s = refs(t.vm); + s.registry.unregister(r); + s.softs.delete(r); + } r.x = null; t.vm.setField(r, R, "referent", null); } -// A weak or phantom reference to obj; a reference with a queue is enqueued -// when obj is collected. +// A weak, soft or phantom reference to obj; a reference with a queue is +// enqueued when obj is collected. function watch(s: RefState, self: JObject, obj: JObject, queued: boolean) { self.x = new WeakRef(obj); + if (self.cls.isSubclassOf(s.soft)) { + s.softs.set(self, obj); + s.softCount++; + } if (queued) s.registry.register(obj, new WeakRef(self), self); } +// Lets the engine collect the soft referents nothing else reaches. Returns +// whether there were any soft references. +function clearSoftReferences(vm: JVM): boolean { + const s = refs(vm); + if (s.softCount === 0) return false; + s.softs = new WeakMap(); + s.softCount = 0; + return true; +} + +function memoryShort(): boolean { + if (hasMaxHeap()) return freeHeap() < MAX_HEAP / 2; + const h = engineHeap(); + return h !== null && h.used > h.limit / 2; +} + +// HotSpot clears soft references when memory is short: here, on System.gc() +// with more than half of the heap limit in use. +export function releaseSoftReferences(vm: JVM) { + if (refs(vm).softCount === 0 || !memoryShort()) return; + clearSoftReferences(vm); +} + const PROBE_MS = 50; // Runs a collection and resolves once the engine has run the callbacks it // queued: they come in later tasks, and a probe in the same registry arrives @@ -199,7 +240,7 @@ function watchFinalizer(vm: JVM, s: RefState, fin: JObject, obj: JObject) { s.registry.register(obj, h, fin); } -// A weak, phantom or final reference restored from a snapshot: watch its +// A weak, soft, phantom or final reference restored from a snapshot: watch its // referent again. export function restoreReference(vm: JVM, self: JObject, obj: JObject) { const s = refs(vm); @@ -222,7 +263,7 @@ export const intrinsics: Natives = { vm.setField(self, R, "queue", queue ?? none); const s = refs(vm); const c = self.cls; - if (obj !== null && (c.isSubclassOf(s.weak) || c.isSubclassOf(s.phantom))) watch(s, self, obj, queue !== null && queue !== none); + if (obj !== null && (c.isSubclassOf(s.weak) || c.isSubclassOf(s.phantom) || c.isSubclassOf(s.soft))) watch(s, self, obj, queue !== null && queue !== none); else vm.setField(self, R, "referent", obj); }, [`${R}.get()Ljava/lang/Object;`]: (t, [self]) => referent(t, self), diff --git a/src/node.ts b/src/node.ts index ed29faa..7dca092 100644 --- a/src/node.ts +++ b/src/node.ts @@ -7,7 +7,8 @@ import * as os from "node:os"; import { isMainThread } from "node:worker_threads"; import * as path from "node:path"; import { deserialize, serialize, setFlagsFromString } from "node:v8"; -import { runInNewContext } from "node:vm"; +import { createContext, runInContext, runInNewContext } from "node:vm"; +import type { Context } from "node:vm"; import { fileURLToPath, pathToFileURL } from "node:url"; import { bootClassPath, bootPatches } from "./bootpath.ts"; import { VFS } from "./fs.ts"; @@ -385,16 +386,29 @@ function fixedInput(data: Uint8Array) { const bridges = new WeakMap(); // The host file system is the VFS root, with the JDK image mounted at /jdk. -// System.gc() calls globalThis.gc, which node defines only under --expose-gc +// System.gc() calls globalThis.gc, which node defines only under --expose-gc. +// The engine keeps a WeakRef's target alive until the job that made or read it +// ends, so a collection from inside running Java code would miss what Java +// just dropped; a microtask checkpoint of another context ends the job. +let jobEnd: Context | null = null; function exposeGc() { const g = globalThis as { gc?: unknown }; - if (typeof g.gc === "function") return; - // V8 aborts on a flag change once flags are frozen - if (/(^|\s)--freeze-flags-after-init\b/.test([...process.execArgv, process.env.NODE_OPTIONS ?? ""].join(" "))) return; - setFlagsFromString("--expose-gc"); - // absent when --expose-gc-as gave it another name - const gc = runInNewContext("typeof gc === 'function' ? gc : undefined"); - if (gc) g.gc = gc; + if (typeof g.gc !== "function") { + // V8 aborts on a flag change once flags are frozen + if (/(^|\s)--freeze-flags-after-init\b/.test([...process.execArgv, process.env.NODE_OPTIONS ?? ""].join(" "))) return; + setFlagsFromString("--expose-gc"); + // absent when --expose-gc-as gave it another name + const gc = runInNewContext("typeof gc === 'function' ? gc : undefined"); + if (!gc) return; + g.gc = gc; + } + const gc = g.gc as (() => void) & { endsJob?: true }; + if (gc.endsJob) return; + const endingJob = Object.assign(() => { + runInContext("0", (jobEnd ??= createContext({}, { microtaskMode: "afterEvaluate" }))); + gc(); + }, { endsJob: true as const }); + g.gc = endingJob; } // Once the reader goes away (a closed pipe), writes fail with an IOException as diff --git a/src/runtime.ts b/src/runtime.ts index 065b69e..931d477 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -435,6 +435,15 @@ export function setAllocationCheck(cap: number, usage: () => number, o: { gc?: ( allocationCheck = { cap, step: Math.min(cap, Math.max(4 << 20, cap / 16)), allocated: 0, baseline: 0, enabled: false, over: false, usage, gc: o.gc, lazy: !!o.lazy }; } export function clearAllocationCheck() { allocationCheck = null; } +// Lets go of what the VM holds for the program's sake (soft referents) before an +// OutOfMemoryError; returns whether there was any. +let reclaim: (() => boolean) | null = null; +export function setReclaim(f: () => boolean) { reclaim = f; } +// An allocation that cannot succeed still collects first, as HotSpot does, so +// the program sees soft references cleared. +function collectBeforeGivingUp() { + if (reclaim?.()) (allocationCheck?.gc ?? (globalThis as { gc?: () => void }).gc)?.(); +} // A JObject made while a collection hands a finalizable object to its Finalizer: // it is not an allocation that can fail. export function wrapperOver(cls: JClass, f: any[]): JObject { @@ -447,6 +456,11 @@ export function wrapperOver(cls: JClass, f: any[]): JObject { if (check) check.enabled = was!; } } +// Java heap bytes still free under the cap, measured as the cap is. +export function freeHeap(): number { + const check = allocationCheck; + return check?.enabled ? MAX_HEAP - (check.usage() - check.baseline) : MAX_HEAP; +} export function resetAllocationCheck() { if (!allocationCheck) return; allocationCheck.baseline = allocationCheck.usage(); @@ -509,7 +523,12 @@ function allocated(bytes: number) { if (check.allocated < (check.gc || check.lazy ? check.step : check.cap)) return; check.allocated = 0; if (check.usage() - check.baseline > check.cap) check.gc?.(); - const over = check.usage() - check.baseline > check.cap; + let over = check.usage() - check.baseline > check.cap; + // soft references go before an OutOfMemoryError, as in HotSpot + if (over && reclaim?.() && check.gc) { + check.gc(); + over = check.usage() - check.baseline > check.cap; + } if (!over) { check.over = false; return; @@ -545,7 +564,10 @@ function store(bytes: number): [ArrayBuffer, number] | null { export function newArrayData(elem: string, n: number): ArrayData { if (n > 0x7ffffffd) throw new PendingThrow("java/lang/OutOfMemoryError", "Requested array size exceeds VM limit"); const bytes = n * ELEM_SIZE[elem]; - if (bytes > MAX_HEAP) throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); + if (bytes > MAX_HEAP) { + collectBeforeGivingUp(); + throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); + } const at = elem === "L" ? null : store(bytes); try { switch (elem) { @@ -560,7 +582,10 @@ export function newArrayData(elem: string, n: number): ArrayData { } } catch (e) { // the JS engine's own size limits, below MAX_HEAP for object arrays - if (e instanceof RangeError) throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); + if (e instanceof RangeError) { + collectBeforeGivingUp(); + throw new PendingThrow("java/lang/OutOfMemoryError", "Java heap space"); + } throw e; } } diff --git a/test/java/SoftRefs.java b/test/java/SoftRefs.java new file mode 100644 index 0000000..68c4ea0 --- /dev/null +++ b/test/java/SoftRefs.java @@ -0,0 +1,45 @@ +// java: -Xmx16m +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; +import java.util.ArrayList; +import java.util.List; + +// Soft references survive System.gc() while memory is free, and are cleared +// before an OutOfMemoryError, except those whose referents are strongly reachable. +public class SoftRefs { + static final int BLOCKS = 32; + + static int live(SoftReference[] refs) { + int n = 0; + for (SoftReference r : refs) if (r.get() != null) n++; + return n; + } + + @SuppressWarnings("unchecked") + static SoftReference[] fill(ReferenceQueue queue) { + SoftReference[] refs = new SoftReference[BLOCKS]; + for (int i = 0; i < BLOCKS; i++) refs[i] = new SoftReference<>(new byte[32768], queue); + return refs; + } + + public static void main(String[] args) throws Exception { + ReferenceQueue queue = new ReferenceQueue<>(); + SoftReference[] refs = fill(queue); + byte[] kept = new byte[32768]; + SoftReference keptRef = new SoftReference<>(kept, queue); + System.gc(); + System.out.println("after gc " + live(refs)); + + List chain = new ArrayList<>(); + try { + while (true) chain.add(new int[65536]); + } catch (OutOfMemoryError e) { + chain = null; + } + System.out.println("after memory ran out " + live(refs)); + System.out.println("kept " + (keptRef.get() == kept)); + int enqueued = 0; + while (queue.remove(2000) != null) enqueued++; + System.out.println("enqueued " + enqueued); + } +} From a311ac297b53e5e3f549685b805211a095537059 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:48:05 -0600 Subject: [PATCH 04/10] feat(gc): unload classes and hidden classes of class loaders that nothing refers to --- src/interp.ts | 14 +++++-- src/jit.ts | 21 +++++++--- src/jvm.ts | 18 +++++++- src/natives/lang.ts | 3 +- src/natives/loader.ts | 3 +- src/natives/ref.ts | 7 +++- src/runtime.ts | 52 +++++++++++++++++++++-- test/java/ClassUnload.java | 84 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 185 insertions(+), 17 deletions(-) create mode 100644 test/java/ClassUnload.java diff --git a/src/interp.ts b/src/interp.ts index db365e3..3f07d0e 100644 --- a/src/interp.ts +++ b/src/interp.ts @@ -1,7 +1,7 @@ // Bytecode interpreter and the per-thread run loop. import { - INITIALIZED, JArray, JClass, JMethod, JObject, isWide, longToFloat, newArrayData, sched, + INITIALIZED, JArray, JClass, JMethod, JObject, isWide, longToFloat, newArrayData, pin, sched, } from "./runtime.ts"; import { Frame, JThread, JavaThrow, MAX_DIRECT, NativeFrame, PendingThrow, RUNNABLE, TERMINATED, WAITING, fromNative, @@ -375,12 +375,20 @@ export function interpret(t: JThread, f: Frame, budget: number): number { target = vm.specialTarget(cls, target); } else if (!target.isPrivate) { const cache = m.cache ??= []; - const ic = cache[ipc]; + let ic = cache[ipc]; if (ic !== undefined && ic.c === o.cls) target = ic.m; else { const sel = o.cls.selectMethod(target.key); if (!sel) throw new PendingThrow("java/lang/AbstractMethodError", `${o.cls.javaName}.${target.name}${target.desc}`); - cache[ipc] = { c: o.cls, m: sel }; + if (ic === undefined) cache[ipc] = ic = { c: o.cls, m: sel, p: false }; + else { + ic.c = o.cls; + ic.m = sel; + } + if (o.cls.unloadable && !ic.p) { + ic.p = true; + pin(ic); + } target = sel; } } diff --git a/src/jit.ts b/src/jit.ts index 1fea5ec..5240d4e 100644 --- a/src/jit.ts +++ b/src/jit.ts @@ -16,7 +16,7 @@ import { cpB, cpTag, cpValue, Tag, utf8 } from "./classfile.ts"; import { - ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc, + ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc, pin, } from "./runtime.ts"; import { memberTarget } from "./natives/invoke.ts"; import { concatStrings } from "./natives/strings.ts"; @@ -74,6 +74,12 @@ export function restoreJit(vm: JVM, m: JMethod, code: SavedJit) { m.hot = THRESHOLD - 1; } +// A constant of compiled code that holds a class another loader can drop. +function namesUnloadable(v: unknown, caller: JClass): boolean { + const c = v instanceof JClass ? v : v instanceof JMethod || v instanceof JField ? v.cls : v instanceof JObject && v.x instanceof JClass ? v.x : null; + return c !== null && c !== caller && c.unloadable && (c.hidden || c.loader !== caller.loader); +} + function compile(t: JThread, m: JMethod) { if (disabled || m.noJit || !m.code || m.name === "" || m.code.code.length > MAX_CODE) return; const vm = t.vm; @@ -85,6 +91,7 @@ function compile(t: JThread, m: JMethod) { if (vm.keepJit && !stats && !noInline) vm.jitCode.set(m, code); } const { src, consts } = code; + if (consts.some((v) => namesUnloadable(v, m.cls))) pin(m); if (dump && m.toString().includes(dump)) console.error(`// ${m}\n${src}`); m.jit = new Function("rt", "K", src)(runtime, consts) as Compiled; if (stats) stats.set(`compiled ${m}`, src.length); @@ -348,7 +355,7 @@ const runtime = { }, fcmp(a: number, b: number, nan: number) { return a > b ? 1 : a < b ? -1 : a === b ? 0 : nan; }, lcmp(a: bigint, b: bigint) { return a > b ? 1 : a < b ? -1 : 0; }, - // cell: the inline cache, [receiver class, method, method key] + // cell: the inline cache, [receiver class, method, method key, registered by pin] select(o: JObject | null, cell: any[], pc: number): JMethod { if (o === null) return runtime.npe(pc); if (cell[0] === o.cls) return cell[1]; @@ -357,6 +364,10 @@ const runtime = { if (!m) throw new PendingThrow("java/lang/AbstractMethodError", `${o.cls.javaName}.${key}`); cell[0] = o.cls; cell[1] = m; + if (o.cls.unloadable && !cell[3]) { + cell[3] = true; + pin(cell); + } return m; }, isGen(r: any) { return r !== null && typeof r === "object" && typeof r.next === "function" && typeof r.throw === "function"; }, @@ -1146,7 +1157,7 @@ function regionInvoke(g: Gen, b: Body, op: number, pc: number, d: number, target let guard: JClass | null = null; if (op === 0xb9 || !isFixed(op, callee)) { const site = b.m.cache?.[pc]; - if (!site || !site.m.code || site.m.isAbstract || site.m.polymorphic) throw new NoInline(); + if (!site?.m?.code || site.m.isAbstract || site.m.polymorphic) throw new NoInline(); callee = site.m; guard = site.c; } @@ -1314,7 +1325,7 @@ function genInvoke( let seen = false; if (guard !== null && !target.isPrivate) { const site = g.top.m.cache?.[pc]; - if (site && site.m.code && !site.m.isAbstract && !site.m.polymorphic) { + if (site?.m?.code && !site.m.isAbstract && !site.m.polymorphic) { callee = site.m; guard = site.c; seen = true; @@ -1345,7 +1356,7 @@ function genInvoke( if (op === 0xb6 || op === 0xb9) { if (target.isPrivate) parts.push(`tm=${K(target)};${known ? "" : `if(${args[0]}===null)np(${pc});`}`); else { - const cell = K([null, null, target.key]); + const cell = K([null, null, target.key, false]); parts.push(`ic=${cell};tm=${args[0]}!==null&&${args[0]}.cls===ic[0]?ic[1]:rt.select(${args[0]},ic,${pc});`); } } else if (op === 0xb7) { diff --git a/src/jvm.ts b/src/jvm.ts index 4ab07d5..22cb552 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -11,7 +11,7 @@ import { JImage } from "./jimage.ts"; import { ACC_ABSTRACT, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_SUPER, ACC_VARARGS, ERRONEOUS, INITIALIZED, INITIALIZING, JArray, JClass, JField, - JMethod, JObject, LINKED, Monitor, defaultValue, newArrayData, noteYield, sched, + JMethod, JObject, LINKED, Monitor, defaultValue, dropsWithLoader, newArrayData, noteYield, sched, } from "./runtime.ts"; import type { CallSite, NativeFn } from "./runtime.ts"; import { @@ -261,6 +261,20 @@ export class JVM { return (loader.x ??= { classes: new Map(), modules: new Map() }); } + // A hidden class defined STRONG lives as long as its loader; the others, as long as they are reachable. + private strongHidden = new WeakMap(); + private bootStrongHidden: JClass[] = []; + keepWithLoader(loader: JObject | null, c: JClass) { + c.unloadable = dropsWithLoader(loader); + if (loader === null) { + this.bootStrongHidden.push(c); + return; + } + const list = this.strongHidden.get(loader); + if (list) list.push(c); + else this.strongHidden.set(loader, [c]); + } + findLoaded(loader: JObject | null, name: string): JClass | undefined { return this.loaderClasses(loader).get(name); } @@ -337,6 +351,7 @@ export class JVM { a.superclass = this.loadBoot("java/lang/Object"); a.interfaces = [this.loadBoot("java/lang/Cloneable")!, this.loadBoot("java/io/Serializable")!]; a.state = INITIALIZED; + a.unloadable = c.unloadable; c.arrayClass = a; this.loaderClasses(c.loader).set(name, a); this.created?.push(a); @@ -353,6 +368,7 @@ export class JVM { const c = new JClass(name, loader, cf, cf.access); this.classCount++; c.hidden = hidden; + if (hidden) c.unloadable = true; c.superclass = superclass; c.interfaces = interfaces; let slot = superclass ? superclass.instanceSlots : 0; diff --git a/src/natives/lang.ts b/src/natives/lang.ts index e0a43c0..9b0273a 100644 --- a/src/natives/lang.ts +++ b/src/natives/lang.ts @@ -4,7 +4,7 @@ import { isHidden } from "./stackwalk.ts"; import { gcStats, javaHeap } from "./management.ts"; import { assertionsEnabled } from "../assertions.ts"; import { normalize } from "../fs.ts"; -import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData } from "../runtime.ts"; +import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData, releasePins } from "../runtime.ts"; import type { Monitor } from "../runtime.ts"; import { canUnmount, dropOwned, unblockOnRelease } from "./continuation.ts"; import { collect, registerFinalizer, releaseFinalizerCycles, releaseSoftReferences } from "./ref.ts"; @@ -387,6 +387,7 @@ export const natives: Natives = { for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp); releaseSoftReferences(t.vm); releaseFinalizerCycles(t.vm); + releasePins(250); const t0 = performance.now(); yield t.block(collect(t.vm, gc)); stats.ms += performance.now() - t0; diff --git a/src/natives/loader.ts b/src/natives/loader.ts index 8bfc3a2..d645098 100644 --- a/src/natives/loader.ts +++ b/src/natives/loader.ts @@ -7,7 +7,7 @@ import { jstr, u8 } from "./util.ts"; import type { Natives } from "./util.ts"; const CL = "java/lang/ClassLoader"; -const HIDDEN_CLASS = 0x2, NESTMATE_CLASS = 0x1; +const HIDDEN_CLASS = 0x2, NESTMATE_CLASS = 0x1, STRONG_LOADER_LINK = 0x4; function internalName(t: JThread, name: JObject | null): string | null { return name === null ? null : jstr(t, name)!.replace(/\./g, "/"); @@ -70,6 +70,7 @@ export const natives: Natives = { const vm = t.vm; const hidden = (flags & HIDDEN_CLASS) !== 0; const c = yield* vm.defineClassGen(t, loader, u8(b, off, len).slice(), internalName(t, name), pd, hidden); + if (hidden && flags & STRONG_LOADER_LINK) vm.keepWithLoader(loader, c); if (flags & NESTMATE_CLASS) { c.nestHost = yield* nestHostOf(t, vm.classOf(lookup)); } diff --git a/src/natives/ref.ts b/src/natives/ref.ts index 9ae6269..2f24467 100644 --- a/src/natives/ref.ts +++ b/src/natives/ref.ts @@ -6,7 +6,7 @@ // finalizer is watched too: its wrapper is collected, and the Finalizer gets a // new wrapper over the same storage. -import { INITIALIZED, MAX_HEAP, freeHeap, hasMaxHeap, setReclaim, wrapperOver } from "../runtime.ts"; +import { INITIALIZED, MAX_HEAP, freeHeap, hasMaxHeap, releasePins, setReclaim, wrapperOver } from "../runtime.ts"; import { JObject } from "../runtime.ts"; import type { JClass } from "../runtime.ts"; import { Signal } from "../thread.ts"; @@ -72,7 +72,10 @@ function refs(vm: JVM): RefState { softs: new WeakMap(), softCount: 0, }; - setReclaim(() => clearSoftReferences(vm)); + setReclaim(() => { + const soft = clearSoftReferences(vm); + return releasePins() || soft; + }); vm.state.set("refs", s); } return s; diff --git a/src/runtime.ts b/src/runtime.ts index 931d477..acb247a 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -236,6 +236,46 @@ export class Monitor { waiters: { t: JThread; wake: () => void; count: number }[] = []; } +// Classes of the boot, platform and application loaders are never unloaded. +const BUILTIN_LOADERS = new Set(["jdk/internal/loader/ClassLoaders$AppClassLoader", "jdk/internal/loader/ClassLoaders$PlatformClassLoader"]); +export function dropsWithLoader(loader: JObject | null): boolean { + return loader !== null && !BUILTIN_LOADERS.has(loader.cls.name); +} + +// Inline caches and compiled code that name an unloadable class keep it, and +// its loader, alive. They are registered here and emptied before a collection. +const pins: WeakRef[] = []; +export function pin(o: JMethod | any[] | { c: JClass | null; m: JMethod | null; p: boolean }) { + pins.push(new WeakRef(o)); +} +// Returns whether there was anything to empty. With a gap, it does nothing +// within that many ms of the last time: compiled code is dropped, and a +// program that collects in a loop would compile it again each time. +let pinsReleased = 0; +export function releasePins(gapMs = 0): boolean { + const now = performance.now(); + if (now - pinsReleased < gapMs) return false; + pinsReleased = now; + const all = pins.splice(0); + let any = false; + for (const r of all) { + const o = r.deref(); + if (!o) continue; + any = true; + if (o instanceof JMethod) { + o.jit = null; + o.hot = 0; + } else if (Array.isArray(o)) { + o[0] = o[1] = null; + o[3] = false; + } else { + o.c = o.m = null; + o.p = false; + } + } + return any; +} + export class JClass { readonly name: string; readonly loader: JObject | null; @@ -260,6 +300,9 @@ export class JClass { // descriptor char for primitive classes prim: string | null = null; hidden = false; + // defined by a loader the VM can drop, or hidden: caches in other code must + // not keep it alive (see pin) + unloadable = false; // instances are registered for finalization: a non-empty finalize() here or // in a superclass finalizable = false; @@ -281,6 +324,7 @@ export class JClass { this.loader = loader; this.cf = cf; this.access = access; + this.unloadable = dropsWithLoader(loader); } get isInterface() { return (this.access & ACC_INTERFACE) !== 0; } @@ -435,12 +479,12 @@ export function setAllocationCheck(cap: number, usage: () => number, o: { gc?: ( allocationCheck = { cap, step: Math.min(cap, Math.max(4 << 20, cap / 16)), allocated: 0, baseline: 0, enabled: false, over: false, usage, gc: o.gc, lazy: !!o.lazy }; } export function clearAllocationCheck() { allocationCheck = null; } -// Lets go of what the VM holds for the program's sake (soft referents) before an -// OutOfMemoryError; returns whether there was any. +// Lets go of what the VM holds for the program's sake (soft referents, caches +// of unloadable classes) before an OutOfMemoryError; returns whether there was any. let reclaim: (() => boolean) | null = null; export function setReclaim(f: () => boolean) { reclaim = f; } // An allocation that cannot succeed still collects first, as HotSpot does, so -// the program sees soft references cleared. +// the program sees soft references cleared and unloaded classes gone. function collectBeforeGivingUp() { if (reclaim?.()) (allocationCheck?.gc ?? (globalThis as { gc?: () => void }).gc)?.(); } @@ -524,7 +568,7 @@ function allocated(bytes: number) { check.allocated = 0; if (check.usage() - check.baseline > check.cap) check.gc?.(); let over = check.usage() - check.baseline > check.cap; - // soft references go before an OutOfMemoryError, as in HotSpot + // soft references and unloadable classes go before an OutOfMemoryError, as in HotSpot if (over && reclaim?.() && check.gc) { check.gc(); over = check.usage() - check.baseline > check.cap; diff --git a/test/java/ClassUnload.java b/test/java/ClassUnload.java new file mode 100644 index 0000000..6b92bd4 --- /dev/null +++ b/test/java/ClassUnload.java @@ -0,0 +1,84 @@ +import java.lang.ref.WeakReference; +import java.lang.reflect.Array; +import java.util.function.Supplier; + +// A class loader that nothing refers to any more, with the classes it defined, +// is collected, after its classes ran static initializers, lambdas, string +// concatenation, reflection and an exception. +public class ClassUnload { + public static class Probe { + static final Object marker = new Object(); + + public Probe() {} + + public static String hello(int n) { + return "hello " + n; + } + + public static Supplier supplier() { + return () -> "lambda"; + } + + public static void fail() { + throw new IllegalStateException("probe"); + } + } + + static class Loader extends ClassLoader { + Loader() { + super(ClassUnload.class.getClassLoader()); + } + + @Override + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + if (!name.equals("ClassUnload$Probe")) return super.loadClass(name, resolve); + synchronized (getClassLoadingLock(name)) { + Class c = findLoadedClass(name); + if (c == null) { + try (var in = getParent().getResourceAsStream(name.replace('.', '/') + ".class")) { + byte[] b = in.readAllBytes(); + c = defineClass(name, b, 0, b.length); + } catch (java.io.IOException e) { + throw new ClassNotFoundException(name, e); + } + } + return c; + } + } + } + + static void use(WeakReference[] out) throws Exception { + Loader loader = new Loader(); + Class c = Class.forName("ClassUnload$Probe", true, loader); + Object o = c.getDeclaredConstructor().newInstance(); + Object array = Array.newInstance(c, 3); + System.out.println(c.getMethod("hello", int.class).invoke(null, 7)); + System.out.println(((Supplier) c.getMethod("supplier").invoke(null)).get()); + try { + c.getMethod("fail").invoke(null); + } catch (java.lang.reflect.InvocationTargetException e) { + System.out.println(e.getCause().getMessage()); + } + System.out.println(o.getClass().getClassLoader() == loader); + out[0] = new WeakReference<>(loader); + out[1] = new WeakReference<>(c); + out[2] = new WeakReference<>(o); + out[3] = new WeakReference<>(array); + } + + public static void main(String[] args) throws Exception { + WeakReference[] refs = new WeakReference[4]; + use(refs); + for (int i = 0; i < 200; i++) { + boolean cleared = true; + for (WeakReference r : refs) if (r.get() != null) cleared = false; + if (cleared) break; + System.gc(); + Thread.sleep(20); + } + System.out.println("loader " + (refs[0].get() == null)); + System.out.println("class " + (refs[1].get() == null)); + System.out.println("instance " + (refs[2].get() == null)); + System.out.println("array " + (refs[3].get() == null)); + } +} From bcfca7436e022834d49d2fe61ef93e92b00bf92a Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:48:11 -0600 Subject: [PATCH 05/10] docs: describe what System.gc, finalizers and class unloading do --- docs/support.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/support.md b/docs/support.md index ed7a3e0..69d3bde 100644 --- a/docs/support.md +++ b/docs/support.md @@ -46,6 +46,17 @@ java-web targets headless Java: console apps, libraries and javac. processes do to a watched host directory, with Node's `fs.watch` (so a short-lived entry can show as a delete without its create). Elsewhere it does not see changes made outside the VM. +- Garbage collection is the JS engine's. Under Node, `System.gc()` collects: weak and + phantom references are cleared and enqueued, finalizers (off with + `--finalization=disabled`) and Cleaners run, and a class loader that nothing + refers to is unloaded with its classes and hidden classes. Soft references + are cleared before an `OutOfMemoryError`, and by `System.gc()` when more than + half the heap limit is in use. + An object with a finalizer is finalized only if no cycle leads from its own + fields back to it (JS cannot finalize such an object): the VM drops the + finalizer of an object it finds on such a cycle, so the object is freed + without `finalize()` running, and a cycle it does not find (a class loader + with a `finalize()`) keeps the object for good. In the browser `System.gc()` is a hint and the engine collects when it chooses. - Not supported: GUI apps (AWT and Swing windows), `ConvolveOp`, child processes in the browser, and native libraries other than JS natives. From 9b65bf6c19ffa83e926ffb1ffc6ffea19c39a461 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 11:48:11 -0600 Subject: [PATCH 06/10] test(jtreg): run 15 reference, finalization and class unloading tests --- test/jtreg/passing.txt | 15 +++++++++++++++ test/jtreg/times.tsv | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/jtreg/passing.txt b/test/jtreg/passing.txt index 28bf054..fb219b7 100644 --- a/test/jtreg/passing.txt +++ b/test/jtreg/passing.txt @@ -8,12 +8,15 @@ java/awt/geom/Path2D/Path2DCopyConstructor.java java/awt/geom/SerialTest.java java/beans/Beans/TypoInBeanDescription.java java/beans/EventHandler/Test6788531.java +java/beans/Introspector/7064279/Test7064279.java java/beans/Introspector/8159696/UnloadClassBeanInfo.java java/beans/Introspector/Test4948761.java +java/beans/Introspector/Test5102804.java java/beans/Introspector/Test8027905.java java/beans/Introspector/TestMethodOrderDependence.java java/beans/PropertyChangeSupport/Test4682386.java java/beans/PropertyEditor/Test4968709.java +java/beans/PropertyEditor/Test6397609.java java/beans/PropertyEditor/TestByteClassValue.java java/beans/XMLEncoder/6777487/TestCheckedSet.java java/beans/XMLEncoder/Test4646747.java @@ -108,6 +111,7 @@ java/io/Serializable/serialProxy/SerialProxyClassNotFound.java java/io/Serializable/skipWriteObject/Write.java java/io/Serializable/skippedObjCNFException/Write.java java/io/Serializable/stopCustomDeserialization/Write.java +java/io/Serializable/subclassGC/SubclassGC.java java/io/Serializable/superclassDataLoss/SuperclassDataLossTest.java java/io/Serializable/unresolvableObjectStreamClass/UnresolvableObjectStreamClass.java java/io/Serializable/unresolvedClassDesc/Write.java @@ -141,6 +145,7 @@ java/lang/ClassLoader/LoadNullClass.java java/lang/ClassLoader/RecursiveSystemLoader.java java/lang/ClassLoader/deadlock/DelegateTest.java java/lang/ClassLoader/deadlock/GetResource.java +java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java java/lang/ClassLoader/getResource/automaticmodules/Driver.java java/lang/Compare.java java/lang/Float/Binary16Conversion.java @@ -164,6 +169,7 @@ java/lang/Math/SqrtTests.java java/lang/Math/WorstCaseTests.java java/lang/ModuleTests/AnnotationsTest.java java/lang/ModuleTests/access/AccessTest.java +java/lang/Object/FinalizationOption.java java/lang/Object/WaitTooLong.java java/lang/Package/PackageFromManifest.java java/lang/Process/WaitForDuration.java @@ -271,6 +277,7 @@ java/lang/System/LoggerFinder/modules/JDKLoggerForImageTest.java java/lang/System/LoggerFinder/modules/JDKLoggerForJDKTest.java java/lang/System/LoggerFinder/modules/NamedLoggerForJDKTest.java java/lang/System/Versions.java +java/lang/System/finalization/FinThreads.java java/lang/Thread/BuilderTest.java java/lang/Thread/HoldsLock.java java/lang/Thread/NullStackTrace.java @@ -371,6 +378,7 @@ java/lang/invoke/condy/CondyWithGarbageTest.java java/lang/invoke/condy/CondyWrongType.java java/lang/invoke/condy/ConstantBootstrapsTest.java java/lang/invoke/defineHiddenClass/PreviewHiddenClass.java +java/lang/invoke/defineHiddenClass/UnloadingTest.java java/lang/invoke/lambda/InheritedMethodTest.java java/lang/invoke/lambda/LambdaAsm.java java/lang/invoke/lambda/LambdaReceiver.java @@ -386,6 +394,7 @@ java/lang/management/ManagementFactory/MXBeanException.java java/lang/management/ManagementFactory/MXBeanProxyTest.java java/lang/management/ManagementFactory/ThreadMXBeanProxy.java java/lang/management/MemoryMXBean/MemoryTest.java#5 +java/lang/management/MemoryMXBean/Pending.java java/lang/management/MemoryPoolMXBean/LargeHeapThresholdTest.java java/lang/management/MemoryPoolMXBean/ThresholdTest.java java/lang/management/OperatingSystemMXBean/GetSystemLoadAverage.java @@ -412,8 +421,11 @@ java/lang/management/ThreadMXBean/VirtualThreadDeadlocks.java#2 java/lang/module/ConfigurationTest.java java/lang/module/ModuleDescriptorHashCodeTest.java java/lang/module/MultiReleaseJarTest.java +java/lang/ref/Basic.java +java/lang/ref/FinalizeOverride.java java/lang/ref/NullQueue.java java/lang/ref/ReferenceRefersTo.java +java/lang/ref/SoftReference/Pin.java java/lang/reflect/AccessFlag/ClassAccessFlagTest.java java/lang/reflect/AccessFlag/FieldAccessFlagTest.java java/lang/reflect/Array/ExceedMaxDim.java @@ -906,6 +918,8 @@ java/util/Timer/NameConstructors.java java/util/TreeMap/HeadTailTypeError.java java/util/UUID/UUIDTest.java java/util/Vector/CopyInto.java +java/util/WeakHashMap/GCDuringIteration.java +java/util/concurrent/ArrayBlockingQueue/WhiteBox.java java/util/concurrent/BlockingQueue/LastElement.java java/util/concurrent/CompletableFuture/Basic.java java/util/concurrent/CompletableFuture/ThenComposeAsyncTest.java @@ -949,6 +963,7 @@ java/util/logging/LocalizedLevelName.java java/util/logging/LogManager/Configuration/rootLoggerHandlers/BadRootLoggerHandlers.java java/util/logging/LogManager/Configuration/rootLoggerHandlers/RootLoggerHandlers.java java/util/logging/LogManagerAppContextDeadlock.java +java/util/logging/Logger/bundleLeak/BundleTest.java java/util/logging/Logger/getGlobal/TestGetGlobal.java java/util/logging/Logger/getGlobal/TestGetGlobalByName.java java/util/logging/Logger/getGlobal/TestGetGlobalConcurrent.java diff --git a/test/jtreg/times.tsv b/test/jtreg/times.tsv index a6e465c..d85a8ce 100644 --- a/test/jtreg/times.tsv +++ b/test/jtreg/times.tsv @@ -339,6 +339,7 @@ 21250 java/util/TimeZone/CLDRDisplayNamesTest.java 21178 java/lang/System/LoggerFinder/modules/JDKLoggerForImageTest.java 21118 java/util/logging/Logger/getGlobal/TestGetGlobal.java +20921 java/lang/invoke/defineHiddenClass/UnloadingTest.java 20900 java/net/httpclient/AsyncShutdownNow.java 20832 java/util/ResourceBundle/modules/unnamed/UnNamedTest.java 20826 java/lang/Thread/virtual/stress/GetStackTraceALotWhenParking.java @@ -420,6 +421,7 @@ 17541 java/io/Serializable/records/SerialPersistentFieldsTest.java 17532 java/util/Arrays/CopyMethods.java 17457 java/util/zip/DeflateIn_InflateOut.java +17429 java/lang/ref/SoftReference/Pin.java 17397 java/lang/Thread/virtual/stress/TimedWaitALot.java#2 17352 java/security/KeyAgreement/NegativeTest.java 17300 java/text/Format/DateFormat/Bug8141243.java @@ -864,6 +866,7 @@ 8225 java/beans/XMLEncoder/Test6570354.java 8224 java/time/test/java/time/format/TestDateTimeTextProviderWithLocale.java 8220 java/security/SignedObject/Copy.java +8209 java/lang/ref/FinalizeOverride.java 8206 java/lang/String/StringRepeat.java 8202 java/io/Serializable/serialFilter/SerialFilterFunctionTest.java 8200 java/net/Authenticator/AuthNPETest.java @@ -987,6 +990,7 @@ 7325 java/io/Console/RestoreEchoTest.java 7323 java/net/CookieHandler/EmptyCookieHeader.java 7321 java/nio/channels/etc/PrintSupportedOptions.java +7318 java/lang/System/finalization/FinThreads.java 7317 java/time/test/java/time/format/TestNumberPrinter.java 7312 java/awt/geom/Path2D/GetBounds2DPrecisionTest.java 7310 java/util/TimeZone/TransitionTest.java @@ -1287,6 +1291,7 @@ 6078 java/time/test/java/time/zone/TestZoneRulesProvider.java 6076 java/time/test/java/time/temporal/TestJulianFields.java 6072 java/net/Socket/RST.java +6071 java/lang/Object/FinalizationOption.java 6067 java/beans/XMLEncoder/java_awt_Rectangle.java 6066 java/util/Locale/CaseFoldLanguageTagTest.java 6061 java/util/Spliterator/SpliteratorCharacteristics.java @@ -1534,6 +1539,7 @@ 5259 java/beans/PropertyDescriptor/OverriddenGetter.java 5256 jdk/nio/zipfs/NewFileSystemTests.java 5255 java/util/Comparator/BasicTest.java +5255 java/util/logging/Logger/bundleLeak/BundleTest.java 5254 java/util/zip/ZipFile/InvalidCommentLengthTest.java 5252 java/beans/XMLEncoder/Test6256805.java 5248 java/lang/Package/GetPackages.java @@ -1690,6 +1696,7 @@ 4804 java/time/tck/java/time/chrono/serial/TCKChronoLocalDateTimeSerialization.java 4803 java/lang/Math/CubeRootTests.java 4802 java/util/Locale/RootLocale.java +4801 java/util/concurrent/ArrayBlockingQueue/WhiteBox.java 4799 java/lang/CharSequence/Comparison.java 4798 java/lang/invoke/ArrayLengthTest.java 4796 java/text/Collator/CollationKeyTest.java @@ -1985,6 +1992,7 @@ 4090 java/net/httpclient/HttpResponseInputStreamTest.java 4088 java/util/Formatter/HexFloatZeroPadding.java 4088 java/nio/channels/DatagramChannel/Unref.java +4087 java/lang/management/MemoryMXBean/Pending.java 4086 java/beans/XMLEncoder/javax_swing_border_LineBorder.java 4084 java/beans/XMLEncoder/java_beans_EventHandler.java 4083 java/lang/Class/GetModuleTest.java @@ -2069,6 +2077,7 @@ 3875 java/beans/Introspector/DefaultMethodBeanPropertyTest.java 3870 java/beans/XMLEncoder/6777487/TestCheckedMap.java 3866 java/math/BigDecimal/AddTests.java +3864 java/beans/PropertyEditor/Test6397609.java 3859 java/util/regex/ImmutableMatchResultTest.java 3849 java/lang/Package/PackageVersionTest.java 3845 java/util/zip/ZipFile/Assortment.java @@ -2645,6 +2654,7 @@ 2676 java/io/Serializable/writeReplace/WriteReplace.java 2675 java/net/MulticastSocket/TestInterfaces.java 2674 java/beans/Introspector/Test8027648.java +2674 java/util/WeakHashMap/GCDuringIteration.java 2667 java/beans/Beans/Test4343723.java 2665 java/util/concurrent/locks/Lock/CheckedLockLoops.java 2663 java/net/spi/InetAddressResolverProvider/BuiltInResolverTest.java @@ -3028,6 +3038,7 @@ 2210 java/nio/channels/unixdomain/SocketOptions.java 2210 java/nio/channels/Selector/WakeupEmpty.java 2209 java/beans/Introspector/Test4168833.java +2209 java/lang/ref/Basic.java 2208 java/lang/Math/PowTests.java 2205 java/text/Format/DateFormat/Bug6609750.java 2203 java/net/InetSocketAddress/CreateUnresolved.java @@ -3274,6 +3285,7 @@ 1972 java/lang/invoke/lambda/LambdaClassSynthetic.java 1971 java/lang/invoke/lambda/MetafactoryArgValidationTest.java 1971 java/io/DataInputStream/SkipBytes.java +1971 java/io/Serializable/subclassGC/SubclassGC.java 1969 java/util/Collections/Frequency.java 1969 java/lang/Double/NaNInfinityParsing.java 1967 java/lang/management/ThreadMXBean/ThreadCpuTime.java @@ -3743,6 +3755,7 @@ 1539 java/awt/geom/Arc2D/ChordContainsTest.java 1538 java/math/BigInteger/HashCode.java 1537 java/net/SocketOption/TcpKeepAliveTest.java +1537 java/beans/Introspector/Test5102804.java 1536 java/lang/String/NonCharacterMapping.java 1535 java/util/EnumSet/InsertWrongType.java 1535 java/lang/reflect/Method/invoke/IllegalAccessInInvoke.java @@ -3827,6 +3840,8 @@ 1439 java/io/OutputStreamWriter/CloseWriterOnFailedFlush.java 1437 java/util/Properties/Bug6609431.java 1437 java/util/LinkedList/AddAll.java +1437 java/lang/ClassLoader/forNameLeak/ClassForNameLeak.java +1436 java/beans/Introspector/7064279/Test7064279.java 1435 java/util/Formatter/NullArg.java 1434 java/io/FilterOutputStream/Write.java 1431 java/util/Collections/AddAll.java From 77465be371e5484c56b30b7fabc416246a112747 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:08:52 -0600 Subject: [PATCH 07/10] fix(threads): invoke the private Thread.exit method directly --- src/jvm.ts | 2 +- test/java/StructuredThreadExit.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/java/StructuredThreadExit.java diff --git a/src/jvm.ts b/src/jvm.ts index 22cb552..79a9673 100644 --- a/src/jvm.ts +++ b/src/jvm.ts @@ -1182,7 +1182,7 @@ export class JVM { *exitThread(t: JThread): NativeGen { const obj = t.obj!; try { - yield t.callVirtual(obj, "exit", "()V"); + yield t.call(this.loadBoot("java/lang/Thread")!.methodMap.get("exit()V")!, obj); } catch { // Thread.exit failures are swallowed, see JavaThread::exit } diff --git a/test/java/StructuredThreadExit.java b/test/java/StructuredThreadExit.java new file mode 100644 index 0000000..b28a710 --- /dev/null +++ b/test/java/StructuredThreadExit.java @@ -0,0 +1,19 @@ +// javac: --enable-preview --release 25 +// java: --enable-preview +import java.util.concurrent.StructuredTaskScope; + +public class StructuredThreadExit { + static class Worker extends Thread { + Worker(Runnable task) { super(task); } + private void exit() { throw new AssertionError("called unrelated private exit"); } + } + + public static void main(String[] args) throws Exception { + try (var scope = StructuredTaskScope.open(StructuredTaskScope.Joiner.awaitAll(), + config -> config.withThreadFactory(Worker::new))) { + var task = scope.fork(() -> 42); + scope.join(); + System.out.println(task.get()); + } + } +} From 4012343031a11d4311be1ce734ba31c50e18443b Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:34:43 -0600 Subject: [PATCH 08/10] perf(gc): release compiled-code pins at most every 5 seconds so a System.gc loop does not recompile --- src/natives/lang.ts | 2 +- src/runtime.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/natives/lang.ts b/src/natives/lang.ts index 9b0273a..7819cc2 100644 --- a/src/natives/lang.ts +++ b/src/natives/lang.ts @@ -387,7 +387,7 @@ export const natives: Natives = { for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp); releaseSoftReferences(t.vm); releaseFinalizerCycles(t.vm); - releasePins(250); + releasePins(5000); const t0 = performance.now(); yield t.block(collect(t.vm, gc)); stats.ms += performance.now() - t0; diff --git a/src/runtime.ts b/src/runtime.ts index acb247a..346fbd3 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -251,7 +251,7 @@ export function pin(o: JMethod | any[] | { c: JClass | null; m: JMethod | null; // Returns whether there was anything to empty. With a gap, it does nothing // within that many ms of the last time: compiled code is dropped, and a // program that collects in a loop would compile it again each time. -let pinsReleased = 0; +let pinsReleased = -Infinity; export function releasePins(gapMs = 0): boolean { const now = performance.now(); if (now - pinsReleased < gapMs) return false; From f196f06b679b1185c851cd03a3b42a11370480d0 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:18:01 -0600 Subject: [PATCH 09/10] perf(jit): give virtual call caches one shape --- src/jit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jit.ts b/src/jit.ts index 5240d4e..a136122 100644 --- a/src/jit.ts +++ b/src/jit.ts @@ -1385,7 +1385,7 @@ function genGuardMiss(g: Gen, pc: number, next: number, d: number, target: JMeth const args = Array.from({ length: n }, (_, i) => `s${base + i}`); const ret = target.ret; const xp = g.xcode(pc, base, EXIT_SUSPEND), xi = g.xcode(pc, d, EXIT_INTERPRET); - const cell = g.K([null, null, target.key]); + const cell = g.K([null, null, target.key, false]); const call = n <= 4 ? `rt.callv(t,f,${cell},${pc},${xp},${xi},${args})` : `rt.callvMany(t,f,${cell},[${args}],${pc},${xp},${xi})`; return `v=${call};if(v===rt.E){x=rt.xc;break X;}${ret === "V" ? "" : `s${base}=v;`}` + `if(t.state!==0)${g.exit(next, ret === "V" ? base : base + (isWide(ret) ? 2 : 1), EXIT_SUSPEND)}`; From 1880884052c59daa6c134010d7eb33c826f23c1d Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:21:50 -0600 Subject: [PATCH 10/10] fix(gc): free finalizable objects on cycles before an OutOfMemoryError and pin constants of unloadable classes --- src/jit.ts | 2 +- src/natives/ref.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/jit.ts b/src/jit.ts index a136122..e259c6e 100644 --- a/src/jit.ts +++ b/src/jit.ts @@ -76,7 +76,7 @@ export function restoreJit(vm: JVM, m: JMethod, code: SavedJit) { // A constant of compiled code that holds a class another loader can drop. function namesUnloadable(v: unknown, caller: JClass): boolean { - const c = v instanceof JClass ? v : v instanceof JMethod || v instanceof JField ? v.cls : v instanceof JObject && v.x instanceof JClass ? v.x : null; + const c = v instanceof JClass ? v : v instanceof JMethod || v instanceof JField ? v.cls : v instanceof JObject ? (v.x instanceof JClass ? v.x : v.cls) : null; return c !== null && c !== caller && c.unloadable && (c.hidden || c.loader !== caller.loader); } diff --git a/src/natives/ref.ts b/src/natives/ref.ts index 2f24467..1988c65 100644 --- a/src/natives/ref.ts +++ b/src/natives/ref.ts @@ -73,8 +73,9 @@ function refs(vm: JVM): RefState { softCount: 0, }; setReclaim(() => { + const cycles = forgetCycles(s!, SCAN_AT_GC); const soft = clearSoftReferences(vm); - return releasePins() || soft; + return releasePins() || soft || cycles; }); vm.state.set("refs", s); } @@ -217,7 +218,8 @@ function leadsBack(w: JObject, f: any[], budget: { left: number }): boolean { return false; } -function forgetCycles(s: RefState, scan: number) { +// Returns whether it released any. +function forgetCycles(s: RefState, scan: number): boolean { const budget = { left: scan }; const list = s.fins; const kept: Fin[] = []; @@ -229,7 +231,9 @@ function forgetCycles(s: RefState, scan: number) { if (leadsBack(w, h.f!, budget)) h.f = null; else kept.push(h); } + const released = i - kept.length; s.fins = list.slice(i).concat(kept); + return released > 0; } export function releaseFinalizerCycles(vm: JVM) {