Java Application Performance Tuning using String Builders
Better performance is one of the most crucial factors that an end-user expects from any software application. Therefore, software developers keep the “performance of the system” as a main non-functional requirement of the final outcome, irrespective of the programming language they used for the implementation. Even though garbage collection can contribute to application performance in little, we can do meaningful advancement with garbage collection when writing the code in order to achieve the performance requirement.
What is Garbage Collection?
In day to day life, what do you mean by garbage? It is nothing but something that we have used and left as no longer useful. So what comes to our mind when the word “garbage collection” is heard — “collecting something that we have left as no longer useful”.
Similarly, garbage collection (GC) in the programming perspective is finding data objects in memory that are no longer demanded and make their space available for reuse.
Java garbage collection performs as an automatic memory management process. Once we implement a java code, it will be complied to create the byte code. That byte code will run on a Java Virtual Machine(JVM). During the Java runtime memory allocations for objects happens in Java Heap Space. When some data objects no longer demanded, the garbage collector finds these unused objects and deletes them to free up memory.
Now you might think, Why do we bother about garbage collection in Java as long as it is an automatic process. YES, you are partially correct because even though you create thousands of short-lived data objects and don’t intentionally free the memory which is allocated to them, still Java garbage collector free the memory for you.
BUT now you have loaded a huge workload to the garbage collector. Now garbage collector needs more time to do its Job. (This is intuitive right? More work -> More time)
So what we can do?? We should create and use objects wisely.
Here is One Tip: String vs String Builders Usage
String is a very common data type that is commonly used with many data structures. The important thing we should know about String is “Strings are immutable”. In other words, after an memory allocation for a string, it can’t be modified.
We simply use ‘+’ sign to concatenate various substrings to an initial substring. Even though we retrieve the modified string, the memory location allocated for the initial substring contains the same initial value. A new memory location is allocated for the concatenated string. Therefore, we need to know each plus sign(‘+’) we use to concatenate substring causes to allocate a new memory block for the modified string.
eg: I initialize a string variable called greeting with the value of “Hi” and later add some substrings to it get the final greeting “Hi, Happy Birthday, Alex!!”
String greeting = “Hi”;greeting = greeting + “,” + “Happy Birthday” + “,” + “ Alex” + “!!”;
Memory allocation in heap for the above code will be as follows. At the point of final greeting is made there are 5 objects left for the garbage collection.
String Builder is a solution for the above issue. String builders are Mutable. In other words, its value can be modified without creating new objects.
Please note: String builders are not thread-safe. The thread-safe solution is String Buffer
If we write the same example with String Builders:
StringBuilder greeting = new StringBuilder("Hi");greeting = greeting.append(“,”).append(“Happy Birthday”).append(“,”).append(“ Alex”).append(“!!”);
Now the heap looks like follows. The same memory block is modified.
Here we come to the end and this is the summary to take:
- Use String — If a string is going to remain constant throughout the program.
- Use String Builder — If a string can manipulate(change) a lot during the program execution and will only be accessed from a single thread.