Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Java garbage Collection?
3.
How does Garbage Collection Works in Java?
3.1.
Mark
3.2.
Sweep
3.3.
Compact
4.
Memory Heap Generational Garbage Collection
4.1.
1. Young Generation
4.2.
2. Old Generation (Tenured Generation)
4.3.
3. Permanent Generation (prior to Java 8)
5.
Benefits of Java Garbage collection
6.
What triggers Java garbage collection?
7.
Types and Strategies of Java Garbage Collection
7.1.
Serial Garbage Collector
7.2.
Parallel Garbage Collector
7.3.
CMS (Concurrent Mark Sweep) Garbage Collector
7.4.
G1 (Garbage-First) Garbage Collector
8.
Examples of Garbage Collection in the Context of Java
8.1.
Null Value
8.2.
Object References
8.3.
Memory Leaks
8.4.
Adding an Anonymous Object to the Code
9.
Example of Garbage Collection
9.1.
Java
10.
Need for Garbage Collection
11.
Java garbage collection best practices
12.
Advantages of Garbage Collection
13.
Disadvantages of Garbage Collection
14.
Types of Generational Garbage Collector
15.
finalize() Method
16.
gc() Method
17.
Frequently Asked Questions
17.1.
How do you handle garbage collection in Java?
17.2.
Is it possible to disable Java's garbage collection?
17.3.
Does garbage collection affect performance in Java? 
17.4.
What is garbage collection in the context of Java answer?
17.5.
What is garbage collection with example?
18.
Conclusion
Last Updated: Mar 27, 2024
Medium

Java garbage collection: What is it and how does it work?

Author Kanak Rana
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

garbage collection in the context of java

What is Java garbage Collection?

Garbage collecting in Java is a built-in mechanism that reclaims the memory that is no longer needed by the program. A Java Virtual Machine (JVM) handles memory allocation and deallocation. The garbage collector makes periodic runs to identify and eliminate items that are no longer needed, leaving other items as souvenirs. This feature makes Java programming more efficient and less error-prone, as developers don’t have to allocate or release memory manually.

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How does Garbage Collection Works in Java?

In Java, garbage collection works by using a mark-and-sweep algorithm. This algorithm is executed by the JVM’s garbage collector and consists of the following steps: 

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. 

node 1
node 2
node 3

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. 

Memory Heap Generational Garbage Collection

In Java, the memory heap is divided into different generations, each having a specific purpose . It is managed by different garbage collection algorithms. The division of the heap into generations allows for more efficient memory management and garbage collection. The main generations in the Java memory heap are:

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.

2. Old Generation (Tenured Generation)

  • Objects that survive multiple garbage collection cycles in the Young Generation are eventually promoted to the Old Generation.
  • The Old Generation is intended for objects with longer lifetimes or those that are larger in size.

3. Permanent Generation (prior to Java 8)

  • The Permanent Generation was responsible for storing metadata about classes, methods, and other JVM internals.
  • It is separate from the Young and Old Generations and had a fixed size.

Benefits of Java Garbage collection

Java's garbage collection (GC) offers several benefits:

  • 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.

What triggers Java garbage collection?

In Java, garbage collection is triggered automatically by the Java Virtual Machine when certain conditions are met. The JVM is responsible for managing memory allocation and deallocation. The specific triggers for garbage collection can vary between different JVM implementations, but the general conditions that can initiate garbage collection are as follows:

  • Memory Pressure
  • Allocation Threshold
  • System.gc() Method
  • Generation Space Filling Up

Types and Strategies of Java Garbage Collection

JVM (Java Virtual Machine) Garbage Collectors are embedded memory management subsystems of the Java Virtual Machine that control the memory used by Java applications. There are four types of JVM garbage collectors:

Serial Garbage Collector

Collects garbage using a single thread, making it suitable for small applications or applications with low data volumes. It’s simple and straightforward but can lead to obvious breath stops when collecting waste on large projects.

Parallel Garbage Collector

Uses more threads to collect garbage, making it faster and more efficient than Serial Garbage Collector. It is suitable for high-volume applications and can withstand short breaks during waste collection.

CMS (Concurrent Mark Sweep) Garbage Collector

Use multiple threads to reduce pauses during garbage collection. It is suitable for applications that require low latency and short pauses. It works by marking and sweeping parts of the collection at the same time, helping to reduce garbage collection time.

G1 (Garbage-First) Garbage Collector

Uses an adaptive garbage collection system and a region-based memory management strategy, making it suitable for applications with low latency and short pauses. It separates the garbage collection into different areas and gathers more garbage, enabling it to recover faster.


There are 2 types of heap where GC takes place:
 

  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.

Examples of Garbage Collection in the Context of Java

Null Value

Code:

class MyClass {
   int[] myArray = new int[10000];
}
 
