Java thousands ask _07JVM architecture (011) _java garbage collection What are the ways

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What are the ways of Java garbage collection?

The so-called garbage collection method, refers to the JVM provides several different garbage collector, different garbage collector when garbage collection takes different way. Of course, the overall principle follows the Java garbage collection mechanism.

Learn about the Java memory model look here: What is the Java memory model
Understanding heap Memory Look here: What is the Java heap memory?
Understanding Java garbage Collection mechanism look here: What is the Java garbage collection mechanism

Each approach has its own strengths and weaknesses. When we program, we can select the garbage collector by passing parameters to the JVM. The different garbage collection periods are different and can provide us with completely different application performance, so it is important to understand each garbage collector and make the right choice based on the choice of the application.

There are 4 main types of garbage collector: Serial garbage collector (Serial garbage Collector)
, Parallel garbage collector (Parallel garbage Collector), concurrent tag Scan garbage collector (CMS garbage Collector), G1 garbage collector (G1 garbage Collector), as follows:

Serial garbage collector

The serial garbage collector works by holding all the threads of the application. It is designed for single-threaded environments and uses only a single thread for garbage collection and works by freezing all application threads, so it may not be suitable for a server environment. It is best suited to a simple command-line program.
The serial garbage collector can be used with the JVM parameter-XX:+USESERIALGC.

Parallel garbage collector

The parallel garbage collector is the default garbage collector for the JVM. Unlike the serial garbage collector, it uses multithreading for garbage collection. Similarly, it freezes all application threads when garbage collection is performed.

Concurrent Tag Scan garbage collector

Concurrent tag garbage collection uses multithreaded scanning of heap memory, marking instances that need to be cleaned up, and cleaning up flagged instances. The concurrent tag garbage collector holds all of the application's threads in the following two scenarios:

    1. When the tagged reference object is in the Laosheng generation;
    2. In the garbage collection, the data of the heap memory is changed concurrently.

Compared to the parallel garbage collector, the concurrent tag scan garbage collector uses more CPUs to ensure program throughput. If we can allocate more CPUs for better program performance, then the concurrent tag scan garbage collector is a better choice than the concurrent garbage collector.
Open the Concurrency tag scan garbage collector with the JVM parameter XX:+USEPARNEWGC.

G1 garbage collector

The G1 garbage collector splits heap memory into different zones and concurrently recycles it. G1 can also compress the remaining heap memory space after the memory is reclaimed.
G1 garbage Collection prioritizes the first block of garbage, which is suitable for large heap memory situations.
Using the G1 garbage collector through the JVM parameter –XX:+USEG1GC
In Java 8, G1 can optimize heap memory by removing duplicate strings through the JVM parameter-xx:+usestringdeduplication and preserving only one char[].

2, how to choose a different garbage collector

We need to decide which garbage collector to use based on the scenario, hardware performance, and throughput requirements, and select the garbage collector with the configuration of the JVM parameters:

-XX:+USESERIALGC: Serial garbage collector
-XX:+USEPARALLELGC: Parallel garbage collector
-XX:+USECONCMARKSWEEPGC: Concurrent tag Scan garbage collector
-xx:parallelcmsthreads: Concurrent tag scan garbage collector = number of threads used
-XX:+USEG1GC:G1 garbage collector

Other Memory common configurations:

-XMS: Initializing heap memory size
-XMX: Heap Memory Max
-XMN: Cenozoic Size
-xx:permsize: Initializing a permanent generation size
-xx:maxpermsize: Maximum capacity for permanent generation

To configure an instance of the JVM GC parameter:

-Xmx1024m-Xms512m-Xmn256m-XX:PermSize=64-XX:MaxPermSize=128-XX:+-jar java-application.jar

The meaning of the above configuration is:
Heap Memory 512-1024m, Cenozoic 256M, Permanent generation 64-128m, using serial garbage collector to perform java-application.jar.

Java thousands ask _07JVM architecture (011) _java garbage collection What are the ways

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.