From ee8265e79f1e6c4f399abbca73c3174e68e83cc0 Mon Sep 17 00:00:00 2001 From: chiba Date: Thu, 16 Jun 2005 17:52:38 +0000 Subject: [PATCH] Turning the automatic pruning on by default. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@181 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/ClassPool.java | 27 +++++++ src/main/javassist/CtClass.java | 83 +++++++++++++--------- src/main/javassist/CtClassType.java | 32 +++++---- src/main/javassist/bytecode/ClassFile.java | 1 + 4 files changed, 97 insertions(+), 46 deletions(-) diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index 05b0bb82..452f2e6f 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -73,6 +73,33 @@ public class ClassPool { */ public boolean childFirstLookup = false; + /** + * Turning the automatic pruning on/off. + * + *

If this field is true, CtClass objects are + * automatically pruned by default when toBytecode() etc. + * are called. The automatic pruning can be turned on/off individually + * for each CtClass object. + * + *

The initial value is true. + * + * @see CtClass#prune() + * @see CtClass#stopPruning(boolean) + */ + public static boolean doPruning = true; + + /* releaseUnmodifiedClassFile was introduced for avoiding a bug + of JBoss AOP. So the value should be true except for JBoss AOP. + */ + + /** + * If true, unmodified and not-recently-used class files are + * periodically released for saving memory. + * + *

The initial value is true. + */ + public static boolean releaseUnmodifiedClassFile = true; + protected ClassPoolTail source; protected ClassPool parent; protected Hashtable classes; // should be synchronous diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 67378862..eabdb891 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -265,33 +265,6 @@ public abstract class CtClass { throw new RuntimeException("cannot defrost " + getName()); } - /** - * Disallows (or allows) pruning the data structure on memory - * when this CtClass object is converted into a class file. - * Pruning saves memory space since a ClassPool holds - * all instances of CtClass - * all the time of program execution. - * However, pruning discards the data representing part of the - * class definition, such as method bodies. - * Therefore, once it is pruned, toBytecode(), - * writeFile(), or toClass() cannot - * be called again. - * - *

Initially, pruning is allowed. - * - * @param stop disallow pruning if true. Otherwise, allow. - * @see #detach() - * @see #toBytecode() - * @see #toClass() - * @see #writeFile() - */ - public void stopPruning(boolean stop) {} - - /* Called by get() in ClassPool. - * CtClassType overrides this method. - */ - void incGetCounter() {} - /** * Returns true if this object represents a primitive * Java type: boolean, byte, char, short, int, long, float, double, @@ -1006,6 +979,55 @@ public abstract class CtClass { cp.cacheCtClass(getName(), obj, false); } + /** + * Disallows (or allows) automatically pruning this CtClass + * object. + * + *

+ * Javassist can automatically prune a CtClass object + * when toBytecode() (or toClass(), + * writeFile()) is called. + * Since a ClassPool holds all instances of CtClass + * even after toBytecode() (or toClass(), + * writeFile()) is called, pruning may significantly + * save memory consumption. + * + *

If ClassPool.doPruning is true, the automatic pruning + * is on by default. Otherwise, it is off. + * + * @param stop disallow pruning if true. Otherwise, allow. + * + * @see #detach() + * @see #prune() + * @see ClassPool#doPruning + */ + public void stopPruning(boolean stop) {} + + /** + * Discards unnecessary data, in particuar, CodeAttributes + * (method bodies) of the class, + * to minimize the memory footprint. + * After calling this method, the class is read only. + * It cannot be modified any more. + * Furthermore, toBytecode(), + * writeFile(), toClass(), + * or instrument() cannot be called. + * However, the method names and signatures in the class etc. + * are still accessible. + * + * @see #toBytecode() + * @see #toClass() + * @see #writeFile() + * + * @see #stopPruning(boolean) + */ + public void prune() {} + + /* Called by get() in ClassPool. + * CtClassType overrides this method. + */ + void incGetCounter() {} + /** * Converts this class to a class file. * Once this method is called, further modifications are not @@ -1111,13 +1133,6 @@ public abstract class CtClass { } } - /** - * Discards unnecessary data to minimize the memory footprint. - * After calling this method, the class is read only. - * It cannot be modified any more. - */ - public void prune() {} - /** * Converts this class to a class file. * Once this method is called, further modifications are not diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 6b1ad068..44014641 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -66,7 +66,7 @@ class CtClassType extends CtClass { private Hashtable hiddenMethods; // must be synchronous private int uniqueNumberSeed; - private boolean doPruning = false; + private boolean doPruning = ClassPool.doPruning; int getCounter; private static int readCounter = 0; private static final int READ_THRESHOLD = 100; // see getClassFile2() @@ -173,12 +173,11 @@ class CtClassType extends CtClass { if (classfile != null) return classfile; - /* - if (readCounter++ > READ_THRESHOLD) { - doCompaction(); + if (readCounter++ > READ_THRESHOLD + && ClassPool.releaseUnmodifiedClassFile) { + releaseClassFiles(); readCounter = 0; } - */ InputStream fin = null; try { @@ -215,7 +214,12 @@ class CtClassType extends CtClass { */ void incGetCounter() { ++getCounter; } - private void doCompaction() { + /** + * Releases the class files and cached CtBehaviors + * of the CtClasses that have not been recently used + * if they are unmodified. + */ + private void releaseClassFiles() { Enumeration e = classPool.classes.elements(); while (e.hasMoreElements()) { Object obj = e.nextElement(); @@ -963,13 +967,17 @@ class CtClassType extends CtClass { } } - public void prune() { - if (wasPruned) - return; + /** + * @see javassist.CtClass#prune() + * @see javassist.CtClass#stopPruning(boolean) + */ + public void prune() { + if (wasPruned) + return; - wasPruned = wasFrozen = true; - getClassFile2().prune(); - } + wasPruned = wasFrozen = true; + getClassFile2().prune(); + } public void toBytecode(DataOutputStream out) throws CannotCompileException, IOException diff --git a/src/main/javassist/bytecode/ClassFile.java b/src/main/javassist/bytecode/ClassFile.java index 4a8fa4ab..9fd3c8e4 100644 --- a/src/main/javassist/bytecode/ClassFile.java +++ b/src/main/javassist/bytecode/ClassFile.java @@ -373,6 +373,7 @@ public final class ClassFile { /** * Returns the names of the interfaces implemented by the class. + * The returned array is read only. */ public String[] getInterfaces() { if (cachedInterfaces != null) -- 2.39.5