Skip to content

Bring the runtime class loader leak fixes to 10.1 - #9908

Closed
sferik wants to merge 3 commits into
jruby:masterfrom
sferik:runtime-classloader-leak-10.1
Closed

sferik wants to merge 3 commits into
jruby:masterfrom
sferik:runtime-classloader-leak-10.1

Conversation

@sferik

@sferik sferik commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

For #9904.

This cherry-picks @matrei's fixes from #9894 and #9895 onto master, so 10.1 gets them now instead of waiting for the next merge from jruby-10.0. The commits are unchanged and keep their original author. They stop caching LibrarySearcher's index wrappers in thread-locals and make MethodGatherer's ClassValues hold JDK types, so the JDK classes no longer pin each runtime's class loader.

I ran the program from the issue with each round on its own thread, plus -Djruby.native.enabled=false -Djruby.ji.class.values=HARD_MAP to get the other roots out of the way:

master:  round 4: class loaders still alive: 4 of 4
this PR: round 4: class loaders still alive: 0 of 4

With default settings all four are still alive, so this doesn't close the issue yet. What's left is the same as on #9781: the jffi Cleaner (fixed in jnr/jffi#200, but that needs a jffi release and a version bump), the fstring dedup wrapper in Ruby.DEDUP_WRAPPER_CACHE, the proxy cache with the default jruby.ji.class.values, and the JRubyClassLoader temp dir shutdown hook. matrei is splitting those into separate PRs, so I left them alone here.

The Java integration specs give the same results with and without these commits (1008 examples, with one jar glob failure that also fails on master).

The key and range wrappers for the loaded features index were cached in
per-runtime ThreadLocals. Their values stay in every thread that used
the runtime after it is torn down, and since StringWrapper is a JRuby
class, a pooled thread keeps the class loader that loaded JRuby alive.

The wrappers are only used while holding the searcher's lock, so they
are now plain fields.

(cherry picked from commit 05fb66b)
It is unchanged, only moved up from the nested class to MethodGatherer,
so that the next commit can replace PartitionedMethods without touching
it.

(cherry picked from commit c988ce5)
FILTERED_DECLARED_METHODS and FILTERED_METHODS are static ClassValues
that are also computed for JDK classes, such as java.lang.Object and
java.lang.Class. Their PartitionedMethods values are a JRuby class, so
the JDK classes kept the class loader that loaded JRuby alive after
every runtime in it was torn down.

The values are now Method[][], indexed by INSTANCE_METHODS and
STATIC_METHODS.

(cherry picked from commit 3a827e9)
@headius

headius commented Oct 28, 2026

Copy link
Copy Markdown
Member

Thanks for the help but this is not necessary. The jruby-10.0 branch is merged frequently to master so it's usually a pretty simple process. I only defer it for a while to avoid having so many merge commites when there's lots of changes coming in for 10.0.

#9894 and #9895 have been merged over from 10.0 to master.

@headius headius closed this Oct 28, 2026
@headius headius added this to the Invalid or Duplicate milestone Oct 28, 2026
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.

3 participants