Many developers do not realize the importance of understanding how Garbage Collection works in Java. Many books state that Java performs “Automatic Garbage Collection” which is unfortunately misunderstood and taken by granted for developers.
While it’s true that Java offers a very effective garbage collection technique, it is the duty of a programmer to ensure that their code does not hog memory and is free of memory leaks.
Why is Garbage Collection needed?
In real life, when a thing outlives its utility, we treat it as garbage/ trash. JVM follows a similar principle for source code. When an object is no longer referenced, its heap space can be freed and reused by new objects.
The sole purpose of Garbage Collector is to locate such objects which are candidates for freeing their associated memory and then to release the memory associated with them back to JVM. The Garbage Collector detects if an object is no longer referenced by defining a set of roots and determining if the object is still searchable from the root.
The Java Garbage Collector has secondary advantages that it protects fragmentation of heap and preserves the integrity of programs.
What does System.gc() do?
It is rather unfortunate that Javadoc states the objective of System.gc() is to “Run the garbage collector”. The call to this method does not trigger garbage collection. It is merely a suggestion to JVM that it might choose to run the garbage collection routine. As a programmer, there is absolutely NO way you can enforce or guarantee that the garbage collector will run for sure.
Is it recommended to call System.gc() in applications?
Many programmers believe that since System.gc() runs the Java Garbage Collector, it will improve the performance of their applications. However, this is far from the truth. In fact, calls to System.gc() can end up degrading the performance of your applications.
Calls to System.gc() can put a heavy load on CPU if the number of live objects to be cleaned up is very high. The problem is more prominent in case of HotSpot JVM which stops all application threads till the entire heap is recollected. This can be a perfect recipe for disaster for time sensitive applications. In such cases, the JVM is best judge on when to invoke the Garbage Collector.
Further, many programmers are not even aware that their code may already be calling System.gc() through hidden calls. It is recommended that the JVM be invoked with XX:+DisableExplicitGC flag in order to isolate such calls to the Garbage Collector.
Mark and Sweep
The core principle behind Java’s Garbage Collector routine is known as ‘Mark and Sweep’. With advances in JVM, the principle of Mark and Sweep has also evolved. Several advanced versions of this algorithm have been introduced including the Concurrent Mark and Sweep (CMS).
The basic principle of all Mark and Sweep algorithms is however the same. It works in 2 stages.
- Mark - Identify the list of objects that need to be freed with black, white and grey flags. The white objects are candidates for Garbage Collection. The black objects are those which have no references to the objects in white set. Initially, the blank set is blank and does not have any objects. The grey objects are those which are reachable from root references but the objects they refer to are yet to be scanned. When the Mark process begins, it picks up each grey objects one by one and moves it to the black set. It then moves all white objects referred to by this object and moves them to the grey set. This iteration is repeated till Grey set is empty.
- Sweep - Sweep and release the memory associated with the objects that have been marked with white flag as they are no longer reachable through the root set.
Conclusion
Java’s Garbage Collector is a boon when compared to the manual garbage collection that needs to be done for other object oriented languages like C++. Often, C++ applications are susceptible to memory leaks and performance degradation as a result of implementation of poor manual garbage collection routines.
If you enjoyed this post, make sure you subscribe to my RSS feed!
Comments