OOM playing with StringBuilder and String

Hi list

I want to check the diff between concatination partial string using stringbuffer (pmd create a suspicion around the sign + inside the stringbufffer.append).

now I write follow the example of code:

public class StringBuffererf

{

Public Shared Sub main (String [] args)

{

nrLoops long = 100000000;

StringBuffer sb = new StringBuffer();

Start long = System.currentTimeMillis ();

for (int i = 0; i < = nrLoops; i ++)

{

If (I / 1000 == 0)

{

SB = new StringBuffer();

}

SB. Append ("asd1" + "asd2" + "asd3" + "TSA4" + "asd5" + "asd1" + "asd2" + "asd3" + "TSA4" + "asd5");

}

System.out.println ("time:" + (System.currentTimeMillis () - start));

Start = System.currentTimeMillis ();

for (int i = 0; i < = nrLoops; i ++)

{

If (I / 1000 == 0)

{

SB = new StringBuffer();

}

SB. Append ("asd1");

SB. Append ("asd2");

SB. Append ("asd3");

SB. Append ("asd4");

SB. Append ("asd5");

SB. Append ("asd1");

SB. Append ("asd2");

SB. Append ("asd3");

SB. Append ("asd4");

SB. Append ("asd5");

}

System.out.println ("time:" + (System.currentTimeMillis () - start));

}

}

an interesting around this code is: partial + operations within a stringbuffer is faster then use each time an addition.

But if I increase the number of loops I get an oom.

IMHO I don't expect this, because with

If (I / 1000 == 0)

{

SB = new StringBuffer();

}

I have however, that release the object and free memory also.

I have sore around it and I've got the cause of the oom.

suspicion and or advanced are welcome

Best regards

Dietmar

The expression ' I / 1000 ' is not what you think it does.

You test not concatenation of the way you wrote it because the compiler, not the virtual machine does concatenation.

Presumably, you understand the difference between StringBuffer and StringBuilder.

Tags: Java

Similar Questions

Maybe you are looking for