Permalink
Browse files

Use weak references to Ruby objects in JavaProxyClassFactory (Fixes #164

)

Both the keys and values stored in the proxies list may contain Ruby
objects so use a WeakHashMap to weakly reference the keys and wrap the
values in a WeakReference.
  • Loading branch information...
1 parent 8869261 commit 3d31a6566c228406732fc68409a09af23ed6dc03 @bbrowning committed May 15, 2012
Showing with 6 additions and 3 deletions.
  1. +6 −3 src/org/jruby/javasupport/proxy/JavaProxyClassFactory.java
@@ -28,6 +28,7 @@
package org.jruby.javasupport.proxy;
+import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@@ -44,6 +45,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
+import java.util.WeakHashMap;
import org.jruby.Ruby;
import org.objectweb.asm.ClassVisitor;
@@ -100,7 +102,7 @@
private static int counter;
- private static Map proxies = Collections.synchronizedMap(new HashMap());
+ private static Map proxies = Collections.synchronizedMap(new WeakHashMap());
private static Method defineClass_method; // statically initialized below
@@ -146,7 +148,8 @@ static JavaProxyClass newProxyClass(Ruby runtime, ClassLoader loader,
key.addAll(names);
}
- JavaProxyClass proxyClass = (JavaProxyClass) proxies.get(key);
+ WeakReference<JavaProxyClass> proxyRef = (WeakReference<JavaProxyClass>) proxies.get(key);
+ JavaProxyClass proxyClass = proxyRef == null ? null : proxyRef.get();
if (proxyClass == null) {
if (targetClassName == null) {
@@ -174,7 +177,7 @@ static JavaProxyClass newProxyClass(Ruby runtime, ClassLoader loader,
proxyClass = generate(loader, targetClassName, superClass,
interfaces, methods, selfType);
- proxies.put(key, proxyClass);
+ proxies.put(key, new WeakReference<JavaProxyClass>(proxyClass));
}
return proxyClass;

0 comments on commit 3d31a65

Please sign in to comment.