Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jruby.util.collections;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
Expand All @@ -10,7 +12,8 @@
*/
final class StableClassValue<T> extends ClassValue<T> {
private final ReentrantLock lock = new ReentrantLock();
private final WeakHashMap<StableValue<Class<?>, T>, Object> stableValues = new WeakHashMap<>();
// the classes are referenced weakly, so that this map does not keep them loaded
private final WeakHashMap<StableValue<Class<?>, T>, WeakReference<Class<?>>> stableValues = new WeakHashMap<>();

public StableClassValue(ClassValueCalculator<T> calculator) {
super(calculator);
Expand All @@ -22,10 +25,17 @@ public T get(Class<?> cls) {
return proxy.get(cls).get(cls);
}

/**
* Clears every computed value and removes it from the class it was computed for. Only clearing them would leave the
* {@link StableValue} instances in the classes, and since those are loaded by JRuby's class loader, a class from
* another class loader, such as a JDK class, would keep JRuby's class loader alive.
*/
public void clear() {
synchronized (stableValues) {
for (StableValue<Class<?>, T> value : stableValues.keySet()) {
value.clear();
for (Map.Entry<StableValue<Class<?>, T>, WeakReference<Class<?>>> entry : stableValues.entrySet()) {
entry.getKey().clear();
Class<?> type = entry.getValue().get();
if (type != null) proxy.remove(type);
}
stableValues.clear();
}
Expand Down Expand Up @@ -77,13 +87,13 @@ void clear() {

private final java.lang.ClassValue<StableValue<Class<?>, T>> proxy = createStableValueProxy(lock, calculator, stableValues);

private static <U> java.lang.ClassValue<StableValue<Class<?>, U>> createStableValueProxy(ReentrantLock lock, Function<Class<?>, U> calculator, WeakHashMap<StableValue<Class<?>, U>, Object> stableValues){
private static <U> java.lang.ClassValue<StableValue<Class<?>, U>> createStableValueProxy(ReentrantLock lock, Function<Class<?>, U> calculator, WeakHashMap<StableValue<Class<?>, U>, WeakReference<Class<?>>> stableValues){
return new java.lang.ClassValue<>() {
@Override
protected StableValue<Class<?>, U> computeValue(Class<?> type) {
StableValue<Class<?>, U> stableValue = new StableValue<>(lock, calculator);
synchronized (stableValues) {
stableValues.put(stableValue, null);
stableValues.put(stableValue, new WeakReference<>(type));
}
return stableValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jruby.util.collections;

import org.junit.Test;

import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Adler32;
import java.util.zip.CRC32;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

public class StableClassValueTest {

/**
* The computed values are only referenced from the classes they were computed for, and must not be collected and
* computed again.
*/
@Test
public void keepsValuesNothingElseReferences() throws InterruptedException {
var computed = new AtomicInteger();
var classValue = new StableClassValue<>(cls -> {
computed.incrementAndGet();
return new Object();
});

var value = new WeakReference<>(classValue.get(Adler32.class));
var garbage = new WeakReference<>(new Object());
forceGC(garbage);

assertNull("GC did not run", garbage.get());
assertNotNull("value was collected", value.get());
assertSame(value.get(), classValue.get(Adler32.class));
assertEquals(1, computed.get());
}

/**
* A JDK class is never unloaded, so the value it holds for a StableClassValue must not reference the class loader
* that loaded StableClassValue once clear() has been called. Here that class loader is a disposable one, like the
* class loader of an embedded JRuby.
*/
@Test
public void clearReleasesTheClassLoader() throws Exception {
WeakReference<ClassLoader> loader = computeAndClearInDisposableLoader();
forceGC(loader);

assertNull("class loader was not collected after clear()", loader.get());
}

private static WeakReference<ClassLoader> computeAndClearInDisposableLoader() throws Exception {
URL[] classpath = {
StableClassValue.class.getProtectionDomain().getCodeSource().getLocation(),
NewObject.class.getProtectionDomain().getCodeSource().getLocation()
};
try (var loader = new URLClassLoader(classpath, null)) {
Class<?> calculatorType = loader.loadClass(ClassValueCalculator.class.getName());
Object calculator = loader.loadClass(NewObject.class.getName()).getDeclaredConstructor().newInstance();

Class<?> stableClassValue = loader.loadClass(StableClassValue.class.getName());
Constructor<?> constructor = stableClassValue.getConstructor(calculatorType);
Method get = stableClassValue.getMethod("get", Class.class);
Method clear = stableClassValue.getMethod("clear");
constructor.setAccessible(true);
get.setAccessible(true);
clear.setAccessible(true);

Object classValue = constructor.newInstance(calculator);
assertNotNull(get.invoke(classValue, CRC32.class));
clear.invoke(classValue);

return new WeakReference<>(loader);
}
}

public static class NewObject implements ClassValueCalculator<Object> {
@Override
public Object computeValue(Class<?> cls) {
return new Object();
}
}

private static void forceGC(WeakReference<?> ref) throws InterruptedException {
for (int i = 0; i < 10 && ref.get() != null; i++) {
System.gc();
Thread.sleep(100);
}
}
}
Loading