GC Getting Started Guide (iii) ---- Garbage Collector type

Source: Internet
Author: User
In this article, we will learn all available types of garbage collectors. Java currently has four types of garbage collectors: 1. Serial garbage collector (Serial Garbage Collector ); 2. Parallel garbage collector (parallel Garbage Collector ); 3. CMS garbage collector (CMS Garbage Collector ); 4. G1 garbage collector );

Each type has its advantages and disadvantages. The most important thing is that we developers can use JVM parameters to select different garbage collectors for the JVM. Each type of Garbage Collector has different concerns, and the application experience is also very different. Therefore, it is very important to understand these types of garbage collectors, it is related to whether we can select the most correct type of Garbage Collector in different application environments.
1. Serial Garbage CollectorWhen the serial garbage collector is working, all application threads are suspended. It is designed for a Single-threaded environment. It only uses one thread for garbage collection. This behavior of pausing all other threads ("stop the world") is not suitable for the server environment. It is only suitable for simple command line programs. Use the-XX: + useserialgc parameter to configure the garbage collector.
2. Parallel Garbage CollectorIt is also called the throughput collector ). It is the default Garbage Collector for virtual machines. Unlike the serial garbage collector, this garbage collector uses multiple threads to recycle garbage. Similar to the serial garbage collector, it also suspends all application threads when it is performing garbage collection.
3. CMS Garbage CollectorConcurrent mark-Clear the concurrent mark sweep Garbage Collector use multi-thread scan heap memory to mark objects to be recycled and then clear the marked objects. The CMS Garbage Collector only suspends all application threads in the following two scenarios:
1. When marking junk objects of the old age;
2. When the heap memory size changes during garbage collection
Compared with the parallel garbage collector, the CMS Garbage Collector consumes more CPU to improve the application throughput. If the CPU permits, the CMS garbage collector is a better choice. Use the-XX: + useparnewgc parameter to configure the garbage collector.

4. G1 Garbage CollectorThe G1 garbage collector is used in a large range of heap areas. It divides the heap memory into several areas and collects garbage in parallel in each area, and cleans up (Clears memory fragments) after garbage collection ), however, the cleanup process needs to stop all application processes (stop the world), and the G1 Garbage Collector preferentially collects areas with more garbage. Use the-XX: + useg1gc parameter to configure the garbage collector.


Java 8 improvements:When G1 garbage collector is used, enabling the-XX: + usestringdeduplication option can optimize heap memory usage. It can move duplicate string values to a separate char array, this option is available in Java 8 U 20.
The preceding four types of garbage collectors depend on the application environment, hardware environment, and throughput requirements.
GC garbage collection optionsThe following are key options related to garbage collection: Garbage Collector options:

GC Optimization Options:


Example:

java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar
Next we will introduce how to monitor and analyze garbage collection.

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

GC Getting Started Guide (iii) ---- Garbage Collector type

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.