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 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.


Asked by: Robert932 | Posted: 24-01-2022






Answer 1

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.

Answered by: Daisy472 | Posted: 25-02-2022



Similar questions

android, iterate over stringbuilder json?

So my webservice returns a bunch of JSONObjects, which is turned into a string by my stringbuilder. [{"DateID":"2011-02-01","DateName":"februari 2011","IntName":1102,"Consumption":172,"Temperature":null},{"DateID":"2011-01-01","DateName":"januari 2011","IntName":1101,"Consumption":316,"Temperature":null}] How can I get this String to be iterable så I can get all the name/values inside? I'...


android - How to trim a java stringbuilder?

I have a StringBuilder object that needs to be trimmed (i.e. all whitespace chars /u0020 and below removed from either end). I can't seem to find a method in string builder that would do this. Here's what I'm doing now: String trimmedStr = strBuilder.toString().trim(); This gives exactly the desired output, but it requires two Strings to be allocated instead of one. Is the...


android - Filtering StringBuilder output - Java

Is there any way i can filter out lines containing a certain word that i do not want? For example an example output would be E/MediaPlayer( 7616): error (1, -2147483648) D/MediaPlayer( 7616): create failed: D/MediaPlayer( 7616): java.io.IOException: Prepare failed.: status=0x1 D/MediaPlayer( 7616): at android.media.MediaPlayer.prepare(Native Method) D/MediaPlayer( 7616): at android.media.MediaPlayer.cr...


Out of Memory Exception for String, StringBuffer and StringBuilder in Android

I'm facing an Out of Memory Exception while converting a 1.8MB image to bytes and then encrypt, finally converting into a string (length printed in log is 1652328). And then, I'm appending this string to some XML format to post, where the real problem arises. While appending some tags to this pictureString using StringBuffer or StringBuilder or adding to a string Out of Memory exception


How to flush the value of StringBuilder variable in android?

I am working on an android app in which i have a static StringBuilder variable named DT_selected.I want to flush this variable's value on a checkbox check.Anyone have any idea or experience how to do it?


Android StringBuilder vs String Concatenation

I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html. The section here caught my eye: Tip: Don't forget that when you make a call like Log.v(TAG, "index=" + i); that when you're building the string to pass into Log.d, t...


android - Using StringBuilder for appending HTML tags

Issue with StringBuilder I have used the table tag in sb.append but it is not showing table can any one help me out of this? StringBuilder sb = new StringBuilder(); sb.append("<table><tr><td>A</td> <td>B</td> <td>C</td> </tr><tr><td>X</td> <td>Y</td> <td>Z</td> </tr></table> "); Spanned ...


android - Dalvik-vm Out Of Memory while appending string with StringBuilder

My App receives image data from server as XML. During paring the XML, I append the data using StringBuilder, decode the image data and then display the image. If the image size is greater than 1MB, I get Dalvik-vm "Out Of Memory' exception as below: 07-16 19:23:35.376: E/dalvikvm-heap(374): Out of memory on a 10501076-byte allocation. 07-16 19:23:35.376: I/dalvikvm(374): "AsyncTask #2" prio=5 tid=10 RUNNABL...


java - Adding an Object to StringBuilder

I'm pretty new to Android development, and what I'm trying to do is display the contents of an ArrayList in a TextView. I've been attempting to convert the ArrayList into an Array, and then append each item to a StringBuilder. However, the StringBuilder doesn't appear to allow me to append an Object from the Array


android - Strange ANR in StringBuilder

ANR stack from Android Developer Console: DALVIK THREADS: (mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0) "main" prio=5 tid=1 SUSPENDED | group="main" sCount=1 dsCount=0 obj=0x40022198 self=0xcec8 | sysTid=15809 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1345006496 | schedstat=( 105169616659 16803131154 25724 ) at java.lang.String._getChars(String.java:~1041) at java.lang.AbstractStringBuilder...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top