Skip to content

Remove StableClassValue values from their classes when cleared - #9922

Merged
headius merged 2 commits into
jruby:jruby-10.0from
matrei:fix/stable-class-value-clear
Oct 28, 2026
Merged

headius merged 2 commits into
jruby:jruby-10.0from
matrei:fix/stable-class-value-clear

Conversation

@matrei

@matrei matrei commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Part of #9781, against jruby-10.0 for the 10.0 LTS line.

StableClassValue.clear() only emptied the StableValue holders and left them in the java.lang.ClassValue of each class. StableValue is a JRuby class, so every Java class a runtime computed a value for kept the class loader that loaded JRuby alive after the runtime was torn down, and JDK classes are never unloaded. With the default jruby.ji.class.values, four caches use StableClassValue: JavaSupport's javaClassCache and proxyClassCache, and JavaSupportImpl's staticAssignedNames and instanceAssignedNames. JavaSupport.tearDown() already clears all four.

clear() now also calls remove(cls) on the underlying java.lang.ClassValue for every class it computed a value for. To know those classes, the stableValues side map, whose values used to be null, now maps each StableValue to a WeakReference to its class. It has to be weak, because a strong reference to the class would keep the key, the StableValue, reachable through the class's own class value map, and the WeakHashMap would never drop the entry.

That WeakReference is the only new one. The computed values are still held strongly by the StableValue in each class's class value map, so nothing is computed more often than before. To check, I computed a value that nothing else referenced, ran GC and got it again: it was the same object, computed once, with and without this change.

Before and after. After creating a runtime, running java.lang.StringBuilder.new('jruby').reverse.to_s.upcase and tearing it down, these StableValue instances were left on 10 JDK classes (Object, Class, Enum, StringBuilder, Process, ProcessBuilder and its Redirect and Redirect.Type, Method and Field):

before tearDown() after
jruby-10.0 15 15
this PR 15 0

With the reproducer from #9781, JRuby 10.0, jnr/jffi#201 and the fstring wrapper held through a weak reference, so that only this root is left, these class loaders were still alive after four rounds, with -XX:SoftRefLRUPolicyMSPerMB=0:

alive
without this PR 3 of 4
without this PR, -Djruby.ji.class.values=HARD_MAP 0 of 4
this PR 0 of 4

So HARD_MAP is no longer needed to release a runtime's class loader.

The second commit adds StableClassValueTest: keepsValuesNothingElseReferences is the GC check above, and clearReleasesTheClassLoader computes and clears a value for a JDK class with StableClassValue loaded in a disposable class loader, then checks that the class loader is collected. The second one fails without the first commit. Both rely on System.gc(), like ScriptingContainerClassLeakTest.

rake spec:ji passes (974 examples, 0 failures, 9 pending). rake spec:ruby:fast has 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodified jruby-10.0, because my machine has no IPv6 network.

clear() only emptied the StableValue holders and left them in the
java.lang.ClassValue of each class. The holders are a JRuby class, so a
Java class from another class loader, such as a JDK class with a Java
proxy, kept the class loader that loaded JRuby alive after the runtime
was torn down.

clear() now also removes the value from each class it was computed for.
The classes are referenced weakly to do that.
keepsValuesNothingElseReferences checks that a value only referenced
from the class it was computed for survives GC and isn't computed again.

clearReleasesTheClassLoader loads StableClassValue in a disposable class
loader, computes a value for a JDK class, clears it, and checks that the
class loader can be collected. It fails without the previous commit.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, looks ok to me. The additional sidecar weak-to-weak map seems to be the missing sauce, allowing us to eagerly scrub out all class values when JRuby is torn down.

The test makes sense, even if it's a little fragile using reflection this way (hard to avoid when referencing code in another classloader).

@headius headius added this to the JRuby 10.0.8.0 milestone Oct 28, 2026
@headius
headius merged commit c2b14f2 into jruby:jruby-10.0 Oct 28, 2026
126 checks passed
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.

2 participants