Java Garbage Collection_ What is It and How Does It Work_ - Naukri Code 360
Java Garbage Collection_ What is It and How Does It Work_ - Naukri Code 360
Introduction
Java garbage collection frees up unused memory by releasing objects that are no longer needed.
When there's no reference to an object, it's automatically removed, a process known as garbage
collection. It also helps the performance of your Java environments and applications.
This article provides an in-depth understanding of the Garbage Collector in Java, including its
functionality and the example available.
Stack memory is used for static memory allocation and manages function call stack, while heap
memory is used for dynamic memory allocation and does not have a defined organizational
structure.
Garbage collection is done on heap memory because it is used for dynamic memory allocation,
where objects are created and destroyed at runtime. The garbage collector identifies and removes
objects no longer referenced by the program.
When you run a program in Java, the computer allocates a specific area of memory called a "heap"
to store all the data generated by that program. As the program runs, it creates new objects and
stores them in a heap. Sometimes the program stops using specific resources but still
occupies heap space. The term "dead" refers to these unused items. A built-in Java feature called
the garbage collector checks the heap regularly for dead items and deletes them if necessary. By
doing this, the program frees up memory for later use. To conclude, garbage collection is a tool for
memory cleanup that eliminates items that aren't being used, enabling programs to use memory
more effectively.
https://www.naukri.com/code360/library/java-garbage-collection 1/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
Mark
The garbage collector first marks all reachable objects from the root objects. Root objects include
references from the program’s main method, static variables, and thread stacks.
Sweep
In this step, the garbage collector scans the heap memory and searches for unmarked objects.
Unmarked items are considered garbage, and the garbage collector eliminates the residue
occupied by these items.
Compact
After the garbage collector clears the heap memory, it may need to move the remaining items into
a consecutive memory block. This step is called compaction, and it helps optimize memory usage.
The Java Virtual Machine performs periodic garbage collection, depending on the program's
needs. Developers can also manually request garbage collection by calling the System.gc()
method, although this is not always recommended as it can negatively impact performance.
https://www.naukri.com/code360/library/java-garbage-collection 2/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
1. Young Generation
Eden Space: This is where new objects are initially allocated.
Survivor Spaces: There are typically two survivor spaces, often referred to as "From" and "To"
spaces. When objects in the Eden Space survive a garbage collection cycle, they are moved to
one of the survivor spaces.
Memory Management: Developers need not manually allocate and deallocate memory,
reducing the risk of memory leaks and segmentation faults.
Simplicity: GC automates memory management, allowing developers to focus on application
logic rather than memory management intricacies.
Prevents Memory Leaks: GC identifies and reclaims memory that is no longer referenced,
preventing memory leaks and improving application stability.
Dynamic Memory Allocation: It allows for dynamic memory allocation, enabling applications
to adapt to changing memory requirements during runtime.
Platform Independence: GC is an integral part of the Java Virtual Machine (JVM), making Java
programs platform-independent. Developers don't need to worry about low-level memory
management differences across platforms.
Memory Pressure
Allocation Threshold
System.gc() Method
Generation Space Filling Up
https://www.naukri.com/code360/library/java-garbage-collection 3/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
collection.
1. Young Generation: The Young Generation heap is where newly created objects are allocated.
It is divided into two parts, called Eden and Survivor spaces. Garbage collection in the Young
Generation heap is typically performed using a minor garbage collector, which identifies and
removes short-lived objects no longer needed.
The younger generation is divided into 2 types:
Eden Space: Eden Space is where new objects are created. A minor garbage collection
removes short-lived objects that are no longer referenced. Surviving objects move to Survivor
Space.
Survivor Space: Survivor Space is where objects that survive one or more minor garbage
collections are moved. It includes two spaces, S0 and S1, and objects that survive multiple
collections are moved to the Old Generation heap.
2. Old Generation: The heap stores long-lived objects, and garbage collection is typically
performed using a major garbage collector. This heap is usually larger than the Young Generation
heap, and it's designed to store objects that survive garbage collection in the Young Generation.
Depending on the JVM implementation, the Old Generation heap can be divided into Permanent
Generation and Tenured Generation.
Null Value
Implementation:
class MyClass {
int[] myArray = new int[10000];
}
In this example, once the obj reference is set to null, the object of MyClass is no longer accessible
and can be freed up by the garbage collector.
Object References
Implementation:
https://www.naukri.com/code360/library/java-garbage-collection 4/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
// obj1's original object is no longer accessible and is eligible for garbage collection
}
In this example when obj1 is set to obj2, the original object that obj1 referred to becomes
inaccessible and is eligible for garbage collection.
Memory Leaks
Implementation:
In this example, obj is not set to null, and a new object is created every time the loop runs. This can
lead to a memory leak as objects accumulate in memory and are not eligible for garbage
collection.
Implementation:
In this example, we make an anonymous MyObject object and call its dothing() method. Then,
using System.gc(), we request garbage collection, wait for it to complete using Thread.sleep(2000),
and print a message indicating that garbage collection has been completed.
This approach can be useful when you have a small amount of code that creates a short-lived
object you don't need to reuse or store as a named variable.
Implementation:
Java
import java.util.Scanner;
https://www.naukri.com/code360/library/java-garbage-collection 5/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.println("You entered: " + input);
You can also try this code with Online Java Compiler Run Code
Input:
Output:
Enter a string: You entered: What is Garbage Collection in the Context of Java
Garbage collection completed.
Explanation:
In this example, we use a Scanner object to get user input for a string. We then print the user's
input and set the input variable to null to make it eligible for garbage collection. Next, we
call System.gc() to request garbage collection and wait for 1 second to ensure that garbage
collection is complete. Finally, we print the message "Garbage collection completed."
When memory is required in such languages, the programmer must allocate it and deallocate it
when it is no longer required. Manual memory management, however, can be prone to mistakes
and result in memory leaks, where memory is improperly deallocated and continues to be
allocated even when it is no longer needed.
The memory management process is automated by garbage collection, relieving the programmer
of manually managing memory allocation and deallocation. Algorithms for garbage collection
identify memory no longer being used by the program and automatically reclaim it.
Note:
Programming languages that use manual memory management may experience the issue of
dangling Learn
pointers,Contests
which happens
& Events
when a pointer points
Interview prep
to a memory
Practice Resources
location that has already
Login
been deallocated. This can result in unpredictable behavior and program crashes.
Dangling pointers are a problem, but garbage collection largely solves this issue. Garbage
collection algorithms keep track of the memory locations the program is still using and release
memory that is no longer required. As a result, there are no longer any "dangling" pointers pointing
to deallocated memory because the garbage collector ensures that all memory is properly Live mast
arbage Collection? managed and cleaned up.
age Collection …
IIT O
by C
https://www.naukri.com/code360/library/java-garbage-collection 6/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
Generational Ga… Java garbage collection (GC) best practices include: 6+ registered
neration Understand GC Algorithms: Learn about different GC algorithms (e.g., Serial, Parallel, CMS,
ration (Tenured …
G1) and their characteristics to choose the best fit for your application.
Monitor GC Activity: Use monitoring tools like JVisualVM, JConsole, or GC logs to analyze GC
nt Generation (pr… behavior, identify patterns, and optimize GC settings accordingly.
Tune GC Settings: Adjust GC settings (e.g., heap size, GC algorithms, GC threads) based on
Garbage collec…
application requirements, workload characteristics, and available hardware resources.
ava garbage col… Minimize Object Creation: Reduce unnecessary object creation by reusing objects, using
object pools, and avoiding excessive use of temporary objects.
tegies of Java G… Avoid Object Finalization: Minimize or avoid using finalize() method as it can cause
ge Collector
performance overhead and unpredictability in GC behavior.
Use Generational GC: Leverage the generational GC concept to separate short-lived objects
bage Collector (young generation) from long-lived objects (old generation) and optimize GC performance
accordingly.
rrent Mark Swe…
e-First) Garbage…
Advantages of Garbage Collection
rbage Collectio…
ntation: Memory Efficiency: Garbage collection can improve memory efficiency by recovering unused
Anonymous Obje…
memory items, allowing other tasks to be performed.
ntation: Reduced Development Time: Garbage collection eliminates the need for manual manipulation
of memory, which can save significant development time. This allows developers to focus on
bage Collection
other aspects of software development.
tion:
Improved Performance: Garbage collection can improve application performance by reducing
the frequency of memory allocation and deallocation operations. This, in turn, can help reduce
age Collection
the fragmentation that can negatively impact performance.
collection best p… Security: Garbage collection can help prevent security vulnerabilities like buffer overflow
attacks and other memory issues.
f Garbage Colle…
Garbage collection requires additional processing time and system resources to monitor and
control memory usage. This might result in slower program execution and more memory
usage.
Garbage collection can make it challenging to anticipate when memory will be recovered.
Non-deterministic Behavior. This may make it more challenging to write predictable and
dependable code.
Some garbage collection algorithms necessitate "stop the world" pauses to free up memory.
This can result in observable program pauses for real-time or interactive applications, which
can be particularly problematic.
Memory fragmentation can result from garbage collection, in which free memory blocks are
dispersed throughout the heap. As a result, the program may have trouble allocating large
blocks of memory and use memory inefficiently.
https://www.naukri.com/code360/library/java-garbage-collection 7/10
1/8/25, 11:37 PM Java Garbage Collection: What is it and how does it work? - Naukri Code 360
Major and minor garbage collection are specific types of generational garbage collection
algorithms.
Minor garbage collection focuses on the younger generation of memory and is triggered more
frequently to reclaim memory from short-lived objects quickly.
Major garbage collection focuses on the older generation of memory and is triggered less
frequently to reclaim memory from long-lived objects.
finalize() Method
The finalize() method is called just before an object is destroyed and removed from memory. It can
be utilized to perform any necessary cleanup actions.
Syntax:
Note:
“The Java Virtual Machine's garbage collector only collects objects that were created using the
"new" keyword. If you have created an object without using "new", you can utilize the finalize()
method to perform cleanup tasks, such as destroying any remaining objects.”
gc() Method
The gc() method can be used to trigger the garbage collector to perform cleanup operations. It is
commonly available in the System and Runtime classes of programming languages. Its purpose is
to help reclaim memory no longer needed by the program and prevent memory leaks.
Syntax:
Conclusion
Garbage collection is an essential feature of the Java programming language that automatically
manages memory allocation and deallocation. It eliminates manual memory management and
https://www.naukri.com/code360/library/java-garbage-collection 8/10