SAX, StringBuilder and memory leak
I have a strange problem. I'm parsing a document with large text field. In my characters section I'm using StringBuilder
currentStory.append(ch, start, length);
Then in my endElement I'm assigning it to the appropriate field on my object.
if (name.equals(tagDesc)) {
inDesc = false;
if (currentItem != null ) {
currentItem.setSummaryText(currentStory.toString());
}
currentStory.setLength(0);
}
setSummaryText(String text)
method is:
public void setSummaryText(String text) {
Story = text;
}
And I'm running out of memory.
If I change setSummaryText to something completely weird like this
public void setSummaryText(String text) {
char[] local = text.toString()
Story = new String(local);
}
I'm fine. I just can't figure out where I'm hold开发者_StackOverflow社区ing that reference ? Story is a member var of this object initialized with ""; Note - Assigning to local String variable instead of char[] - fails as well.
I think it might be to do with a performance optimisation of the StringBuffer
toString()
method.
The Sun javadoc says the following:
This method can be coded so as to create a new String object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the string buffer. Any subsequent operation that alters the content or capacity of the string buffer must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation when it is implemented using a string buffer.
Because you're re-using the StringBuffer
with the setLength(0)
it might be keeping a reference to all the Strings it's created with toString()
.
Replace:
currentStory.setLength(0);
with:
currentStory = new StringBuffer();
and see if that resolves it. I don't think this will be any more overhead since in both cases you'll need to create a new char[]
array, since in the first case the array is being used by the String
created with toString()
.
Also, you should consider using a StringBuilder
as they are preferred to StringBuffer
.
精彩评论