views:

270

answers:

1

Hi All. I have 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);
  }

setSummary 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 cant figure out where i'm holding that reference ? Story is a member var of this object initialized with ""; note - assigning to local String variable instead of char[] - fails as well.

+3  A: 

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.

Dave Webb
I'm gonna try this - I do use stringbuilder - typo .. Yep new StringBuilder does the trick. Thanks for the explanation
Alex Volovoy