Apparently, Jalopy 1.5b2-b5 has memory leaks
somewhere. Formatting one and the same small source
file in a loop eventually causes a
java.lang.OutOfMemoryError. I am providing the code
that I use to test it below. Please note I do not
provide a Java source file because the problems
appears with any source file. I use JDK 1.5.0_05. From
my example below you can see I am not holding any
references anywhere to anything:
import java.io.File;
import de.hunsicker.jalopy.Jalopy;
public final class PrettyPrinterAdapter {
public static void format(File file) throws
Exception {
// create a new Jalopy instance with the
currently active code
// convention settings
Jalopy jalopy = new Jalopy();
// specify input and output target
jalopy.setInput(file);
jalopy.setOutput(file);
// format and overwrite the given input file
System.out.println("Running source code
formatter: " + file.getName());
jalopy.format();
if (jalopy.getState() == Jalopy.State.OK)
System.out.println(file + " successfully
formatted");
else if (jalopy.getState() ==
Jalopy.State.WARN)
System.out.println(file + " formatted with
warnings");
else if (jalopy.getState() ==
Jalopy.State.ERROR)
System.out.println(file + " could not be
formatted");
}
public static void main(String[] args) throws
Exception {
for (int i=0; i<200; i++) {
File file = new File
("F:\\Developer\\Tomcat\\webapps\\CMS\\WEB-
INF\\src\\gov\\pbgc\\biz\\Stdtermnot.java");
format(file);
System.gc();
}
}
}
Logged In: YES
user_id=1250025
Apparently, JavaNode class instances are not being
released. Please see attached screen shot.
Screen shot from the profiler showing the problem
Logged In: YES
user_id=1250025
The workaround I now use is following. You need to use the
following code to "clear" references after each usage of
Jalopy. Simply discarding Jalopy instance aint helping:
..........
clearObject(jalopy);
}
/**
* @param jalopy
*/
private void clearObject(Jalopy jalopy) {
Class clazz = jalopy.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
try {
field.setAccessible(true);
field.set(jalopy, null);
} catch (IllegalArgumentException e) {
//e.printStackTrace();
} catch (IllegalAccessException e) {
//e.printStackTrace();
}
}
}
Logged In: YES
user_id=1250025
changed priority back to normal since workaround is
available
Logged In: YES
user_id=723231
The next beta release will be more memory friendly.
The majority of the problem was that the Javanodes where
never being released. This was simply due to circular
references inside the node.
Logged In: YES
user_id=723231
Performed a couple of tests using JMP formatting the same
file 20 times.
Instances / memory
Jalopy 1.5b5------:225,918 / 11,103,192
Reusing javanodes : 49,583 / 2,915,488
Clearing javanodes: 45,156 / 2,430,688
Thats a pretty good reduction...