Home > Java > javaTutorial > body text

Types of Garbage Collector in Java

PHPz
Release: 2024-08-30 15:54:06
Original
526 people have browsed it

The types of Java garbage collectors define the type we use to develop the program. Garbage collection is an important feature of Java. In Java, we use garbage collection to free up memory that is no longer in use. The garbage collector tracks all objects that are still in use and marks the rest of the objects as garbage. The garbage collector uses the sweep and mark algorithm.

In Java, garbage collection is nothing but the management of memory; we are doing the same by using JVM. By using garbage collection, we do not need to handle the allocation and deallocation of an object by using a programmer. In java application allocates and frees memory by using the operating system, and we are providing the same from the application and garbage collection of new variables.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Java offers multiple garbage collectors, which meet the different needs of the application. Choosing the right garbage collector for our application is very important to improve the performance of our application. Java offers multiple types of garbage collectors to meet the needs of the application.

Types of Garbage Collector in Java

Key Takeaways

  • Parallel GC in Java is known as the default garbage collector used in JVM. The working of parallel GC is the same as serial GC.
  • The serial garbage collector is matched with the environments of single-threaded. It uses a single thread for GC.

Types of Java Garbage Collectors

Below are the types of Java garbage collectors.

We are using those types as per our requirements as follows;

  • Serial garbage collector
  • Parallel garbage collector
  • Concurrent mark sweep garbage collector
  • G1 garbage collector
  • Epsilon garbage collector
  • Z garbage collector
  • Shenandoah garbage collector

The working and performance of every garbage collector are different; it contains their own pros and cons. Java allows us to choose any garbage collector which is used by JVM. At the time of selection of garbage collector, we need to pass the arguments of JVM.

1. Serial Garbage Collector

This garbage collector works while holding all the threads of the application. So we can say that threads of our application freeze by using the process of serial garbage collector; this process is known as the world and stops event. For avowing the use of a serial garbage collector in the server environment, we can use the same in simple programs.

To use the serial garbage collector, we need to execute the –XX:+UseSerialGC into the argument of JVM. We can specify this at the time of using a serial garbage collector.

2. Parallel Garbage Collector

The only difference between parallel and serial GC is that parallel GC makes use of multiple threads, while serial GC only uses a single thread. We are using parallel GC to speed up the throughput of our application; this is also known as a throughput collector.

To use the parallel garbage collector, we need to use the –XX:+UseParallelGC in the argument of JVM. Parallel and serial garbage collectors are essential while developing Java applications.

3. CMS Garbage Collector

In CMS GC, we are using multiple threads, which were used for heap and scanning, which will mark the eviction of marked instances. This type of GC is not freezing the application thread at the time of GC. The thread of the garbage collector is concurrently executing with the application threads.

Using this garbage collector, we can use multiple CPUs for better throughput of our application. We are using CMS GC if we contain more CPU to use. To use the CMS garbage collector then, we need to use the –XX:+UseParNewGC into the argument of JVM. We are using this Java virtual machine argument when using this garbage collector.

4. G1 Garbage Collector

The G1 garbage collector is used if we contain more than 4 GB of memory. This GC divides the heap into equal size chunks and performs the parallel garbage collection on the same as per priority. The G1 garbage collector is showing the global marking phase.

While completing the marking phase, this garbage collector collects the information which contains the object of the garbage collector. To use the G1 garbage collector then, we need to use the –XX:+UseG1GC into the argument of JVM.

5. Epsilon Garbage Collector

This is a passive or non-operational garbage collector. This garbage collector allocates the memory for application, but it will not collect the unused objects. When the application is exhausting the heap, JVM will shut down, so we can say that this GC allows the application out of memory or crash.

The main purpose of this garbage collector is to manage and measure the performance of the application. This garbage collector contains the complex programs which were running inside the program.

6. Z Garbage Collector

This garbage collector performs all the work concurrently without stopping the execution of our applications. This garbage collector handles a heap that contains the size of multiple terabytes.

This type of garbage collector performs its cycle into threads. It will pause its application in an average time of 1 MS.

7. Shenandoah Garbage Collector

This type of garbage collector uses the memory regions for managing which objects are no longer in use and which was ready for compression. This garbage collector adds the forwarding pointer to every heap, which uses the control access on the specified object.

JVM Arguments

Below are the arguments for Java virtual machines. We are using those arguments in garbage collectors as follows.

  • –XX:ParallelGCThreads=: This argument controls the GC threads number.
  • –XX:MaxGCPauseMillis=: This argument specifies the maximum pause time.
  • –XX:GCTimeRatio=: This argument specifies the max throughput target.
  • –XX:+UseSerialGC: This argument specifies the serial garbage collector.
  • –XX:+UseParallelGC: This argument specifies the parallel garbage collector.
  • –XX:+UseG1GC: This argument specifies the G1 garbage collector.
  • –XX:+UseConcMarkSweepGC: This argument specifies the CMS garbage collector.
  • –XX:ParallelCMSThreads: This argument specifies the CMS collector and the number of threads that we use.
  • -XX:InitiatingHeapOccupancyPercent=: This argument controls the heap occupancy while starting the concurrent cycle.
  • -XX:G1MixedGCLiveThresholdPercent=: When a live object exists in the old region, this argument excludes its value from the GC object.
  • -XX:G1HeapWastePercent=: This argument specifies the number of regions allowed to waste.

Conclusion

Java offers a variety of garbage collectors to meet the needs of various applications. Selecting the right garbage collector for our application to improve its performance is essential. The type of java garbage collector defines the type of garbage collector that we will use when developing the program. Java has an important feature called garbage collection.

The above is the detailed content of Types of Garbage Collector in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template