原文地址:http://www.cnblogs.com/redcreen/archive/2011/05/04/2037029.html

collector种类

GC在 HotSpot VM 5.0里有四种:

incremental (sometimes called train) low pause collector已被废弃,不在介绍.

类别 serial collector parallel collector
( throughput collector )
concurrent collector
(concurrent low pause collector)
介绍

单线程收集器
使用单线程去完成所有的gc工作,没有线程间的通信,这种方式会相对高效

并行收集器
使用多线程的方式,利用多CUP来提高GC的效率
主要以到达一定的吞吐量为目标

并发收集器
使用多线程的方式,利用多CUP来提高GC的效率
并发完成大部分工作,使得gc pause短

试用场景 单处理器机器且没有pause time的要求

适用于科学技术和后台处理
有中规模/大规模数据集大小的应用且运行在多处理器上,关注吞吐量(throughput)

适合中规模/大规模数据集大小的应用,应用服务器,电信领域
关注response time,而不是throughput

使用 Client模式下默认
可使用
可用-XX:+UseSerialGC强制使用
优点:对server应用没什么优点
缺点:慢,不能充分发挥硬件资源

Server模式下默认

--YGC:PS FGC:Parallel MSC

可用-XX:+UseParallelGC或-XX:+UseParallelOldGC强制指定

--ParallelGC代表FGC为Parallel MSC

--ParallelOldGC代表FGC为Parallel Compacting

优点:高效

缺点:当heap变大后,造成的暂停时间会变得比较长

可用-XX:+UseConcMarkSweepGC强制指定
优点:
对old进行回收时,对应用造成的暂停时间非常端,适合对latency要求比较高的应用
缺点:
1.内存碎片和浮动垃圾
2.old去的内存分配效率低
3.回收的整个耗时比较长
4.和应用争抢CPU
内存回收触发 YGC
eden空间不足
FGC
old空间不足
perm空间不足
显示调用System.gc() ,包括RMI等的定时触发
YGC时的悲观策略
dump live的内存信息时(jmap –dump:live)
      
YGC
eden空间不足
FGC
old空间不足
perm空间不足
显示调用System.gc() ,包括RMI等的定时触发
YGC时的悲观策略--YGC前&YGC后
dump live的内存信息时(jmap –dump:live)
      
YGC
eden空间不足
CMS GC
1.old Gen的使用率大的一定的比率 默认为92%
2.配置了CMSClassUnloadingEnabled,且Perm Gen的使用达到一定的比率 默认为92%
3.Hotspot自己根据估计决定是否要触法
4.在配置了ExplictGCInvokesConcurrent的情况下显示调用了System.gc.
Full GC(Serial MSC)
promotion failed 或 concurrent Mode Failure时;
内存回收触发时发生了什么 YGC
清空eden+from中所有no ref的对象占用的内存
将eden+from中的所有存活的对象copy到to中
在这个过程中一些对象将晋升到old中:
--to放不下的
--存活次数超过tenuring threshold的
重新计算Tenuring Threshold;
单线程做以上动作
全程暂停应用
FGC
如果配置了CollectGen0First,则先触发YGC
清空heap中no ref的对象,permgen中已经被卸载的classloader中加载的class的信息
单线程做以上动作
全程暂停应用
YGC
同serial动作基本相同,不同点:
1.多线程处理
2.YGC的最后不仅重新计算Tenuring Threshold,还会重新调整Eden和From的大小
FGC
1.如配置了ScavengeBeforeFullGC(默认),则先触发YGC(??)
2.MSC:清空heap中的no ref对象,permgen中已经被卸载的classloader中加载的class信息,并进行压缩
3.Compacting:清空heap中部分no ref的对象,permgen中已经被卸载的classloader中加载的class信息,并进行部分压缩
多线程做以上动作.

YGC
同serial动作基本相同,不同点:
1.多线程处理
CMSGC:
1.old gen到达比率时只清除old gen中no ref的对象所占用的空间
2.perm gen到达比率时只清除已被清除的classloader加载的class信息
FGC
同serial
细节参数 可用-XX:+UseSerialGC强制使用
-XX:SurvivorRatio=x,控制eden/s0/s1的大小
-XX:MaxTenuringThreshold,用于控制对象在新生代存活的最大次数
-XX:PretenureSizeThreshold=x,控制超过多大的字节的对象就在old分配.
-XX:SurvivorRatio=x,控制eden/s0/s1的大小
-XX:MaxTenuringThreshold,用于控制对象在新生代存活的最大次数

