Summary of types of Java GC garbage collector

Source: Internet
Author: User
Tags xms

Read the Java Paper garbage collector type article, here to do a summary, the article part of the translation from the Java Paper GC collector, part of the summary of their own, the image from the network, which is only used to understand the expression.

One, JVM GC garbage collector type

The JVM's garbage collector is broadly divided into four types:

(Image from Network)

1. Serial garbage collector Serial garbage collector serial garbage collector when it is garbage collected, it holds all application threads, freezes all application threads, and uses a single garbage collection thread for garbage collection.
The serial garbage collector is designed for single-threaded environments, and if your program does not require multiple threads, start serial garbage collection. (usually command line program)
How to use:-XX:+USESERIALGC

Ps: In JDK client mode, do not specify VM parameters, default is serial garbage collector

(Image from Network)

2. Parallel garbage collector Parallel garbage Collector
When the parallel garbage collector is garbage collected, it also holds threads for all applications and freezes all application threads for garbage collection.
The only thing that is different from the serial garbage collector is that the parallel garbage collector uses multithreading to work with garbage collection.

The serial garbage collector is used in JDK client mode.

(Image from Network)

3. Concurrent tag Scan garbage collector CMS garbage collectorconcurrent Mark Sweep (CMS) garbage collector uses the concurrency tagging algorithm, uses multithreading to scan the heap memory to mark instances, and then cleans up the flagged instances.
The CMS garbage collector sometimes hold all application threads, but sometimes only some application threads are hold.

When will all application threads be paused?
1. In the old age, when the referenced object is marked
2. If garbage collection occurs, parallel changes occur in the heap memory.

Compared to the first two garbage collector, the CMS has somewhat shortened the time that the application GC pauses.
Compared to parallel garbage collection, the CMS collector uses more CPUs to exchange for more application throughput.
However, the CMS will stop the world when the memory space is compressed after the memory is restored.

If you can allocate more CPUs to the garbage collector, then the CMS will be a better choice than parallel garbage collection.

Xx:+useparnewgc

(Image from Network)

4, G1 garbage collector G1 garbage CollectorG1 garbage collector is used in the heap memory is very large, the heap is divided into many large chunks of the region, and then in parallel to the garbage collection.
The G1 garbage collector also does memory compression after it clears the memory space occupied by the instance.

The G1 garbage collector does not stw the region when it is reclaimed, but it is garbage collected on the region based on the policy of most garbage priority recycling.

–xx:+useg1gc

JAVA8, use-xx:+usestringdeduplication
This optimizes the heap memory by removing duplicate String values to a single char[] array
This optimization optimizes the redundancy of the string as a char array.

(Image from Network)

Selection of two or four types of garbage collector determining factors: 1. Application Scenarios
2, the hardware constraints
3, the demand for throughput


Serial garbage collection is the simplest and most efficient, if only the single-threaded, simple task of the console, and the machine configuration is not high, recommended to use.

The parallel garbage collector is the default garbage collector for 64bit server, which is generally not configured by default on our work and production, and is a parallel garbage collection. For general applications that do not require throughput, and where hardware resources are not sufficient, the parallel garbage collector can almost meet the requirements.

The CMS garbage collector is an optimization of the parallel garbage collector, which at the expense of CPU and system resources, in exchange for GC latency. Not a GC will STW, but according to the situation STW. To some extent, the rate of exchange of resources.

The G1 garbage collector is a garbage collector for the large heap, and if the heap is allocated large enough, the priority reclamation policy of the Sub region will prioritize the cleanup of the region with more garbage. It also reduces memory space fragmentation and does not cause the next GC to be triggered prematurely because contiguous memory space cannot be found when allocating large objects.

Third, the configuration

Option Description
-xx:+useserialgc Serial garbage Collector serial garbage collector
-xx:+useparallelgc Parallel garbage collector parallel garbage collector
-xx:+useconcmarksweepgc CMS garbage collector concurrent Tag garbage collector
-xx:parallelcmsthreads= CMS Collector–number of threads to use concurrency tag the number of threads used by the garbage collector, typically the number of CPUs
-xx:+useg1gc G1 gargbage Collector using G1 garbage collector

GC optimization Options
Option Description
-xms Initial Heap Memory Size initializes the heap size-xms512m
-xmx Maximum Heap Memory size sets the maximum heap size
-xmn Size of young Generation younger generation
-xx:permsize Initial Permanent Generation Size initialization of the permanent band
-xx:maxpermsize Maximum Permanent Generation Size Maximum Permanent band sizes


Parallel GC Parallel Garbage Collection Policy example:

java -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20-XX:+UseParallelOldGC MaxGCPauseMillis=100-XX:MaxGCPauseMillis=100


CMS GC concurrency Tag Clear example of garbage collection policy:

java -Xmx3550m -Xms3550m -Xmn2g -Xss128k-XX:ParallelGCThreads=20  -XX:+UseConcMarkSweepGC-XX:+UseParNewGC -XX:CMSFullGCsBeforeCompaction=5 -xx:+usecmscompactatfullcollection


Eg:

Java-xmx12m -xms3m -xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+USESERIALGC -jar Java-Application.Jar

For more detailed configuration, please refer to: http://blog.sina.com.cn/s/blog_4080505a0101i6cr.html


Iv. Viewing the garbage collector

We know that the JVM is divided into client and server mode.

If the boot JVM does not specify a pattern, the JDK launches different modes of the JVM based on the current operating system configuration.

The default 64bit operating system will be the JVM for server mode.

JAVA-XX: +printcommandlineflags-version
-XX:MAXHEAPSIZE=1073741824-XX:PARALLELGCTHREADS=85-XX: +printcommandlineflags-xx: +UseParallelGCjava Version " 1.6.0_14 "Java (tm) SE Runtime Environment (build 1.6.0_14-b07) Java HotSpot (tm) Server VM (build 14.0-b15, Mixed mode)


For J2SE 5.0, the definition of a server-class machine was one with at least 2 CPUs and at least 2GB of physical m Emory.

For JDK5, distinguish between server or client JVM mode, at least 2 CPUs and 2GB of physical memory.

Platform Default VM
Architecture OS Client VMS If server-class, server VM;
Otherwise, client VM
Server VMs
SPARC 32-bit Solaris X
i586 Solaris X
Linux X
Microsoft Windows X
SPARC 64-bit Solaris X
AMD64 Linux X
Microsoft Windows X
Legend: X= Default vm-= client VM not provided for this platform

Five, Summary:

The garbage collector is currently divided into four types, serial, parallel, concurrent tagging, G1.

Small data volumes and small applications, using the serial garbage collector.

For no special requirements for response time, you can use the parallel garbage collector and the concurrency token garbage collector. (Medium and large applications)

For large, medium-sized applications where the heap can be allocated, it is better to use the G1 garbage collector to further optimize and reduce GC pause times.

There is no silver bullet, for different scenes, the use of different garbage collector.


Reference documents:

http://javapapers.com/java/types-of-java-garbage-collectors/

Http://blog.sina.com.cn/s/blog_4080505a0101i6cr.html

http://www.techpaste.com/2012/02/20/default-jvm-settings-gc-jit-java-heap-sizes-xms-xmx-operating-systems/#more-3569

Http://docs.oracle.com/javase/1.5.0/docs/guide/vm/server-class.html

Original articles, reproduced please specify from: http://blog.csdn.net/oopsoom/article/details/40374897

Summary of types of Java GC garbage collector

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.