Garbage Collection in Java (JVM)
Garbage Collection in Java automatically allocates and deallocates memory, so that developers don’t need to write an explicit program to do memory management.
Join the DZone community and get the full member experience.Join For Free
Garbage Collection Process in Java
Garbage Collection in Java automatically allocates and deallocates memory, so that developers don’t need to write an explicit program to do memory management, which is one of the main advantages of Java programming.
Whenever a Java program runs on the JVM, the objects are created on the heap and are a portion of memory that is dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.
The garbage collector will look at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. An in-use object, or a referenced object, means that some part of your program still maintains a pointer to that object.
An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory which is used by an unreferenced object can be reclaimed by performing a Garbage Collection.
Deallocation of memory can be described in 3 basic processes:
- Normal Deletion
- Deletion with Compacting
Marking — Process of identifying the pieces of memory which are in use and are not by Garbage collector and is the first step
Normal Deletion — Process of removing unreferenced objects leaving referenced objects and pointers to free space.
Deletion with Compacting — In addition to deleting unreferenced objects, it will compact the remaining the referenced objects by moving the objects together to make the
new memory allocation much easier and faster.
JVM Heap Memory
Newly created objects start in the Young Generation. The young generation is also called nursery as the new object start living here. The Young Generation is further subdivided into an Eden space, where all new objects start, and two Survivor spaces, where objects are moved from Eden after surviving one garbage collection cycle. These are call Minor garbage collection event when objects are garbage collected from the Young Generation,
- Eden Space
All new objects are first created in the Eden Space. A minor GC collection will kicks in when it reaches a threshold that JVM decides. Referenced objects are moved from Eden space to the first survivor space(‘Eden’ and ‘from’ —> ‘to’). Unreferenced objects are deleted when the Eden space is cleared.
- Survivor 0 (S0) and Survivor 1 (S1)
Both survivor spaces(From and to) start out empty. When a minor GC collection happens all the referenced objects are moved to survivor space. Once the GC is over, the Survivor spaces ‘from’ and ‘to’ roles (names) are swapped. S1 was the ‘to’ role during the previous garbage collection (GC). Now S1 is filled and takes the ‘from’ role and S0 is empty and will take the ‘to’ role.
After a minor GC, when aged objects reach a certain age threshold(By default, modern JVMs threshold is set to 15 GC cycles) they are promoted from the young generation to the old generation. Objects that are long-lived are eventually moved from the Young Generation to the Old Generation. As minor GCs continue to occur, objects will continue to be promoted to the old generation space and it will start getting filled and a Major GC will occur. Major garbage collection will happen when the objects are garbage collected from the Old Generation.
Metadata such as classes and methods are stored in the Permanent Generation. Classes that are no longer in use may be garbage collected from the Permanent Generation. During a full garbage collection event, unused objects in all generations are garbage collected.
Types of Garbage Collection (GC)
The Garbage Collection events cleaning out different parts inside heap memory are often called Minor, Major, and Full GC events. But, as the terms Minor, Major, and Full GC are widely used and without a proper definition, we will have a look at the explanation of all these GC.
Collecting garbage from the Young Generation space is called Minor GC. Minor GC cleans the Young Generation. Minor GC is always triggered when the JVM is unable to allocate space for a new object, i.e., when the Eden space is getting full. So the higher the allocation rate, the more frequently Minor GC occurs.
Major GC is cleaning the Tenured (Old space). As OLD Gen is bigger in size, the GC occurs less frequently than in the young generation. When objects disappear from the old generation, we say a 'major GC' has occurred. The old generation collector will try to predict when it needs to collect to avoid a promotion failure from the young generation.
The collectors track a fill threshold for the old generation and begin collection when this threshold is passed. If this threshold is not sufficient to meet promotion requirements then a 'FullGC' is triggered.
Full GC is cleaning the entire Heap — both Young and Old spaces. Many people get confused with Major (Only OLD generation) and Full GC (Young + OLD(Heap)). A FullGC involves promoting all live objects from the young generation to the OLD generation after collection and compaction of the old generation. Full GC will be a Stop-the-World pause. Stop-the-World is making sure that new objects are not allocated and objects do not suddenly become unreachable while the collector is running.
To identify the Heap Memory usage using Jmap and Jstat: Please refer to the below article.
Please refer to below for Garbage collection:
Opinions expressed by DZone contributors are their own.