Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
JVM Garbage Collectors
Next article icon

JVM Garbage Collectors

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JVM Garbage Collectors are essential for Java memory management. It automatically frees up unused memory to prevent memory leaks and improve performance. Java manages memory through the Java Virtual Machine using a process called garbage collection. Garbage collection automatically frees up memory by removing objects from the Java heap that are no longer needed. Garbage collectors use the Mark and Sweep algorithm to clear unused memory.

In this article, we will learn the types of JVM Garbage Collectors and their implementation details.

Threads in Garbage Collection

1. Controlling Garbage Collection Threads

To control the number of threads used by the garbage collector, we can use multi-threaded collectors like the Parallel Garbage Collector.

Implementation:

java -XX:+UseParallelGC -XX:ParallelGCThreads=NumberOfThreads -jar ApplicationJar.java

2. Limiting GC Pause Time

To limit the maximum pause time during garbage collection, we can use the MaxGCPauseMillis option:

Implementation:

java -XX:+UseParallelGC -XX:MaxGCPauseMillis=SecInMillisecond -jar ApplicationJar.java

Types of Garbage Collectors

1. Serial Garbage Collector

  • Serial Garbage Collector is the simplest type of garbage collector.
  • It uses a single thread to handle all garbage collection tasks.
  • When garbage collection is triggered, it pauses all running application threads.
  • Serial Garbage Collector is used for small applications or embedded systems where memory is limited.

Implementation: To enable Serial Garbage Collector, we can use the following argument:

java -XX:+UseSerialGC -jar ApplicationJar.java

2. Parallel Garbage Collector

  • Parallel Garbage Collector is the default garbage collector in Java 8.
  • It is also known as the throughput collector
  • It uses multiple threads to perform garbage collection tasks.
  • It is designed to maximize performance by making full use of available CPU resources.
  • Parallel Garbage Collector is suitable for applications that need high performance and can tolerate some pauses, such as batch processing systems.
  • Parallel Garbage Collector is better than the Serial Garbage Collector

Note: The main difference between serial Garbage Collector and Parallel Garbage Collector is that

  • Serial Garbage Collector uses a single thread for garbage collection.
  • Parallel Garbage Collector uses multiple threads to improve performance and reduce pause times.
  • One problem with the parallel garbage collector is that it pauses the application during minor operations also.
  • It is best suited for applications that can handle such pauses.
  • If we are using JDK 8 then parallel garbage collector is the default garbage collector.

Implementation: If we are running on java 9 and want to use parallel garbage collector then we should use below command:

java -XX:+UseParallelGC -jar ApplicationJar.java

3. Concurrent Mark-Sweep Collector

  • Concurrent Mark-Sweep Collector is designed to minimize pause times by doing most of its work while the application is still running.
  • It uses multiple threads to mark and clean up unreachable objects.
  • As we know, Serial garbage collector and Parallel garbage collector freeze the running threads of the application while performing the garbage collection. But CMS Garbage collector will perform freezing of running threads i.e. application pause in two cases only:
    • While performing the garbage collection, If there is a change in heap memory in parallel.
    • While marking the referenced objects in the old generation space.
  • Concurrent Mark-Sweep Collector is used for applications that require low latency, such as web servers and interactive applications.

Note: The main difference between serial Garbage Collector and Parallel Garbage Collector is that

  • Parallel Garbage Collector uses multiple threads but stops all application threads during garbage collection
  • CMS Garbage Collector allows the application to keep running most of the time, reducing pause times.

Implementation: To enable CMS Garbage Collector, we can use the following argument:

java -XX:+UseParNewGC -jar ApplicationJar.java

4. G1 Garbage Collector

  • G1 Garbage collector was introduced in JDK 7 and became the default garbage collector in Java 9.
  • Designed for applications with large heap sizes (greater than 4GB).
  • Divides the heap into multiple equal-sized regions (1MB to 32MB).
  • G1 Garbage collector replaced the CMS collector due to better performance efficiency.
  • Marks regions with active objects and prioritizes collection in regions with the most unused objects, hence the name "Garbage-First" (G1).
  • Compacts free heap space after garbage collection, improving memory efficiency.

