json

Archive for March, 2016|Monthly archive page

StringBuilder Hell.

In Uncategorized on March 31, 2016 at 3:37 pm

StringBuilder sucks big-time.  From (java 7 docs):

Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.”

Admittedly this is perfectly ok for most applications. But when you’re dealing with large datasets and need to assemble string contents from god knows where and how big.  What happens is StringBuilder will take up twice the memory (and processing time) because of the constant resizing and re-allocation.  Development work and testing might be ok, but once its on production and the content grows larger, StringBuilder will return to bite you.

Bottomline is StringBuilder is fast for small String sizes but terrible for large data sets.

So what to do?  This not being a unique problem to this writer, others have pointed out solutions.  One is using a Collection for the strings, which is too tedious I think, the other is to use IO Streams.  The idea is to flush the assembled strings to a file first prior and just read it back after assembly thereby eliminating the need to put it in a String buffer which can be affected by the memory issue (note all String + String code will result in a byte code that creates a StringBuilder to implement the code).

Here’s the sample code:

FileWriter fw = null;
String tempfile = System.getProperty("user.dir")+"/"+reportid+".tmp";
try
{
// write all strings to the file writer
String str = null;
while (str=do_something())
{
fw.write(str);
}
fw.close();
}
catch (Exception f)
{
f.printStackTrace();
}
finally
{
try
{
// we delete the temp file
File file = new File(tempfile);
file.delete();
}
catch (Exception f)
{
}

}

Have fun.