[PATCH] make the JavaClass repository cache memory sensitive

"Dave Brosius" <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <00b701c4f2e2$95e6d440$837596d1@MBFG>
I'd like to wrap the repository cache (map values) in SoftReferences. 
Applications can load quite a few JavaClass'es that are stored in the 
repository, and it can become easy to start failing due to out of memory 
conditions. Using the softreferences keeps BCEL humming, even under heavy 
usage. Here's a patch against the latest, that accomplishes this. BTW, if 
anyone would like to give me the secret handshake, i'd be interested in 
committer status to help support BCEL in the future.




Index: src/java/org/apache/bcel/util/SyntheticRepository.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-bcel/src/java/org/apache/bcel/util/SyntheticRepository.java,v
retrieving revision 1.9
diff -u -r1.9 SyntheticRepository.java
--- src/java/org/apache/bcel/util/SyntheticRepository.java 15 Dec 2004 
08:28:50 -0000 1.9
+++ src/java/org/apache/bcel/util/SyntheticRepository.java 5 Jan 2005 
04:40:26 -0000
@@ -19,6 +19,7 @@

 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.ref.SoftReference;
 import java.util.HashMap;

 import org.apache.bcel.classfile.ClassParser;
@@ -76,7 +77,7 @@
    * Store a new JavaClass instance into this Repository.
    */
   public void storeClass(JavaClass clazz) {
-    _loadedClasses.put(clazz.getClassName(), clazz);
+    _loadedClasses.put(clazz.getClassName(), new SoftReference(clazz));
     clazz.setRepository(this);
   }

@@ -91,7 +92,10 @@
    * Find an already defined (cached) JavaClass object by name.
    */
   public JavaClass findClass(String className) {
-    return (JavaClass)_loadedClasses.get(className);
+    SoftReference ref = (SoftReference)_loadedClasses.get(className);
+    if (ref == null)
+      return null;
+    return (JavaClass)ref.get();
   }

   /**
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.