!--a11y-->
Strings 
Half of the memory consumption originating from an average Knowledge Management request is related to handling strings. You can reduce the memory consumption for strings by avoiding the concatenation of strings with +.
Improved handling of strings reduces:
· Memory required for combining strings
· Time required for garbage collection
·
CPU time
required for string operations. Examples of such operations are:
<init>, append,
replace, substring, charAt, length, indexOf, toLowerCase,
toUpperCase
Concatenation of strings with + can require up to 50% additional memory. The main reason for the excessive memory consumption is that String objects are immutable. Once the have been created, they cannot be changed. As a consequence, operations that modify String objects often actually generate new objects.
For a concatenation operation, an internal StringBuffer is created. With each concatenation step, characters are added to the StringBuffer. However, if not enough space is available, a new StringBuffer, double the size of the original one, is created. The content of the original StringBuffer is copied to the new one.
In the following code sample, the concatenation operation initially requires a StringBuffer for 16 characters, then for 32 characters and finally for 64 characters.
String message = “Short test ” + “with more ” + “than 16 chars.”;
The following code improves performance, using memory for only 35 characters for the same operation. It achieves this by explicitly allocating an initial size to StringBuffer and using the append and toString methods. The String that is returned by toString does not occupy any additional space. In total, the code only uses space for 35 characters, thus saving 44% memory for the same operation.

Memory savings are possible as long as the result string is greater than 16 characters.
String s1 = “Short test ”; String s2 = “with more ”; String s3 =
“than 16 chars”;
String message = new StringBuffer(35)
.append(s1).append(s2).append(s3).toString();