Implementation: To use the G1 Garbage Collector in Java versions less than 9, specify the following command when running your application:

java -XX:+UseG1GC -jar ApplicationJar.java

5. Z Garbage Collector:

  • ZGC is a low-latency garbage collector introduced in Java 11.
  • It is designed to handle large heaps with minimal pause times, typically in the range of milliseconds.

Implementation: To enable Z Garbage Collector, we can use the following argument:

java-XX:+UseZGC -jar ApplicationJar.java

6. Shenandoah Garbage Collector

  • Shenandoah is another low-pause-time garbage collector introduced in Java 12.
  • It aims to reduce pause times by performing garbage collection concurrently with application threads.

Implementation: To enable Shenandoah Garbage Collector, we can use the following argument:

java-XX:+UseShenandoahGC -jar ApplicationJar.java

Garbage Collection JVM Options

JVM Arguments

Here’s a table describing the arguments that can be used to instruct the JVM:

Option

Description

-Xms<size>

Sets the initial heap size.

-Xmx<size>

Sets the maximum heap size.

-XX:MaxMetaspaceSize=<size>

Sets the maximum metaspace size.

-XX:+HeapDumpOnOutOfMemoryError

Generates a heap dump when an OutOfMemoryError occurs.

-XX:HeapDumpPath=<path>

Specifies the path to save the heap dump file.

-XX:+PrintGCDetails

Prints detailed garbage collection information to the console.

-XX:+UseG1GC

Enables the G1 Garbage Collector (default for most modern JVMs)

-XX:+UseContainerSupport

Optimizes JVM behavior in containerized environments.

Garbage Collector Options

Here's a table describing the available garbage collections algorithms and optimization options in the JVM.

Option

Description

-XX:+UseSerialGC

Enables the Serial Garbage Collector

-XX:+UseParallelGC

Enables the Parallel Garbage Collector

-XX:+UseConcMarkSweepGC

Enables the CMS Garbage Collector

-XX:ParallelCMSThreads=<n>

Sets the number of threads to use

-XX:+UseG1GC

Enables the G1 Garbage Collector

-XX:+UseZGC

Enables the Z Garbage Collector

-XX:+UseShenandoahGC

Enables the Shenandoah Garbage Collector

-XX:ParallelGCThreads=<n>

Sets the number of threads for Parallel and G1 Garbage Collectors.

-XX:G1HeapRegionSize=<size>

Sets the size of regions for G1 Garbage Collector.


Next Article
JVM Garbage Collectors

B

bishaldubey
Improve
Article Tags :
  • Java
  • Technical Scripter
  • DSA
  • Technical Scripter 2019
  • java-garbage-collection
Practice Tags :
  • Java

Similar Reads

    Z Garbage Collector in Java
    Today, it's common for applications to respond to thousands or even millions of users concurrently. Such applications need immeasurable amounts of memory. However, managing all that memory may easily impact application performance. To overcome this issue java 11 includes a lot of improvements and ch
    6 min read
    Garbage Collection in Java
    Garbage collection in Java is an automatic memory management process that helps Java programs run efficiently. Java programs compile to bytecode that can be run on a Java Virtual Machine (JVM). When Java programs run on the JVM, objects in the heap are created, which is a portion of memory dedicated
    7 min read
    Java Collectors
    Collectors is one of the utility class in JDK which contains a lot of utility functions. It is mostly used with Stream API as a final step. In this article, we will study different methods in the collector class. When it comes to the functional style of programming in Java, we typically have few fun
    13 min read
    Short-Pause Garbage Collection
    Compiler design is a complicated field. It's long, it's technical, and it often involves trade-offs between various features and performance. But one particular set of design choices can cause problems for your program's garbage collector (GC). That decision is: do you use short-pause GC or long-pau
    6 min read
    Output of Java programs | Set 10 (Garbage Collection)
    Prerequisite - Garbage Collection in Java Difficulty level : Intermediate In Java, object destruction is taken care by the Garbage Collector module and the objects which do not have any references to them are eligible for garbage collection. Below are some important output questions on Garbage colle
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences