StringBuffer.build() - a way to create strings without unnecessary allocations

Jaroslav Tulach jaroslav.tulach at oracle.com
Wed Dec 8 23:03:39 UTC 2010


Hello.
Can anybody hear me? I'd like to talk to someone who cares about String, 
StringBuffer and StringBuilder. I am NetBeans Platform architect and I'd like 
to use this opportunity to fasten start of NetBeans as well as improve OpenJDK 
project. Here is my story:

I've been investigating the memory allocations during NetBeans start. I've 
noticed that we are allocating about 60MB of char[] mostly due to parsing of 
various configuration files. Suprisingly at least half of these 60MB is 
garbage collected quickly. This is because we use StringBuffer/Builder to 
concatenate the texts and then convert them to String. The conversion to 
String however allocates new array. I believe this is less then optimal. 
However there is no way I can tell the Java libraries to be more effective.

That is why I decided to create my patch. It adds StringBuffer.build() method 
which can create Strings without unnecessary copying. It adds 
StringBuilder.build() method too, for symetry, but that one is not that 
effective (I don't know how to make it robust without usage of 
synchronization). If I had StringBuffer.build, I could allocate only 30MB of 
data on NetBeans startup and very likely make it faster. Can you consider 
adding this method to JDK7?

I am attaching my patch that adds the new API. I can refine it, add new tests, 
etc. I just need a buddy interested in StringBuffer to guide me. I belive that 
together we can make NetBeans start faster and OpenJDK7 better. Can you help 
me?

-jt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: StringBuffer.build.diff
Type: text/x-patch
Size: 3032 bytes
Desc: not available
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20101209/5ed14d74/StringBuffer.build.diff>


More information about the core-libs-dev mailing list