Concept of Garbage Collection(GC) in Java
Garbage Collection (GC) in Java is a form of automatic memory management. The Java Virtual Machine (JVM) uses GC to identify and discard objects that are no longer needed by the program, thereby freeing up memory resources. This process helps prevent memory leaks and optimizes the performance of Java applications.
Key Concepts of Garbage Collection:
Heap Memory :
The heap is the runtime data area from which memory for all class instances and arrays is allocated. It is divided into several generations: Young Generation, Old (Tenured) Generation, and Permanent Generation (in older versions).
Generations:
Young Generation: This is where all new objects are allocated and aged. When this fills up, it triggers a minor garbage collection.
Eden Space: Most objects are initially allocated here.
Survivor Spaces: Two areas (S0 and S1) where objects are moved after surviving a garbage collection in Eden space.
Old Generation: This is where long-surviving objects are stored. When this fills up, it triggers a major garbage collection (also known as a full GC).
Permanent Generation: This is used to store metadata required by the JVM to describe the classes and methods used in the application (replaced by Metaspace in Java 8).
Garbage Collection algorithms:
Mark-and-Sweep Algorithm: The GC marks live objects during the marking phase and then sweeps up dead objects during the sweeping phase.
Copying Algorithm: Used in the young generation, where live objects are copied to a new space and the old space is reclaimed.
Generational GC: Based on the observation that most objects die young, the heap is divided into generations and collected accordingly.
Concurrent Mark-Sweep (CMS): Aims to minimize the pauses caused by GC by performing most of the work concurrently with the application threads.
G1 (Garbage-First) GC: Designed to replace CMS, G1 divides the heap into regions and performs incremental collections with a goal of predictable pause times.
Garbage Collection Process:
Minor GC:
Occurs in the young generation.
When the Eden space fills up, live objects are moved to the survivor spaces.
If a survivor space fills up, live objects are moved to the old generation.
Major GC (Full GC):
Occurs in the old generation.
Involves both marking and sweeping phases to reclaim memory.
Causes a significant pause as the application is typically stopped during the collection process.
Phases of Garbage Collection:
Marking: Identifies which objects are still in use and should not be collected.
Normal Deletion (Sweeping): Removes unreferenced objects.
Compacting: Optional phase that eliminates fragmentation by moving objects to be contiguous in memory.
Example:
public class GarbageCollectionExample { public static void main(String[] args) { for (int i = 0; i < 1000; i++) { createObject(); } }
private static void createObject() { String[] array = new String[1000]; } }
In this example:
Each iteration of the loop creates a new array object.
These objects are initially allocated in the Eden space of the young generation.
When Eden space fills up, a minor GC is triggered, moving surviving objects to the survivor spaces.
If objects survive several minor GCs, they are promoted to the old generation.
A major GC is eventually triggered to clean up the old generation.
Tuning Garbage Collection:
Java provides various parameters to tune the garbage collection process, such as:
-Xms
and-Xmx
: Set the initial and maximum heap size.-XX:NewRatio
: Adjusts the ratio of the young to old generation sizes.-XX:+UseG1GC
: Enables the G1 garbage collector.-XX:MaxGCPauseMillis
: Sets a target for maximum GC pause times.
Garbage collection in Java is a crucial process that helps manage memory automatically. By understanding its concepts, algorithms, and tuning options, developers can optimize their applications to achieve better performance and reliability.
Subscribe to my newsletter
Read articles from sujitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
sujitha
sujitha
I am a student at SRM University, AP pursuing my BTECH in CSE.