-XX:UseAdaptiveSizePolicy 去掉YGC后动态调整eden from已经tenuringthreshold的动作

-XX:ParallelGCThreads 设置并行的线程数

-XX:CMSInitiatingOccupancyFraction 设置old gen使用到达多少比率时触发
-XX:CMSInitiatingPermOccupancyFraction,设置Perm Gen使用到达多少比率时触发
-XX:+UseCMSInitiatingOccupancyOnly禁止hostspot自行触发CMS GC

注:

  • throughput collector与concurrent low pause collector的区别是throughput collector只在young area使用使用多线程,而concurrent low pause collector则在tenured generation也使用多线程。
  • 根据官方文档,他们俩个需要在多CPU的情况下,才能发挥作用。在一个CPU的情况下,会不如默认的serial collector,因为线程管理需要耗费CPU资源。而在两个CPU的情况下,也提高不大。只是在更多CPU的情况下,才会有所提高。当然 concurrent low pause collector有一种模式可以在CPU较少的机器上,提供尽可能少的停顿的模式,见CMS GC Incremental mode
  • 当要使用throughput collector时,在java opt里加上-XX:+UseParallelGC,启动throughput collector收集。也可加上-XX:ParallelGCThreads=<desired number>来改变线程数。还有两个参数 -XX:MaxGCPauseMillis=<nnn>和 -XX:GCTimeRatio=<nnn>,MaxGCPauseMillis=<nnn>用来控制最大暂停时间,而-XX: GCTimeRatio可以提高GC说占CPU的比,以最大话的减小heap。

附注SUN的官方说明

1. The throughput collector: this collector uses a parallel version of the young generation collector. It is used if the -XX:+UseParallelGC option is passed on the command line. The tenured generation collector is the same as the serial collector.

2. The concurrent low pause collector: this collector is used if the -Xincgc™ or -XX:+UseConcMarkSweepGC is passed on the command line. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is used with the concurrent collector. The concurrent low pause collector is used if the option -XX:+UseConcMarkSweepGC is passed on the command line.

3. The incremental (sometimes called train) low pause collector: this collector is used only if -XX:+UseTrainGC is passed on the command line. This collector has not changed since the J2SE Platform version 1.4.2 and is currently not under active development. It will not be supported in future releases. Please see the 1.4.2 GC Tuning Document for information on this collector.

CMS GC Incremental mode

当要使用 concurrent low pause collector时,在java的opt里加上 -XX:+UseConcMarkSweepGC。concurrent low pause collector还有一种为CPU少的机器准备的模式,叫Incremental mode。这种模式使用一个CPU来在程序运行的过程中GC,只用很少的时间暂停程序,检查对象存活。

在Incremental mode里,每个收集过程中,会暂停两次,第二次略长。第一次用来,简单从root查询存活对象。第二次用来,详细检查存活对象。整个过程如下:

* stop all application threads; do the initial mark; resume all application threads(第一次暂停,初始话标记)
* do the concurrent mark (uses one procesor for the concurrent work)(运行是标记)
* do the concurrent pre-clean (uses one processor for the concurrent work)(准备清理)
* stop all application threads; do the remark; resume all application threads(第二次暂停,标记,检查)
* do the concurrent sweep (uses one processor for the concurrent work)(运行过程中清理)
* do the concurrent reset (uses one processor for the concurrent work)(复原)

当要使用Incremental mode时,需要使用以下几个变量:

-XX:+CMSIncrementalMode default: disabled 启动i-CMS模式(must with -XX:+UseConcMarkSweepGC)
-XX:+CMSIncrementalPacing default: disabled 提供自动校正功能
-XX:CMSIncrementalDutyCycle=<N> default: 50 启动CMS的上线
-XX:CMSIncrementalDutyCycleMin=<N> default: 10 启动CMS的下线
-XX:CMSIncrementalSafetyFactor=<N> default: 10 用来计算循环次数
-XX:CMSIncrementalOffset=<N> default: 0 最小循环次数(This is the percentage (0-100) by which the incremental mode duty cycle is shifted to the right within the period between minor collections.)
-XX:CMSExpAvgFactor=<N> default: 25 提供一个指导收集数

      SUN推荐的使用参数是:
