Repository navigation
Remove StableClassValue values from their classes when cleared - #9922
Merged
Merged
Conversation
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.
7 of 8 tasks
headius
approved these changes
Oct 28, 2026
headius
left a comment
Member
There was a problem hiding this comment.
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #9781, against
jruby-10.0for the 10.0 LTS line.StableClassValue.clear()only emptied theStableValueholders and left them in thejava.lang.ClassValueof each class.StableValueis 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 defaultjruby.ji.class.values, four caches useStableClassValue:JavaSupport'sjavaClassCacheandproxyClassCache, andJavaSupportImpl'sstaticAssignedNamesandinstanceAssignedNames.JavaSupport.tearDown()already clears all four.clear()now also callsremove(cls)on the underlyingjava.lang.ClassValuefor every class it computed a value for. To know those classes, thestableValuesside map, whose values used to benull, now maps eachStableValueto aWeakReferenceto its class. It has to be weak, because a strong reference to the class would keep the key, theStableValue, reachable through the class's own class value map, and theWeakHashMapwould never drop the entry.That
WeakReferenceis the only new one. The computed values are still held strongly by theStableValuein 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.upcaseand tearing it down, theseStableValueinstances were left on 10 JDK classes (Object,Class,Enum,StringBuilder,Process,ProcessBuilderand itsRedirectandRedirect.Type,MethodandField):tearDown()jruby-10.0With 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:-Djruby.ji.class.values=HARD_MAPSo
HARD_MAPis no longer needed to release a runtime's class loader.The second commit adds
StableClassValueTest:keepsValuesNothingElseReferencesis the GC check above, andclearReleasesTheClassLoadercomputes and clears a value for a JDK class withStableClassValueloaded in a disposable class loader, then checks that the class loader is collected. The second one fails without the first commit. Both rely onSystem.gc(), likeScriptingContainerClassLeakTest.rake spec:jipasses (974 examples, 0 failures, 9 pending).rake spec:ruby:fasthas 1 failure and 4 errors, all IPv6 socket specs that fail the same way on unmodifiedjruby-10.0, because my machine has no IPv6 network.