Java Garbage Collection Fundamentals: How it Works and Why it Matters

Computer memory is finite: it must be managed so that items no longer in use are removed to make space for new items. In older languages, it was the programmer’s responsibility to deallocate memory. Newer languages use garbage collection (GC). In Java, this is a process that runs in the background within the JVM.
When applications run into performance problems, one of the first things to look at is whether garbage collection is running efficiently. If not, it can become extremely resource-hungry, causing software to become unresponsive.
Java GC tuning can result in phenomenal gains in performance. This article is an introduction to GC in Java, providing an overview of how it works, and sharing useful links to Java GC tuning resources.
What is the Garbage Collector in Java?
The GC is a process within the JVM that keeps track of all allocated memory. At intervals, it identifies objects in memory that no longer have any references pointing to them. This happens:
When the programmer specifically sets an object to null;
When an object is no longer in scope, for example, the local variables of a method that has completed.
The GC then sweeps unreferenced objects from memory so it can be reused. The application pauses during some GC tasks. The duration of these pauses should be only a few milliseconds, but if the GC is badly tuned, they may take seconds or even minutes to complete. The percentage of application time taken up by these pauses is known as latency.
How Does GC work?
The GC has three phases:
Mark: Unreferenced objects are marked as finalizable, and their finalize() method, if any, is called.
Sweep: Finalized objects are swept from memory, and the memory marked as re-usable.
Compaction: Memory is compacted so that all unused space is at the back.
Some of this work can take place concurrently with other program tasks, but some, known as stop-the-world (STW) events, must pause all other running threads.
In a simplistic overview, Java memory consists of:
The Heap: the place where all objects are stored;
Metaspace: Primarily used for class information;
Other: This includes various areas such as the thread stack.
Modern GC algorithms split the heap up into smaller areas to speed up GC:
Young Generation (YG). Many variables in Java are used for only a short space of time. For example, when processing an HTTP request, various variables may be created, but they won’t be needed again once the request has been serviced. It makes sense, then to create new variables in a smaller space which is cleaned frequently, and to move any surviving objects to a larger space which is cleaned less frequently. YG itself is, in fact, divided into smaller sections:
- Eden Space: New objects are created here;
- S1 and S2: Objects that survived the first GC cycle are alternately moved to these areas.Old (Tenured) Generation (OG). Objects that survive GC in S1 and S2 are moved to this section.
Fig: Sections of the JVM heap
Garbage collection events are classified into Minor, where only the YG is cleaned, and Major, where all memory is cleaned.
Garbage Collection Algorithms
Seven different GC algorithms are available in the JVM, each suited to different types of application. Some of these are now obsolete or deprecated. This article describes the seven algorithms, discussing what each is best suited to, with links to tuning guides for each.
Java GC Tuning
If GC is not performing as it should, there are various ways of tuning it using JVM command line arguments.
These include:
Adjusting the size of memory areas;
Selecting and tuning the GC algorithm;
Refactoring the code for efficiency.
Here are some useful tuning resources:
Garbage Collection VM Options Simplified
For detailed analysis of how GC is performing, the GCeasy tool is an invaluable resource.
Conclusion
It’s important to understand how garbage collection works, both for performance tuning and for writing efficient code.
This article has looked at the principles of garbage collection and Java GC tuning.
You can explore the topic further by reading these articles.
Subscribe to my newsletter
Read articles from Jill directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
