dimas
dimas

Reputation: 2597

Java alternatives with toString conversion from StringBuilder

I have this piece of code that converts an ArrayList of StringBuilders into an ArrayList of Strings:

Code:

public ArrayList<String> convGenSeqToString(ArrayList<StringBuilder> buff){
        ArrayList<String> convBuf = new ArrayList<String>();
        
        for (StringBuilder xVar: buff){
            convBuf.add(xVar.toString());
        }
        return convBuf;
    }

My code just works fine for files which are 15-20MB text files. However I have a text file which is 44MB and whenever I run my program with that text file I always get this error.

Error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2746)
    at java.util.ArrayList.ensureCapacity(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:378)
    at Recognition.convGenSeqToString(Recognition.java:157)
    at Recognition.genSeq(Recognition.java:145)
    at Recognition.Recognitions(Recognition.java:96)
    at ChainDetection.main(Detection.java:25)

I already increased the memory in the JVM Run Configuration with -Xmx2048M and still I have the same error. I pinpointed the error to the code I displayed above and its highlighting this line:

convBuf.add(xVar.toString());

Are there any other ways to convert a StringBuilder to a string without using the .toString() method? I have seen in other forums and here as well that they created custom classes for toString but am not yet well versed with Generics and some "@" keywords. Anyone have suggestions or guidelines for how to solve this?

Edited

I edited my code as suggested by vanza here it is:

public ArrayList<String> convGenSeqToString(ArrayList<StringBuilder> buff){
        ArrayList<String> convBuf = new ArrayList<String>(buff.size());
        Iterator <StringBuilder> iterBuf = buff.iterator();
        
        while (iterBuf.hasNext()){
            StringBuilder x = iterBuf.next();
            convBuf.add(x.toString());
            iterBuf.remove();
        }
        return convBuf;
    }

Error Message:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.ArrayList.<init>(ArrayList.java:132)
    at Recognition.convGenSeqToString(Recognition.java:154)
    at Recognition.genSeq(Recognition.java:145)
    at Recognition.Recognitions(Recognition.java:96)
    at ChainDetection.main(ChainDetection.java:25)

I tried this with buff.size() and without buff.size() and I got the same error message. But it seems like my error message decreased to 5 now. Any other thoughts on this? Thanks in advance!

Upvotes: 1

Views: 2638

Answers (2)

Sergii Zagriichuk
Sergii Zagriichuk

Reputation: 5399

There are two ways to get value from StringBuilder

  1. First use Reflection and call package protected method getValue() It is hard and I do not think that is so good.
  2. Second call method getChars

BTW, you will have arrays of chars, and for use string you will create new instances of string, and it will use your memory.

Upvotes: 1

vanza
vanza

Reputation: 9903

Without knowing more about your program, I'll refrain from making any comments about the approach you've taken and comment only on the code you've posted. It sounds weird to me that a 44MB file is filling a 2G heap, though.

One thing you can do is allocate space in your target array up front:

ArrayList<String> convBuf = new ArrayList<String>(buf.size());

This will avoid the ArrayList resize step, which creates a copy of the existing list (and shows up in your stack trace).

Another thing you can try is to release references to the original StringBuilders while building the String array. Use an Iterator (instead of a "for each" loop), and remove the StringBuilder from the buff array using Iterator.remove() on each iteration; that way you'll free some memory for the garbage collector to reclaim if you're running out of memory.

But again, it sounds weird that you're running out of memory with such a small file. Maybe looking at your heap with jvisualvm can shed some light.

Upvotes: 5

Related Questions