Java String Representation

String has four state members: offset, count (number of characters to be used), char[], hashcode.

substring() is designed to perform in O(1) by reusing the original char[]. This can lead to memory leak if the original String is big and the substring() is used to create a small String.  A work around is to pass the result of substring() to a new String(substring) constructor.

Starting Java 7 (1.7.0_06) Oracle got rid of offset & count fields, to fix the memory leak. This leads to substring performing in  O(n).

http://www.infoq.com/news/2013/12/Oracle-Tunes-Java-String

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s