From 92fd6f92a6b77dccc72247bd6e5ec51779cb6393 Mon Sep 17 00:00:00 2001 From: Matthias Fratz Date: Wed, 11 Dec 2024 16:20:52 +0100 Subject: [PATCH] disable JRuby's Variable Sharing feature This avoids a memory leak in JRuby 9.4.3.0 (probably at least 9.4.x). --- .../library/cantaloupe/delegate/JRubyDelegateProxy.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/edu/illinois/library/cantaloupe/delegate/JRubyDelegateProxy.java b/src/main/java/edu/illinois/library/cantaloupe/delegate/JRubyDelegateProxy.java index cb3222ca6..1116aa3c8 100644 --- a/src/main/java/edu/illinois/library/cantaloupe/delegate/JRubyDelegateProxy.java +++ b/src/main/java/edu/illinois/library/cantaloupe/delegate/JRubyDelegateProxy.java @@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory; import javax.script.Invocable; +import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; @@ -71,6 +72,14 @@ final class JRubyDelegateProxy implements DelegateProxy { System.setProperty("org.jruby.embed.localvariable.behavior", "transient"); scriptEngine = new ScriptEngineManager().getEngineByName("jruby"); + + // Workaround for a memory leak in JRuby's BiVariableMap: By default, all + // instance variables are stored into BiVariableMap so they are accessible + // from Java. However, there is currently no mechanism for ever removing + // them, forming a memory leak. + // That feature isn't used by Cantaloupe, so we can just disable it. + scriptEngine.getContext().setAttribute("org.jruby.embed.sharing.variables", + false, ScriptContext.ENGINE_SCOPE); } /**