[jdom-interest] Bug Report: Memory Leak in SAXHandler.java

Kevin A. Roll kroll at anaglyph.net
Wed Apr 2 17:51:49 PST 2003


Greetings all... I am hoping that this is the proper place to make a bug 
report, and that the information will get into the proper hands.

I've uncovered a rather serious memory leak in JDOM b8. This can literally 
cause over 100 megabytes of memory to get sucked up, as I've seen in my own 
application. The problem is on line 721 of the file 
org/jdom/input/SAXHandler.java. The code reads:

String data = textBuffer.toString();
textBuffer.setLength( 0 );

This would appear to simply convert the StringBuffer to a String and assign 
it to a new String. The problem, as I discovered after some intense 
debugging, is in the internal implementation of StringBuffer. It attempts 
to conserve memory by simply sharing the memory with the new String, and 
setting an internal flag called shared. Once this flag is set, any 
alteration to the StringBuffer causes the memory to be duplicated. Since 
this buffer is used as a temporary buffer by the class and constantly 
altered, the effect is that hundreds of 8192-byte blocks are allocated and 
remain stuck in memory.

When I altered this line to read

String data = new String( textBuffer.toString() );

the problem went away. I saw no evidence of extra memory blocks that didn't 
belong.

I noticed that there is a comment above this section that recommends 
eventually using textBuffer.substring(0) once JDK 1.1 support is removed. 
Someone may already be at least partially aware of this problem, if not 
fully aware of the ramifications.




More information about the jdom-interest mailing list