Java: Garbage Collector

Garbage collection is one of the big advantages of Java. Many languages like C and C++ do not offer an automatic mechanism to perform memory management. Manually releasing every object which is not being used anymore could become a fastidious and a really difficult task.

When memory is not correctly managed, objects can stay allocated indefinitely, making applications to consume more memory than what they actually need. This can lead to performance and memory management issues, and eventually make the application crash. Such problem is known as memory leak.

It is the JVM that decides when to run the garbage collector. Throughout a Java program, it’s possible to ask the JVM to run the garbage collector using System.gc(). Nevertheless, there are no guarantees that the request will be attended. So, it’s not a good practice to design an application relying on the execution of the garbage collection.

In Java, objects are allocated on the heap space. The mission of the garbage collector is to make sure that the heap has as much free space as possible. It ensures that the available memory will be managed efficiently, but it cannot guarantee that there will be always enough space.

It is important to identify when an object is eligible for garbage collection. In Java, an object is eligible for deletion when it cannot be accessed by any live thread. In other words, if all the references to an object cannot be reached by a live thread the object is marked for collection. Obviously, if there are no reachable references to an object, we don’t care about that object anymore.

There are basically three ways to mark an object for garbage collection:

Finally, it is also possible to execute some operations just before an object is deleted by the garbage collector. In order to do that we need to override the method finalize() inherited from class Object. But be careful, as we have no guarantees about when the GC will run, the code inside the method finalize() can wait a very long time to be executed. For that reason, it is recommended to not use the finalize() method to release resources allocated by an object. Use an explicit method call for that instead.