GC: CMS垃圾回收器一(英文版)
Memory Management in the Java HotSpot™ Virtual Machine
Concurrent Mark-Sweep (CMS) Collector
For many applications, end-to-end throughput is not as important as fast response time. Young generation
collections do not typically cause long pauses. However, old generation collections, though infrequent, can
impose long pauses, especially when large heaps are involved. To address this issue, the HotSpot JVM includes a
collector called the concurrent mark-sweep (CMS) collector, also known as the low-latency collector.
Young Generation Collection Using the CMS Collector
The CMS collector collects the young generation in the same manner as the parallel collector.
Old Generation Collection Using the CMS Collector
Most of the collection of the old generation using the CMS collector is done concurrently with
the execution of the application.
A collection cycle for the CMS collector starts with a short pause, called the initial mark, that
identifies the initial set of live objects directly reachable from the application code. Then,
during the concurrent marking phase, the collector marks all live objects that are transitively
reachable from this set. Because the application is running and updating reference fields while the
marking phase is taking place, not all live objects are guaranteed to be marked at the end of the
concurrent marking phase. To handle this, the application stops again for a second pause, called remark,
which finalizes marking by revisiting any objects that were modified during the concurrent marking
phase. Because the remark pause is more substantial than the initial mark, multiple threads are run in
parallel to increase its efficiency.
At the end of the remark phase, all live objects in the heap are guaranteed to have been marked, so the
subsequent concurrent sweep phase reclaims all the garbage that has been identified. Figure 7
illustrates the differences between old generation collection using the serial mark-sweep-compact
collector and the CMS collector.
Since some tasks, such as revisiting objects during the remark phase, increase the amount of work the
collector has to do, its overhead increases as well. This is a typical trade-off for most collectors that
attempt to reduce pause times.
The CMS collector is the only collector that is non-compacting. That is, after it frees the space that was
occupied by dead objects, it does not move the live objects to one end of the old generation.
This saves time, but since the free space is not contiguous, the collector can no longer use a simple
pointer indicating the next free location into which the next object can be allocated. Instead, it now
needs to employ free lists. That is, it creates some number of lists linking together unallocated regions
of memory, and each time an object needs to be allocated, the appropriate list (based on the amount of
memory needed) must be searched for a region large enough to hold the object As a result, allocations
into the old generation are more expensive than they are with a simple bump-the-pointer technique.
This also imposes extra overhead to young generation collections, as most allocations in the old
generation occur when objects are promoted during young generation collections.
Another disadvantage the CMS collector has is a requirement for larger heap sizes than the other
collectors. Given that the application is allowed to run during the marking phase, it can continue to
allocate memory, thereby potentially continuing to grow the old generation. Additionally, although the
collector guarantees to identify all live objects during a marking phase, some objects may become
garbage during that phase and they will not be reclaimed until the next old generation collection. Such
objects are referred to as floating garbage.
Finally, fragmentation may occur due to lack of compaction. To deal with fragmentation, the CMS
collector tracks popular object sizes, estimates future demand, and may split or join free blocks to
meet demand.
Unlike the other collectors, the CMS collector does not start an old generation collection when the old
generation becomes full. Instead, it attempts to start a collection early enough so that it can complete
before that happens. Otherwise, the CMS collector reverts to the more time-consuming stop-the-world
mark-sweep-compact algorithm used by the parallel and serial collectors. To avoid this, the CMS
collector starts at a time based on statistics regarding previous collection times and how quickly the old
generation becomes occupied. The CMS collector will also start a collection if the occupancy of the old
generation exceeds something called the initiating occupancy. The value of the initiating occupancy is
set by the command line option –XX:CMSInitiatingOccupancyFraction=n, where n is a
percentage of the old generation size. The default is 68.
In summary, compared to the parallel collector, the CMS collector decreases old generation pauses—
sometimes dramatically—at the expense of slightly longer young generation pauses, some reduction in
throughput, and extra heap size requirements.
Incremental Mode
The CMS collector can be used in a mode in which the concurrent phases are done incrementally. This
mode is meant to lessen the impact of long concurrent phases by periodically stopping the concurrent
phase to yield back processing to the application. The work done by the collector is divided into small
chunks of time that are scheduled between young generation collections. This feature is useful when
applications that need the low pause times provided by the concurrent collector are run on machines
with small numbers of processors (e.g., 1 or 2). For more information on usage of this mode, see the
“Tuning Garbage Collection with the 5.0 Java™ Virtual Machine” paper referred to in Section 9.
When to Use the CMS Collector
Use the CMS collector if your application needs shorter garbage collection pauses and can afford to
share processor resources with the garbage collector when the application is running. (Due to its
concurrency, the CMS collector takes CPU cycles away from the application during a collection cycle.)
Typically, applications that have a relatively large set of long-lived data (a large old generation), and that
run on machines with two or more processors, tend to benefit from the use of this collector. An example
would be web servers. The CMS collector should be considered for any application with a low pause time
requirement. It may also give good results for interactive applications with old generations of a modest
size on a single processor.
CMS Collector Selection
If you want the CMS collector to be used, you must explicitly select it by specifying the command line
option -XX:+UseConcMarkSweepGC. If you want it to be run in incremental mode, also enable that
mode via the –XX:+CMSIncrementalMode option.
GC: CMS垃圾回收器一(英文版)的更多相关文章
- GC: CMS垃圾回收器三(实践)
jstat -gc -t [pid] 1000 监控日志... ,抽取其中关键记录不一定连续 应用启动时间 2015-06-23 10:22:27 ,换算后,第二条记录时间是2015-06-24 22 ...
- 探索ParNew和CMS垃圾回收器
前言 上篇文章我们一起分析了JVM的垃圾回收机制,了解了新生代的内存模型,老年代的空间分配担保原则,并简单的介绍了几种垃圾回收器.详细内容小伙伴们可以去看一下我的上篇文章:秒懂JVM的垃圾回收机制. ...
- 关于 CMS 垃圾回收器,你真的懂了吗?
大家好,我是树哥. 前段时间有个小伙伴去面试,被问到了 CMS 垃圾回收器的详细内容,没答出来.实际上,CMS 垃圾回收器是回收器历史上很重要的一个节点,其开启了 GC 回收器关注 GC 停顿时间的历 ...
- 【JVM】CMS垃圾回收器
一.简介 Concurrent Mark Sweep,是一种以获取最短回收停顿时间为目标的收集器,尤其重视服务的响应速度. CMS是老年代垃圾回收器,基于标记-清除算法实现.新生代默认使用ParNew ...
- 浅析CLR的GC(垃圾回收器)
文章目录: 了解托管堆和GC GC高效的处理方式—代 特殊类型的清理 手动监控和控制对象生命周期 1.了解托管堆和GC 在面向对象环境中,每一个类型都代表了一种资源.我们要使用这些资源,就要为这些代表 ...
- GC:垃圾回收器简介
Java堆内存被划分为新生代和年老代两部分,新生代主要使用复制和标记-清除垃圾回收算法,年老代主要使用标记-整理垃圾回收算法,因此java虚拟中针对新生代和年老代分别提供了多种不同的垃圾收集器,JDK ...
- JVM GC算法 垃圾回收器
JVM的垃圾回收算法有三种: 1.标记-清除(mark-sweep):啥都不说,直接上图 2.标记-整理(mark-compact) 3.复制(copy) 分代收集算法 ...
- jvm——CMS 垃圾回收器(未完)
https://matt33.com/2018/07/28/jvm-cms/ 阶段1:Initial Mark stop-the-wolrd 标记那些直接被 GC root 引用或者被年轻代存活对象所 ...
- Visual GC(监控垃圾回收器)
Java VisualVM默认没有安装Visual GC插件,需要手动安装,JDK的安装目录的bin目露下双击jvisualvm.exe,即可打开Java VisualVM,点击菜单栏 工具-> ...
随机推荐
- 解决android sdk manage打开闪退的解决方法
在打开android sdk mangage.exe的时候,一闪而过,在eclipse中出现如下提示: [2015-07-20 13:42:23 - SDK Manager] [SDK Manager ...
- 浅析Android中的消息机制(转)
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- JDK,JRE,JVM区别与联系-理解与概括
我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JDK中的编译程序(javac)将我们的文本java文件编译成JAVA字节码,在JRE上运行这些JAVA字节码,JVM解析 ...
- jq的post传递数组
a = new Object(); b = new Object(); a['你好[眼见]'] = "y"; a[ ...
- MySQL的事件调度器
自MySQL5.1.0起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务,可以看作基于时间的触发器. 一.开启 事件调度默认是关闭的,开启可执行 ...
- Shell教程2-变量
Shell支持自定义变量. 定义变量 定义变量时,变量名不加美元符号($),如: 复制纯文本新窗口 variableName="value" 注意,变量名和等号之间不能有空格, ...
- C#利用最新版的WPS实现导入导出
微软的EXCEl操作相信大家也知道,不方便,安装包太大,而且表格的数据量也只有6000多(是6000多还是60000多我就忘记了),在导出导入大量数据的就没办法,而wsp表格则实现了百万数据的容量,而 ...
- android view生命周期
onFinishInflate() 当View中所有的子控件均被映射成xml后触发 onMeasure( int , int ) 确定所有子元素的大小 onLayout( boolean , in ...
- Math.random();函数 随机数
random函数参数 无参数 random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) random函数示例 document.write(Math.random()); ...
- ASP.NET静态页生成
由于业务需要,得把页面按照模板页生成静态页面,所以自己就琢磨了下,写些思路,以备日后需要的时候用. 静态页生成用到最多的就是匹配跟替换了,首先得读取模板页的html内容,然后进行你自己定义的标签匹配, ...