Garbage Collection Process in JAVA
Garbage Collection in Java automatically allocates and deallocates memory, so that developers doesn’t need to write explicit program to do memory management. which is one of the main advantage of Java programming.
Whenever a Java programs run on the JVM, the objects are created on the heap and is a portion of memory which 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.
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 Garbage collection.
Deallocation of memory can be described in 3 basic process
2) Normal Deletion
3) 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. 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 which JVM decides. Referenced objects are moved from eden space to the first survivor space(‘Eden’ & ‘from’ —> ‘to’). Unreferenced objects are deleted when the Eden space is cleared.
- Survivor 0 (S0) & Suvrivor 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 young generation to old generation. Objects that are long-lived are eventually moved from the Young Generation to the Old Generation. As minor GCs continue to occure 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)
Minor GC $ Major GC $ Full 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 on 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,ie 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 generations to 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.