-XX:+UseConcMarkSweepGC \
-XX:+CMSIncrementalMode \
-XX:+CMSIncrementalPacing \
-XX:CMSIncrementalDutyCycleMin=0 \
-XX:CMSIncrementalDutyCycle=10 \
-XX:+PrintGC Details \
-XX:+PrintGCTimeStamps \
-XX:-TraceClassUnloading

注:如果使用throughput collector和concurrent low pause collector,这两种垃圾收集器,需要适当的挺高内存大小,以为多线程做准备。

如何选择collector:

  • app运行在单处理器机器上且没有pause time的要求,让vm选择或者UseSerialGC.

  • 重点考虑peak application performance(高性能),没有pause time太严格要求,让vm选择或者UseParallelGC+UseParallelOldGC(optionally).
  • 重点考虑response time,pause time要小,UseConcMarkSweepGC.

Garbage Collctor – Future

  • Garbage First(G1)
    jdk1.6 update 14 or jdk7

  • few flags need to set
    -XX:MaxGCPauseMillis=100
    -XX:GCPauseIntervalMillis=6000

还没尝试过使用…

Summary

import java.util.ArrayList;
import java.util.List;
public class SummaryCase {
public static void main(String[] args) throws InterruptedException {
List<Object> caches = new ArrayList<Object>();
for (int i = 0; i < 7; i++) {
caches.add(new byte[1024 * 1024 * 3]);
Thread.sleep(1000);
}
caches.clear();
for (int i = 0; i < 2; i++) {
caches.add(new byte[1024 * 1024 * 3]);
Thread.sleep(1000);
}
}
}

