json icon indicating copy to clipboard operation
json copied to clipboard

Don't cache JRuby runtime in static fields

Open headius opened this issue 1 year ago • 1 comments

There are a few places where we cache the JRuby runtime or runtime-specific objects in a static field, such as here with a WeakReference:

https://github.com/ruby/json/blob/7d2ad6d6556da03300a5aeadeeacaec563435773/java/src/json/ext/RuntimeInfo.java#L83-L91

The runtimes cached in weak references will not "leak" but it may take longer for the runtime to be collected than normal. In addition, the cost of traversing the weak reference may offset any gains from caching runtime objects.

These should be removed and cached in a runtime-safe way.

headius avatar Nov 20 '24 22:11 headius

I improved this in #708 but eliminating all references is tricky. Basically this is being used as a way to key a cache object per-runtime that holds references to things like JSON classes, modules and exceptions. It shouldn't leak but having weak references means that dereferenced objects will survive at least one or two more GC cycles while they get cleaned up.

The cache being used may actually be more expensive than just looking up the values again. This should remain open for now and I'll return to it when I can (or someone else will) to try to eliminate more of these references.

headius avatar Dec 03 '24 20:12 headius