public static void main(String[] args) {
   MyClass obj = new MyClass();
   
   // setting obj reference to null
   obj = null;
   
   // the object is no longer accessible and is eligible for garbage collection
}


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

Code:

public static void main(String[] args) {
   MyClass obj1 = new MyClass();
   MyClass obj2 = new MyClass();
   
   // obj1 now refers to the same object as obj2
   obj1 = obj2; 
   
   // 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

Code:

public static void main(String[] args) {
   while (true) {
      MyClass obj = new MyClass();
      // obj is not set to null, and a new object is created each time the loop runs
   }
}


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.

Adding an Anonymous Object to the Code

Code:

// create and use an anonymous object
new MyObject().dothing();

// request garbage collection
System.gc();

// wait for garbage collection to complete
Thread.sleep(2000);

// print a message to indicate that garbage collection has completed
System.out.println("Garbage collection completed.");


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.

Example of Garbage Collection

Code:

  • Java

Java

import java.util.Scanner;


public class GarbageCollectionExample {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter a string: ");
       String input = scanner.nextLine();
       System.out.println("You entered: " + input);

       // set input to null to make it eligible for garbage collection
       input = null;

       // request garbage collection
       System.gc();

        // wait for garbage collection to complete
       try {
          // Sleep the current thread for 1000 milliseconds
           Thread.sleep(1000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }

       System.out.println(" Garbage collection completed.");
   }
}


Input:

What is Garbage Collection in the Context of Java


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."

Need for Garbage Collection

Garbage collection is required because many programming languages use dynamic memory allocation, which allocates memory as needed by the program at runtime.

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 pointers, which happens when a pointer points to a memory location that has already 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 managed and cleaned up.

Java garbage collection best practices

Java garbage collection (GC) best practices include:

  • Understand GC Algorithms: Learn about different GC algorithms (e.g., Serial, Parallel, CMS, 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 behavior, identify patterns, and optimize GC settings accordingly.
  • Tune GC Settings: Adjust GC settings (e.g., heap size, GC algorithms, GC threads) based on application requirements, workload characteristics, and available hardware resources.
  • Minimize Object Creation: Reduce unnecessary object creation by reusing objects, using object pools, and avoiding excessive use of temporary objects.
  • Avoid Object Finalization: Minimize or avoid using finalize() method as it can cause performance overhead and unpredictability in GC behavior.
  • Use Generational GC: Leverage the generational GC concept to separate short-lived objects (young generation) from long-lived objects (old generation) and optimize GC performance accordingly.

Advantages of Garbage Collection

Garbage collection is a memory management technique used by programming languages ​​to manage the allocation and deallocation of memory resources automatically. 

Here are some benefits of garbage collection: 

  • Simplifies Memory Management: Garbage collection simplifies memory management by automating memory allocation and deallocation processes. This reduces the likelihood of memory leaks, dangling pointers, and other common programming errors. 
     
  • Memory Efficiency: Garbage collection can improve memory efficiency by recovering unused memory items, allowing other tasks to be performed. 
     
  • 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 other aspects of software development.
     
  • Improved Performance: Garbage collection can improve application performance by reducing the frequency of memory allocation and deallocation operations. This, in turn, can help reduce the fragmentation that can negatively impact performance.
     
  • Security: Garbage collection can help prevent security vulnerabilities like buffer overflow attacks and other memory issues.

Disadvantages of Garbage Collection

Although garbage collection is a helpful feature in programming languages, it also has some drawbacks:

  • 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.

Types of Generational Garbage Collector

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:

protected void finalize() throws Throwable {
   // perform cleanup operations
}


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:

public static void gc(){}  

Frequently Asked Questions

How do you handle garbage collection in Java?

In Java, garbage collection is a built-in mechanism that automatically cleans up unused objects. It helps free up memory and manage resources without manual intervention. It makes it easier to work with objects and prevents memory leaks.

Is it possible to disable Java's garbage collection?

No, Java does not support turning off garbage collection. Program crashes will result if garbage collection is not implemented because it is a crucial component of the Java virtual machine.

Does garbage collection affect performance in Java? 

Though the impact is typically negligible, garbage collection in Java can impact performance. The JVM makes an effort to reduce the impact of garbage collection on program performance when it occurs regularly.

What is garbage collection in the context of Java answer?

Garbage collection in Java is an automatic memory management process where the JVM identifies and reclaims memory occupied by objects that are no longer referenced by the program.

What is garbage collection with example?

In Java, when an object goes out of scope or loses all references, like Object obj = null;, the garbage collector automatically deallocates its memory, enhancing memory efficiency.

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 ensures that memory is efficiently used. 

Suppose you want to know more about “What is Garbage Collection in the Context of Java” and topics like this. In that case, refer to the following articles:

To learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java.

Happy Learning!

Previous article
Stack Memory and Heap Memory in Java
Next article
File Class in Java
Live masterclass