用以下两种参数执行,会执行几次YGC几次FGC?
  • -Xms30M -Xmx30M -Xmn10M  -Xloggc:gc.log -XX:+PrintTenuringDistribution -XX:+UseParallelGC

    2.062: [GC
    Desired survivor size 1310720 bytes, new threshold 7 (max 15)
    6467K->6312K(29440K), 0.0038214 secs]
    4.066: [GC
    Desired survivor size 1310720 bytes, new threshold 7 (max 15)
    12536K->12440K(29440K), 0.0036804 secs]
    6.070: [GC
    Desired survivor size 1310720 bytes, new threshold 7 (max 15)
    18637K->18584K(29440K), 0.0040175 secs]
    6.074: [Full GC 18584K->18570K(29440K), 0.0031329 secs]
    8.078: [Full GC 24749K->3210K(29440K), 0.0045590 secs]

    (具体分析见http://rdc.taobao.com/team/jm/archives/440)

  • -Xms30M -Xmx30M -Xmn10M  -Xloggc:gc.log -XX:+PrintTenuringDistribution -XX:+UseSerialGC
    2.047: [GC
    Desired survivor size 524288 bytes, new threshold 15 (max 15)
    - age 1: 142024 bytes, 142024 total
    6472K->6282K(29696K), 0.0048686 secs]
    4.053: [GC
    Desired survivor size 524288 bytes, new threshold 15 (max 15)
    - age 2: 141880 bytes, 141880 total
    12512K->12426K(29696K), 0.0047334 secs]
    6.058: [GC
    Desired survivor size 524288 bytes, new threshold 15 (max 15)
    - age 3: 141880 bytes, 141880 total
    18627K->18570K(29696K), 0.0049135 secs]
    8.063: [Full GC 24752K->3210K(29696K), 0.0077895 secs]

    (具体分析见http://rdc.taobao.com/team/jm/archives/458)

参考:

  1. http://jiangyongyuan.iteye.com/blog/356502

  2. http://www.helloying.com/blog/archives/164
  3. http://hi.baidu.com/sdausea/blog/item/c599ef13fcd3a7dbf6039e12.html
  4. Tuning Garbage Collection with the 1.4.2 JavaTM Virtual Machine .
  5. http://blog.bluedavy.com/?p=200
  6. http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

[转]HotSpot VM GC 的种类的更多相关文章

  1. HotSpot VM GC 的种类

    collector种类 GC在 HotSpot VM 5.0里有四种: incremental (sometimes called train) low pause collector已被废弃,不在介 ...

  2. 什么是HotSpot VM & 深入理解Java虚拟机

    参考 http://book.2cto.com/201306/25434.html 另外,这篇文章也是从一个系列中得出的: <深入理解Java虚拟机:JVM高级特性与最佳实践(第2版)> ...

  3. Java 8 VM GC Tunning Guide Charter 5

    第5章 Available GC The Java HotSpot VM includes three different types of collectors, each with differe ...

  4. Java 8 VM GC Tunning Guide Charter 7-8-b

    第七章 并发gc Java 8提供两种并发gc,CMS和G1 Concurrent Mark Sweep (CMS) Collector This collector is for applicati ...

  5. Java 8 VM GC Tunning Guild Charter 9-b

    第九章 G1 GC The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for ...

  6. 关于HotSpot VM以及Java语言的动态编译 你可能想知道这些

    目录 1 HotSpot VM的历史 2 HotSpot VM 概述 2.1 编译器 2.2 解释器 2.3 解释型语言 VS 编译型语言 3 动态编译 3.1 什么是动态编译 3.2 HotSpot ...

  7. 014-通过JDB调试,通过HSDB来查看HotSpot VM的运行时数据

    一.JDB调试        在预发环境下进行debug时,时常因为工具和环境的限制,导致debug体验非常差,那么有什么方法能够简化我们进行debug的体验吗?JDB就是一种.        JDB ...

  8. HotSpot VM

    1.4.2 Sun HotSpot VM_深入理解Java虚拟机:JVM高级特性与最佳实践(第2版)_红黑联盟读书频道 http://book.2cto.com/201306/25434.html 提 ...

  9. 什么是HotSpot VM

    学习并转载自https://www.cnblogs.com/charlesblc/p/5993804.html 提起HotSpot VM,相信所有Java程序员都知道,它是Sun JDK和OpenJD ...

随机推荐

  1. 软件设计之基于Java的连连看小游戏(一)——开题及游戏首页的制作

    原本计划紧张忙碌的考试月在图书馆和实验室度过,结果突如其来为期两周的软件设计把课余时间几乎捆绑在了机房.软设没有太多知识上的要求,只要成品简洁美观.实用准确即可.考虑了很久决定要用Java swing ...

  2. Add a Parametrized Action 添加带参数的按钮

    In this lesson, you will learn how to add a Parametrized Action. These types of Actions are slightly ...

  3. Windows 7下Node.js Web开发环境搭建笔记

    Node.js是什么? 我们看看百科里怎么说的?JavaScript是一种运行在浏览器的脚本,它简单,轻巧,易于编辑,这种脚本通常用于浏览器的前端编程,但是一位开发者Ryan有一天发现这种前端式的脚本 ...

  4. HTTPS混合内容解析

    什么是HTTPS混合内容 我们可能会有这样的经验,当我们通过HTTPS访问一个网站的时候,突然有提示:“本页面包含有不安全的内容”.这个时候会询问是否显示“不安全的内容”,这个时候,就是遇到了有混合内 ...

  5. Linux-3.14.12内存管理笔记【构建内存管理框架(1)】

    传统的计算机结构中,整个物理内存都是一条线上的,CPU访问整个内存空间所需要的时间都是相同的.这种内存结构被称之为UMA(Uniform Memory Architecture,一致存储结构).但是随 ...

  6. STL 中 map 的使用

    涉及:vector,pair 基本结构 map 从本质上来说是一颗二叉排序树,树上的节点类型为pair.pair类型中有两个重要的成员变量,其中 first 存储了 key ,second 存储了 v ...

  7. 7.jenkins 按标签发布

    jenkins 如果要按标签发布,需要安装下, Git Parameter Plug-In   的 插件. 之前我们的jar包项目.  我们运行的时候是以下内容. 现在我们对这个jar进行小范围修改. ...

  8. acwing 528. 奶酪 解题记录

    习题地址 https://www.acwing.com/problem/content/description/530/ 现有一块大奶酪,它的高度为h,它的长度和宽度我们可以认为是无限大的,奶酪中间有 ...

  9. 2019.08.06模拟赛T2

    题目大意: 已知三个$n$位二进制数$A$,$B$,$C$. 满足: $A+B=C$ 它们二进制位中$1$的个数分别为$a$,$b$,$c$. 求满足条件的最小的$C$. Solution 唉,又是一 ...

  10. python调用C++实例:用C++对numpy执行BFS(广度优先搜索)

    下文的代码可能展示不全,详情请下载文件:用cpp遍历ndarray.rar 问题背景: 现在我有一张二值图test.npy,需要对其闭区域进行孔洞填充,如下图所示: 文件下载链接:用cpp遍历ndar ...