Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Java garbage collectors

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what are the Java garbage collectors". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the Java garbage collector has!

This article will introduce different types of Java garbage collectors. Garbage collection is an automated process used by Java to free programmers from the chore of allocating and releasing memory.

Java has four types of garbage collectors

Serial Garbage Collector

Parallel Garbage Collector

CMS Garbage Collector

G1 Garbage Collector

These four types of garbage collectors have their own advantages and disadvantages. The most important thing is that programmers can choose which type of garbage collector JVM uses. We can set which one to use by passing different JVM parameters. The efficiency of each garbage collector will vary greatly in different application scenarios. Therefore, it is important to understand the different types of garbage collectors and their application scenarios.

Serial Garbage Collector

The serial garbage collector controls all application threads. It is designed for single-threaded scenarios and uses only one thread to perform garbage collection. The way it suspends all application threads to perform garbage collection does not apply to the server's application environment. It works best with simple command-line programs.

Use the-XX:+UseSerialGCJVM parameter to turn on using the serial garbage collector.

Parallel Garbage Collector

The parallel garbage collector is also called a throughput-based collector. It is the default garbage collector for JVM. Unlike Serial, it uses multiple threads to perform garbage collection. Like the Serial collector, it needs to pause all application threads while performing garbage collection.

CMS Garbage Collector

The concurrent tag cleanup (Concurrent Mark Sweep,CMS) garbage collector uses multiple threads to scan heap memory and mark objects that can be cleared, and then clear marked objects. The CMS garbage collector pauses worker threads only in the following two situations

When marking reference objects in the old days,

Changes occur in heap memory during garbage collection

Compared to the parallel garbage collector, the CMS collector uses more CPU to ensure higher throughput. If we can have more CPU to improve performance, then the CMS garbage collector is a better choice than the parallel collector.

Use the-XX:+UseParNewGCJVM parameter to turn on using the CMS garbage collector.

G1 Garbage Collector

The G1 garbage collector is used in large heap memory space. It divides the heap memory space into different areas and reclaims each area in parallel. After reclaiming the memory space, G1 immediately heaps the free space to do consolidation work to reduce fragmentation. CMS performs memory consolidation when all stops (stop the world,STW). For different areas, G1 determines the priority according to the amount of garbage.

Use the-XX:UseG1GCJVM parameter to turn on using the G1 garbage collector.

Optimization of Java 8

When using the G1 garbage collector, enable the use of the-XX:+UseStringDeduplacatonJVM parameter. It optimizes heap memory footprint by moving duplicate string values to the same char [] array. This is an option introduced by Java 8 u 20.

When to use which of the four Java garbage collectors given above depends on the application scenario, hardware configuration, and throughput requirements.

Garbage Collection JVM Options

Here are some major JVM options related to Java garbage collection.

Type of Garbage Collector to run

GC optimization options

Example Usage of JVM GC Options

Java- Xmx12m-Xms3m-Xmn1m-XX:PermSize=20m-XX:MaxPermSize=20m-XX:+UseSerialGC-jar java-application.jar here, I believe you have a deeper understanding of "what is the Java garbage collector?" you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report