From d133f96da4ac7bb312ddddbc00cbcd19e94cd412 Mon Sep 17 00:00:00 2001 From: chiba Date: Tue, 3 Jul 2007 15:31:19 +0000 Subject: [PATCH] for reducing memory footprint git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@393 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/ClassPool.java | 19 ++++++++ src/main/javassist/CtClass.java | 5 ++ src/main/javassist/CtClassType.java | 76 ++++++++++++----------------- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index d97d24d1..e7e209a9 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -29,6 +29,7 @@ import java.security.ProtectionDomain; import java.util.Hashtable; import java.util.Iterator; import java.util.ArrayList; +import java.util.Enumeration; import javassist.bytecode.Descriptor; /** @@ -119,6 +120,9 @@ public class ClassPool { */ public static boolean doPruning = false; + private int compressCount; + private static final int COMPRESS_THRESHOLD = 100; + /* releaseUnmodifiedClassFile was introduced for avoiding a bug of JBoss AOP. So the value should be true except for JBoss AOP. */ @@ -184,6 +188,7 @@ public class ClassPool { } this.cflow = null; + this.compressCount = 0; clearImportedPackages(); } @@ -262,6 +267,19 @@ public class ClassPool { return source.toString(); } + /** + * This method is periodically invoked so that memory + * footprint will be minimized. + */ + void compress() { + if (compressCount++ > COMPRESS_THRESHOLD) { + compressCount = 0; + Enumeration e = classes.elements(); + while (e.hasMoreElements()) + ((CtClass)e.nextElement()).compress(); + } + } + /** * Record a package name so that the Javassist compiler searches * the package to resolve a class name. @@ -607,6 +625,7 @@ public class ClassPool { public CtClass makeClass(InputStream classfile, boolean ifNotFrozen) throws IOException, RuntimeException { + compress(); classfile = new BufferedInputStream(classfile); CtClass clazz = new CtClassType(classfile, this); clazz.checkModify(); diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 48a8ca21..42ae5040 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -1330,4 +1330,9 @@ public abstract class CtClass { public String makeUniqueName(String prefix) { throw new RuntimeException("not available in " + getName()); } + + /* Invoked from ClassPool#compress(). + * This method is overridden by CtClassType. + */ + void compress() {} } diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index f3106b9e..859b3af3 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.List; @@ -73,10 +72,8 @@ class CtClassType extends CtClass { private int uniqueNumberSeed; private boolean doPruning = ClassPool.doPruning; - int getCounter; - private static int readCounter = 0; - private static final int READ_THRESHOLD = 100; // see getClassFile2() - private static final int GET_THRESHOLD = 2; // see releaseClassFiles() + private int getCount; + private static final int GET_THRESHOLD = 2; // see compress() CtClassType(String name, ClassPool cp) { super(name); @@ -89,7 +86,7 @@ class CtClassType extends CtClass { fieldInitializers = null; hiddenMethods = null; uniqueNumberSeed = 0; - getCounter = 0; + getCount = 0; } CtClassType(InputStream ins, ClassPool cp) throws IOException { @@ -169,17 +166,13 @@ class CtClassType extends CtClass { if (cfile != null) return cfile; - if (readCounter++ > READ_THRESHOLD) { - releaseClassFiles(); - readCounter = 0; - } - + classPool.compress(); if (rawClassfile != null) { try { classfile = new ClassFile(new DataInputStream( new ByteArrayInputStream(rawClassfile))); rawClassfile = null; - getCounter = GET_THRESHOLD; + getCount = GET_THRESHOLD; return classfile; } catch (IOException e) { @@ -218,11 +211,33 @@ class CtClassType extends CtClass { } } - /** + /* Inherited from CtClass. Called by get() in ClassPool. + * + * @see javassist.CtClass#incGetCounter() + * @see #toBytecode(DataOutputStream) + */ + final void incGetCounter() { ++getCount; } + + /** + * Invoked from ClassPool#compress(). + * It releases the class files that have not been recently used + * if they are unmodified. + */ + void compress() { + if (getCount < GET_THRESHOLD) + if (!isModified() && ClassPool.releaseUnmodifiedClassFile) + removeClassFile(); + else if (isFrozen() && !wasPruned) + saveClassFile(); + + getCount = 0; + } + + /** * Converts a ClassFile object into a byte array * for saving memory space. */ - public synchronized void saveClassFile() { + private synchronized void saveClassFile() { /* getMembers() and releaseClassFile() are also synchronized. */ if (classfile == null || hasMemberCache() != null) @@ -239,40 +254,11 @@ class CtClassType extends CtClass { catch (IOException e) {} } - public synchronized void releaseClassFile() { + private synchronized void removeClassFile() { if (classfile != null && !isModified() && hasMemberCache() == null) classfile = null; } - /* Inherited from CtClass. Called by get() in ClassPool. - * - * @see javassist.CtClass#incGetCounter() - * @see #toBytecode(DataOutputStream) - */ - void incGetCounter() { ++getCounter; } - - /** - * Releases the class files - * of the CtClasses that have not been recently used - * if they are unmodified. - */ - public void releaseClassFiles() { - Enumeration e = classPool.classes.elements(); - while (e.hasMoreElements()) { - Object obj = e.nextElement(); - if (obj instanceof CtClassType) { - CtClassType cct = (CtClassType)obj; - if (cct.getCounter < GET_THRESHOLD) - if (!cct.isModified() && ClassPool.releaseUnmodifiedClassFile) - cct.releaseClassFile(); - else if (cct.isFrozen() && !cct.wasPruned) - cct.saveClassFile(); - - cct.getCounter = 0; - } - } - } - public ClassPool getClassPool() { return classPool; } void setClassPool(ClassPool cp) { classPool = cp; } @@ -1344,7 +1330,7 @@ class CtClassType extends CtClass { // classfile = null; } - getCounter = 0; + getCount = 0; wasFrozen = true; } catch (NotFoundException e) { -- 2.39.5