On the Eclipse Developer mailing list there were a couple of emails mentioning some Java String memory quirks.
The emails exchanged were as follows:
1st Email
I just noticed that String#substring(...) shares the underlying char array.
So if you have a big string, take a substring of it, throw away the big
string, you will still hold on the big char array.
A simple way to solve this is to make a copy of the substring using the
String(String) constructor.
I saved 550KB in JDT Core by changing the following code:
String simpleName =
fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.'));
to
String simpleName = new
String(fullyQualifiedName.substring(fullQualifiedName.lastIndexOf('.')));
2nd Email
Along the same line, when creating String from some char[] fragment, you
should prefer doing:
char[] original = ...;
new String(original, start, length);
instead of:
char[] original = ...;
char[] fragment;
System.arraycopy(original, 0, fragment = new char[length], start,
length);
new String(fragment);
This saves the transient char[] allocation.
A later email pointed to a paper on such matters:
Paper on Strings