Skip to content

feat(gc): run finalizers, clear soft references and unload class loaders that nothing refers to - #92

Merged
Project516 merged 10 commits into
nextfrom
p516/gc-refs
Nov 17, 2026
Merged

Project516 merged 10 commits into
nextfrom
p516/gc-refs

Conversation

@Project516

@Project516 Project516 commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

What this changes

System.gc() now does what a program expects from it under Node, on top of the JS engine's collector:

  • Runtime.gc returns once the engine has delivered the reference callbacks of its collection, so a weak or phantom reference is cleared and enqueued by the time System.gc() returns.
  • finalize() runs on objects nothing refers to. --finalization=disabled turns it off, as on HotSpot. An object on a cycle through its own fields cannot be finalized by JS; the VM finds such cycles with a bounded scan and drops their finalizer so they are freed.
  • Soft references are cleared before an OutOfMemoryError, and by System.gc() when more than half the heap limit is in use.
  • A class loader that nothing refers to is unloaded with its classes and hidden classes. Inline caches and compiled code that name such a class register a pin, and pins are emptied before a collection, at most every 5 seconds so a program that collects in a loop does not recompile each time.
  • Thread.exit is private, so the VM invokes it directly instead of selecting it virtually.
  • docs/support.md says what System.gc(), finalizers and class unloading do, and which cycles leak.

Tests

  • New programs compared with HotSpot: RefsAfterGc, Finalizers, FinalizerCycles, FinalizationDisabled, SoftRefs, ClassUnload and StructuredThreadExit.
  • 15 OpenJDK reference, finalization and class unloading tests added to passing.txt.
  • Full suite before the rebase: 261 tests, 258 pass, 3 skipped. Typecheck passes after the rebase onto the current master.
  • The passing.txt tests in java/lang, java/beans, java/io/Serializable, java/util/logging, java/util/concurrent and java/util/WeakHashMap: the first 135 of 436 passed before the rebase; this PR's CI runs the rest.
  • Cost of System.gc(), a URLClassLoader loop with 60 calls, 5 alternating runs, median per call: master 7.9 ms, branch 8.8 ms, HotSpot 1.9 ms directly in the call; about 12 ms more per call on the branch counting the recompiles after pins are released.

Noticed, not fixed

  • ReachabilityFenceTest still fails: a local that is dead in the bytecode stays live in a frame.
  • Test4809008 still fails: a class loader with a finalize() sits on a cycle the scan does not follow, so it is never freed (documented).

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 481910fc-7883-4392-beb4-4f4f129f1eb4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This PR implements comprehensive GC features: System.gc() now clears weak/phantom references and enqueues them, runs finalizers (with --finalization=disabled flag), clears soft references under memory pressure, unloads unreachable class loaders, and manages pins on inline caches/compiled code referencing unloadable classes. Thread.exit is made private for direct VM invocation. Documentation updated in support.md and node.md.

Not approved: the diff of test/java/SoftRefs.java, test/java/StructuredThreadExit.java, test/jtreg/passing.txt, test/jtreg/times.tsv, test/programs.test.ts was too large to read.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

Comment thread src/natives/ref.ts Resolved
Comment thread src/jit.ts Resolved
Comment thread src/runtime.ts Resolved
@Project516

Copy link
Copy Markdown
Owner Author

Review of the files the review bot could not read (test/java/SoftRefs.java, test/java/StructuredThreadExit.java, test/jtreg/passing.txt, test/jtreg/times.tsv, test/programs.test.ts): no findings. The two programs print deterministic output on HotSpot (SoftRefs waits on the queue with a timeout instead of racing the reference handler). The --finalization flag reaches the spawned run in programs.test.ts. The 15 passing.txt lines are sorted and not duplicated. Each one has a times.tsv line, and times.tsv stays sorted longest first.

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PR adds finalization, class unloading and related GC behavior to the Java‑web runtime, extending Node options and implementing finalizer handling, soft reference clearing and class loader unloading.

Not approved: the diff of test/java/SoftRefs.java, test/java/StructuredThreadExit.java, test/jtreg/passing.txt, test/jtreg/times.tsv, test/programs.test.ts was too large to read.


review-bot, model nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free, verdict comment

@Project516
Project516 merged commit a749c46 into next Nov 17, 2026
12 checks passed
@Project516
Project516 deleted the p516/gc-refs branch November 17, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant