DEV Community

Cover image for Java Garbage Collecting

Posted on


Java Garbage Collecting

Hello everyone, for today’s post I am going to discuss garbage collecting for Java. For those that don’t know, garbage collecting is the process of clearing up memory in the application at runtime. As objects are created over the life cycle of the program they will eventually take up too much space. In programming languages like C or C++ programmers will have to manage memory on their own. Java is different in that it handles it in house and will delete object that are no longer referenced automatically in the JVM.

Each JVM can implement its own form of garbage collecting as long as the type chosen meets the requirements of the JVM. The most common garbage collector is Hotspot by Oracle. Hotspot uses a generational garbage collector method that categorizes objects by the number of collections cycles it has lived through. Hotspot’s philosophy is that most objects are short lived and in order to increase efficiency most passes of the garbage collector should just look at newer objects since they are most likely to be deleted. Looking at the graphic below, Hotspot breaks up the memory heap into sections called the young generation, old generation, and permanent generation.


The young generation consists of the eden, S0, and S1 blocks. Eden is where new objects are stored. They are frequently deleted in this phase since many are no longer used each collection cycle. Objects that live past the first cycle move into S0 or S1 depending on which one is the survivor block for the rotation.


Objects already in one of the survivor blocks move between them together with each rotation they survive.


If an object has aged a specific threshold of cycles it then moves on to the tenured block. Minor garbage collecting cycles of the young generation do not pass over this part of the heap. Eventually, a major garbage collection event will occur which will clean up and compact this space as well.

The permanent generation is used to contain metadata and is populated by the JVM at runtime. This data labels and describes the classes and methods used by the program. Java standard libraries are also stored in this section. Classes here can be removed during a full garbage collection if the JVM determines they are no longer needed and space is required for other classes.


  1. Altvater, A. (2017, May 11). What is Java Garbage Collection? Best Practices, Tutorials & More. Stackify.,Machine%2C%20or%20JVM%20for%20short.&text=The%20garbage%20collector%20finds%20these,them%20to%20free%20up%20memory.

  2. Oracle. (n.d.). Java Garbage Collection Basics.

Top comments (0)