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垃圾回收器一(英文版)的更多相关文章

  1. GC: CMS垃圾回收器三(实践)

    jstat -gc -t [pid] 1000 监控日志... ,抽取其中关键记录不一定连续 应用启动时间 2015-06-23 10:22:27 ,换算后,第二条记录时间是2015-06-24 22 ...

  2. 探索ParNew和CMS垃圾回收器

    前言 上篇文章我们一起分析了JVM的垃圾回收机制,了解了新生代的内存模型,老年代的空间分配担保原则,并简单的介绍了几种垃圾回收器.详细内容小伙伴们可以去看一下我的上篇文章:秒懂JVM的垃圾回收机制. ...

  3. 关于 CMS 垃圾回收器,你真的懂了吗?

    大家好,我是树哥. 前段时间有个小伙伴去面试,被问到了 CMS 垃圾回收器的详细内容,没答出来.实际上,CMS 垃圾回收器是回收器历史上很重要的一个节点,其开启了 GC 回收器关注 GC 停顿时间的历 ...

  4. 【JVM】CMS垃圾回收器

    一.简介 Concurrent Mark Sweep,是一种以获取最短回收停顿时间为目标的收集器,尤其重视服务的响应速度. CMS是老年代垃圾回收器,基于标记-清除算法实现.新生代默认使用ParNew ...

  5. 浅析CLR的GC(垃圾回收器)

    文章目录: 了解托管堆和GC GC高效的处理方式—代 特殊类型的清理 手动监控和控制对象生命周期 1.了解托管堆和GC 在面向对象环境中,每一个类型都代表了一种资源.我们要使用这些资源,就要为这些代表 ...

  6. GC:垃圾回收器简介

    Java堆内存被划分为新生代和年老代两部分,新生代主要使用复制和标记-清除垃圾回收算法,年老代主要使用标记-整理垃圾回收算法,因此java虚拟中针对新生代和年老代分别提供了多种不同的垃圾收集器,JDK ...

  7. JVM GC算法 垃圾回收器

    JVM的垃圾回收算法有三种: 1.标记-清除(mark-sweep):啥都不说,直接上图 2.标记-整理(mark-compact) 3.复制(copy) 分代收集算法                 ...

  8. jvm——CMS 垃圾回收器(未完)

    https://matt33.com/2018/07/28/jvm-cms/ 阶段1:Initial Mark stop-the-wolrd 标记那些直接被 GC root 引用或者被年轻代存活对象所 ...

  9. Visual GC(监控垃圾回收器)

    Java VisualVM默认没有安装Visual GC插件,需要手动安装,JDK的安装目录的bin目露下双击jvisualvm.exe,即可打开Java VisualVM,点击菜单栏 工具-> ...

随机推荐

  1. Linux C double linked for any data type

    /************************************************************************** * Linux C double linked ...

  2. JVM——类加载器的双亲委派模型

    类加载器双亲委派模型,如下图所示: 双亲委派模型的工作过程 如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把这个请求委派给父类加载器去完成,每一个层次的类加载器都是如此,因此 ...

  3. Android 网络流量监听开源项目-ConnectionClass源码分析

    很多App要做到极致的话,对网络状态的监听是很有必要的,比如在网络差的时候加载质量一般的小图,缩略图,在网络好的时候,加载高清大图,脸书的android 客户端就是这么做的, 当然伟大的脸书也把这部分 ...

  4. php在apache中一共有三种工作方式:CGI模式、FastCGI模式、Apache 模块DLL

    php在apache中一共有三种工作方式:CGI模式.FastCGI .FastCGI是什么? FastCGI是语言无关的.可伸缩架构的CGI开放扩展,其主要行 为是将CGI解释器进程保持在内存中并因 ...

  5. javascript对象之 selectionStart selectionEnd

    <script> function inserttag(){ var text=document.getElementById('con'); text.focus(); var star ...

  6. 基数排序/Go实现

    package main import ( "fmt" ) type Radix struct { length int //序列中最大数的位数 radix [][]int //0 ...

  7. Effective java笔记4--方法

    一.检查参数的有效性 极大多数方法和构造函数都会对于传递给它们的参数值有某些限制. 对于公有的方法,使用Javadoc @throws标签(tag)可以使文档中记录下“一旦针对参数值的限制被违反之后将 ...

  8. 【C++】统计代码覆盖率(三)

    报告集成到jenkins才是最终目的,因此又进行了部分资料查找,得到html和xml报告集成jenkins的配置如下: 一 集成html报告 这种方式集成在你已经用gcov+lcov生成了html报告 ...

  9. Windows下安装GTK+

    Step 1:到GTK官方网站上下载安装包.有32位的和64位,64位的有这句: Note that these 64-bit packages are experimental. Binary co ...

  10. Chapter10:泛型算法

    泛型算法的基础是迭代器. 迭代器令算法不依赖于容器,但是算法依赖于元素类型的操作.也即:算法永远不会执行容器的操作. 那么,如果想向容器中添加元素或者执行其他的一些操作呢?标准库提供了插入迭代器来完成 ...