17

Is there some ways how I could clone StringBuilder ? I am reading files by bits then convert these bits to ASCII chars after that I collect chars into String builder and when I have for example 8 chars I put that String Builder object into Array List. Then I clean it and again do the same. However I can't create new string builder because of memory and I can't do changes to that String builder because in Array List also that builder changes.

So I think I have to clone that String Builder and put it into Array List. There is just one problem String Builder don't have clone(). So what is my alternatives ?

Maybe someone could give some ideas what is neat way to do this considering about performance and memory.

ArrayList characters = new ArrayList(); int counter = 0;

StringBuilder sb = new StringBuilder(blockSize-1);

while (mbb.hasRemaining()) {   

char charAscii = (char)mbb.get();


    counter++;
    charCounter++;

     if (counter == blockSize){

        sb.append(charAscii);
        characters.add(sb);//sb.toString()
        sb.delete(0, sb.length());
        counter = 0;

    }else{

        sb.append(charAscii);

     }

 if(!mbb.hasRemaining()){
    characters.add(sb);
}



}
fc.close();
return characters;
TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
Streetboy
  • 4,351
  • 12
  • 56
  • 101
  • 2
    "i can't create new string builder because of memory" - a cloned StringBuilder will use a similar amount of memory, so how would this help you? – DNA Mar 01 '12 at 19:15
  • 2
    `new StringBuilder(oldbuilder.toString())` is the simplest way to copy a StringBuilder. You haven't actualy explained what you are trying to achieve, so you might be able to just store the String from the StringBuilder instead. – DNA Mar 01 '12 at 19:17
  • Yap understood that it won't help anyway. So i am out of options becouse this characters.add(new StringBuilder(sb)); gives java outOffMemory exception – Streetboy Mar 01 '12 at 19:30

2 Answers2

26

If you don't have the memory to create a new StringBuilder, then you don't have the memory to create a new StringBuilder, and cloning wouldn't change that. The only real way to copy a StringBuilder is new StringBuilder(myBuilder), or something equivalent.

If you're getting OutOfMemoryException, you'll need to either get more memory, or find some other way to reduce memory consumption.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 1
    It should be noted that this OutOfMemoryException doesn't mean the machine is out of memory, it means the JVM has hit its limit. It may be a machine limit or a JVM limit. I would recommend making sure you are telling Java to use enough memoryby passing in e.g. `-Xmx1024` – SubJunk Apr 06 '20 at 02:49
1

You can clear StringBuilder object by:

1.

sb.delete(0, sb.length()) 

2.

sb = null;
sb = new StringBuilder();

For performance, 1. is the better option. But you cannot clone it anyway. For more info about clone see

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • In 2. `sb = null;` isn't necessary since `sb = new StringBuilder();` also overwrites `sb`. Also, I would think 1 may immediately impact memory consumption since it modifies the object, whereas 2 just changes what a variable points to, leaving any original object still in memory as a possibly candidate for garbage collection. – coder34 Jun 04 '22 at 15:38
  • @LoganMay, I think 1) will make the previously allocated memory eligible for the garbage collection. – Tapas Bose Jun 12 '22 at 06:34
  • I agree that seems possible in theory. Is there any documentation you can point to that makes it explicitly clear whether the memory affected by the `delete` call is immediately freed or first has to be garbage collected? – coder34 Jun 20 '22 at 15:22