(3/4)Java 垃圾回收方式

原文地址 https://javapapers.com/java/t...java

In this tutorial we will go through the various type of Java garbage collectors available. Garbage collection is an automatic process in Java which relieves the programmer of object memory allocation and de-allocation chores. This is the third part in the garbage collection tutorial series. In the previous part 2 we saw about how garbage collection works in Java, it is an interesting read and I recommend you to go through it. In the part 1 introduction to Java garbage collection, we saw about the JVM architecture, heap memory model and surrounding Java terminologies.
本教程將完整介紹一遍各類不一樣的 Java 垃圾回收器類型。Java 中的垃圾回收是一種自動處理的機制,它幫助開發人員從手工分配和釋放內存的苦差事中解脫出來。本文是系列教程的第三篇,在前文中咱們瞭解了垃圾回收的運行機理,推薦感興趣的讀者先行閱讀。而在第一篇垃圾回收介紹中,咱們已經瞭解了 JVM 的架構、堆內存模型及相關的術語概念。ios

Garbage Collection

Java has four types of garbage collectors,
Java 擁有四種類型的垃圾回收器(Garbage Collector):編程

  • Serial Garbage Collector
    序列垃圾回收器
  • Parallel Garbage Collector
    並行垃圾回收器
  • CMS Garbage Collector
    併發標記掃除垃圾回收器
  • G1 Garbage Collector
    G1 垃圾回收器

Each of these four types has its own advantages and disadvantages. Most importantly, we the programmers can choose the type of garbage collector to be used by the JVM. We can choose them by passing the choice as JVM argument. Each of these types differ largely and can provide completely different application performance. It is critical to understand each of these types of garbage collectors and use it rightly based on the application.
它們有着各自的優缺點,做爲編程者咱們能夠自由選擇讓 JVM 使用哪一種,選擇的方式是經過 JVM 參數。不一樣的垃圾回收器處理方式差異很大,也會令應用的性能表現徹底不一樣。所以理解每種類型的垃圾回收器是十分關鍵的,這樣你才能爲你的應用選擇正確的垃圾回收器類型。segmentfault

Types-of-Java-Garbage-Collectors3_th_thumb

1. Serial Garbage Collector

Serial garbage collector works by holding all the application threads. It is designed for the single-threaded environments. It uses just a single thread for garbage collection. The way it works by freezing all the application threads while doing garbage collection may not be suitable for a server environment. It is best suited for simple command-line programs.
序列垃圾回收器會阻塞應用的全部線程,其設計就是針對單線程應用的。垃圾回收自己也只有一個線程。這種在垃圾回收過程當中阻塞應用所有線程的方式,可能不適合服務器環境,它更適合簡單的命令行程序。數組

Turn on the -XX:+UseSerialGC JVM argument to use the serial garbage collector.
你能夠經過 JVM 參數 -XX:+UseSerialGC 啓用序列垃圾回收器。服務器

2. Parallel Garbage Collector

Parallel garbage collector is also called as throughput collector. It is the default garbage collector of the JVM. Unlike serial garbage collector, this uses multiple threads for garbage collection. Similar to serial garbage collector this also freezes all the application threads while performing garbage collection.
平行垃圾回收器也稱做吞吐量回收器,它是 JVM 的默認垃圾回收器(所以無需參數來啓用)。與上面的序列垃圾回收器不一樣地方在於它使用多個線程來執行垃圾回收,而相同的地方是當執行垃圾回收時它也會凍結應用的全部線程。架構

3. CMS Garbage Collector

Concurrent Mark Sweep (CMS) garbage collector uses multiple threads to scan the heap memory to mark instances for eviction and then sweep the marked instances. CMS garbage collector holds all the application threads in the following two scenarios only,
併發標記掃除(CMS)垃圾回收器使用多個線程來掃描堆內存,標記可回收的對象實例,而後將它們回收。CMS 垃圾回收器僅當處於下面兩個場景時纔會阻塞全部線程:併發

  1. while marking the referenced objects in the tenured generation space.
    當在老年代區域執行回收標記時;
  2. if there is a change in heap memory in parallel while doing the garbage collection.
    當在回收過程當中,堆內存出現變化時。

In comparison with parallel garbage collector, CMS collector uses more CPU to ensure better application throughput. If we can allocate more CPU for better performance then CMS garbage collector is the preferred choice over the parallel collector.
與平行垃圾回收器相比,CMS 回收器會消耗更多 CPU 以便保持更高的應用處理性能。若是咱們有條件分配更多的 CPU 來改善性能,那麼 CMS 垃圾收集器比起平行收集器來講是更好的選擇。app

Turn on the XX:+USeParNewGC JVM argument to use the CMS garbage collector.
你能夠經過 JVM 參數 XX:+UseParNewGC 啓用 CMS 垃圾回收器。ide

4. G1 Garbage Collector

G1 garbage collector is used for large heap memory areas. It separates the heap memory into regions and does collection within them in parallel. G1 also does compacts the free heap space on the go just after reclaiming the memory. But CMS garbage collector compacts the memory on stop the world (STW) situations. G1 collector prioritizes the region based on most garbage first.
G1 垃圾回收器是針對大堆內存區設計的。它將堆內存拆分紅多個區域,而後併發地執行垃圾回收。G1 在收回內存完成的同時就合併了內存區域當中的碎片,相比之下 CMS 垃圾回收器的合併過程則是須要 STW(即掛起全部線程)的。G1 回收器會根據垃圾最多的數量對區域進行優先級排序。

Turn on the –XX:+UseG1GC JVM argument to use the G1 garbage collector.
你能夠經過 JVM 參數 –XX:+UseG1GC 啓用 CMS 垃圾回收器。

Java 8 Improvement

Java 8 改進

Turn on the -XX:+UseStringDeduplication JVM argument while using G1 garbage collector. This optimizes the heap memory by removing duplicate String values to a single char[] array. This option is introduced in Java 8 u 20.
當使用 G1 垃圾收集器時,打開 -XX:+UseStringDeduplication 選項,可以經過刪除重複字符串,只留下一個 char[] 數組的方式,來優化堆內存。該優化是從 java 8 u 20 引進的。

Given all the above four types of Java garbage collectors, which one to use depends on the application scenario, hardware available and the throughput requirements.
以上就是四種垃圾收集器類型的所有介紹,選擇哪一種取決於你的應用場景、硬件能力和吞吐量需求。

Garbage Collection JVM Options

垃圾回收 JVM 參數

Following are the key JVM options that are related to Java garbage collection.
下面總結一下與 Java 垃圾收集有關的關鍵參數:

Type of Garbage Collector to run

垃圾收集器的類型

Option Description
-XX:+UseSerialGC Serial Garbage Collector
-XX:+UseParallelGC Parallel Garbage Collector
-XX:+UseConcMarkSweepGC CMS Garbage Collector
-XX:ParallelCMSThreads= CMS Collector – number of threads to use
-XX:+UseG1GC G1 Gargbage Collector

GC Optimization Options

垃圾收集優化參數

Option Description
-Xms Initial heap memory size
-Xmx Maximum heap memory size
-Xmn Size of Young Generation
-XX:PermSize Initial Permanent Generation size
-XX:MaxPermSize Maximum Permanent Generation size

Example Usage of JVM GC Options

使用 JVM 垃圾收集參數的例子

java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseSerialGC -jar java-application.jar

In the next part of this Java garbage collection tutorial series, we will see about how to monitor and analyze the garbage collection with an example Java application.

在接下來的部分,咱們將經過一個例子,來了解如何監控和分析垃圾收集。

相關文章
相關標籤/搜索