What is Java Garbage Collection?
Java application creates several objects to service the new incoming requests. For instance, when a customer signs on, our applications creates following objects: an HTTP request object, HTTP session object, HTTP filter object, Servlet object, customer object (which internally includes username and password string objects), a DAO object for backend database interaction, a connection object for database connection, and several such objects.
Once the request is serviced, these newly created objects will become useless. Because these objects are specific to a particular customer sign on request and it can’t be re-used. Now these objects are called ‘Garbage’.
This garbage has to be removed from memory. Because your memory is a finite space, it needs to free up to service the other new incoming requests. The process of removing such unreferenced objects from memory is called ‘Garbage Collection’.
Garbage Collection – Life partner of your application
GC is like a life partner of your application. Seriously 😊, there are a lot of similarities:
Just like how choosing the right life partner is essential for the happiness of your life, choosing the right Garbage Collector and GC settings is essential for a high-performing application.
There are 7 GC algorithms in OpenJDK. Here is a post which can assist you in choosing the right Garbage Collector algorithm for your application.
Garbage Collection Evolution
In earlier days, especially in the languages like C and C++, to service the new incoming requests, developers had to manually allocate and as well deallocate memory using ‘malloc()’ and ‘free()’ APIs, respectively. Once the request is serviced, they need to write code to deallocate those objects from memory. Business applications are complex, there are multiple workflows and use cases. If a developer misses to deallocate objects in just one workflow, then those objects will start to build up in the memory, eventually causing OutOfMemoryError. Thus, OutOfMemoryError was quite pervasive in those days.
When Java was introduced in 1995, it promised automatic garbage collection. While some languages before Java also had automatic garbage collection, Java really championed it. Java developers only need to allocate objects; and deallocation is handled by the Java runtime environment. Developers love this feature because it reduces memory leaks and allows them to focus on business logic. From that point on, all modern programming languages come with this automatic garbage collection capability.
Side-effects of Automatic GC
So far this automatic garbage collection sounds good right? It’s good, but it comes with two side effects:
a. Response Time degradation
b. High CPU consumption
Recommended by LinkedIn
a. Response Time degradation
To do automatic garbage collection, Java Runtime Environment has to keep track of all the objects created in memory. It should identify what are the active objects and inactive (i.e. unreferenced) objects and sweep them out of memory. To do this, the Garbage Collector pauses your application. This pause can span from a few milliseconds to several seconds (sometimes even minutes). During the pause window, your customer transactions are not processed. Thus, your application’s response time will degrade.
b. High CPU consumption
Garbage Collection is a highly CPU-intensive operation. Modern applications create a large number of objects. All those objects must be constantly tracked. Their incoming references, outgoing references need to be constantly checked. Unreferenced objects need to be identified and removed from memory. As a result, automatic garbage collection consumes enormous amounts of CPU cycles. If your application consumes 70% CPU, it’s likely that more than 40% of CPU cycles are consumed by Garbage Collection.
GC Tuning
Tuning Garbage Collection performance is essential to address above mentioned two side effects. Here is a real-case study of a large automobile manufacturer who tuned their GC settings and improved their response time by 49.6%. Another real case study is from Uber, a large ride-sharing application, who tuned their GC settings and reduced their CPU consumption by 70,000 CPU cores, ultimately saving millions of dollars in their cloud hosting bill. Learn more GC tuning success stories from here.
Interested in optimizing your application’s GC performance? Check out our in-depth guide ‘9 Tips to Reduce Your GC Pauses‘.
GC Analysis Tools
Best way to study and tune the GC performance is through GC Log analysis tools such as GCeasy, IBM GC Visualizer, HP JMeter, Garbage Cat. For example, tools like GCeasy identifies the GC and memory bottlenecks in your application and reports it as shown below:
This tool also reports the appropriate recommendation to enhance your GC performance. Here is a live GC log analysis report generated by GCeasy. You should consider checking it out to see the insightful GC graphs, KPIs and GC tuning recommendations it reports.
Conclusion
Garbage Collection optimization brings several hidden benefits to your organization. I would like to strongly encourage you to learn this lifetime skill and become a superhero in your organization.