Generational Collectors (分代收集器)

  • GC algos optimised based on two hypotheses / observations:

    • Most objects soon become unreachable - short lived.

    • References from old objects to young objects only exist in small numbers

  • The Oracle HotSpot JVM:

    • Objects allocated in the Eden space of the Young Generation (or New Generation年轻代)

    • Once the Eden space is full, a young collection or minor collection occurs

    • Surviving objects live in the survivor space of the young generation

    • When an object is “old enough”, it is promoted to the Old Generation (or tenured space年老代)

    • When the old generation is “full enough”, a major collection occurs.

  • Allocation is usually very fast

    • Thread Local Allocation Buffers

    • Bump of pointer

Java Garbage Collectors

  • Available in the Oracle HotSpot JVM:

    • Serial Collector

    • Parallel (Throughput) Collector

    • Concurrent Mark-Sweep Collector (CMS)

    • G1

  • Others:

    • Oracle JRockit Real Time

    • IBM Websphere Real Time

    • Azul Zing

Serial Collector

  • Single threaded for all collections (minor and major)

  • All collections are Stop-The-World (STW)

  • Collections use a mark-sweep-compact algorithm

  • Suitable for single-threaded apps with low memory footprint (~100MB)

  • Enabled using -XX:+UseSerialGC

Parallel Collector

  • Similar to the serial collector, but uses multiple threads to speed things up

  • Offers highest throughput of all the collectors

  • Enabled using ‑XX:+UseParallelGC

Concurrent Mark Sweep (CMS) Collector

  • Boasts shorter pauses than the serial or parallel collectors at the expense of application throughput

  • Minor collections similar to serial collector

  • Old generation is collected concurrently with the application

  • Not compacting, so can result in old generation fragmentation

  • Enabled using -XX:+UseConcMarkSweepGC

G1 Garbage Collector

  • G1 = Garbage First

  • Default GC in Java 9

  • Aims for low pause times (<0.5s) and good throughput (90%) for large heaps (>6GB)

  • Generational and concurrent

  • Adaptive to meet pause time target

  • Enabled with -XX:+UseG1GC

G1 Layout

  • Unlike other collectors, G1 divides the heap into evenly sized regions

  • Regions can be 1MB to 32MB in size, in power of two increments

  • Regions can be dynamically assigned as:

    • Eden

    • Survivor

    • Old

    • Humongous

  • G1 aims for 2048 regions based on minimum heap size

G1 Minor Collections

  • Also known as Evacuation Pauses

  • A STW event

  • Subset of regions logically assigned as the young generation

  • Minor collection triggered when the young generation is full

  • Live objects “evacuated” to new regions to achieve compaction

  • Objects moved to either old region or survivor region based on age

  • Number of regions in young generation can be changed to meet the pause time target

G1 Concurrent Marking

  • Triggered when the used heap reaches a configurable threshold of total heap

  • Aim is to identify which old generation regions can be collected

  • Multi-phased process, some of which is STW, some concurrent with application

  • Concurrent phase can be stopped by a young collection

G1 Mixed Collections

  • Occurs after the concurrent marking phase

  • Old regions optionally added to the eden and survivor regions to be collected

  • Old regions eligible for collection usually split over multiple collections

  • The number of mixed collections is tunable via flags

  • G1 reverts to minor (young) collections when mixed collections have finished

G1 Humongous Objects

  • Humongous object defined as one greater than 50% the size of a region.

  • Allocated directly to the old generation to avoid copying during young collections

  • Region(s) marked as humongous

  • Don’t want too many, and ideally they should be long lived.

G1 Evacuation Failures and Full GC

  • Evacuation failure is when there is no free space to copy objects to

  • Evacuation failures trigger a full GC - very expensive!

G1 Configuration

  • There are many flags. Advice is to not tune G1 much unless you have to.

  • Some flags are experimental, and require -XX:+UnlockExperimentalVMOptions to be set also

  • Primary flag is to control the pause target:
    -XX:MaxGCPauseMillis=200

  • -XX:InitiatingHeapOccupancyPercent=45
    Percentage of heap occupancy at which the marking phase is triggered

  • Many more options described at http://www.oracle.com/technetwork/articles/java/g1gc-1984535.html

  • Some options for GC logging:

    • -XX:+PrintGCDetails - Enable detailed GC logging

    • -XX:+PrintGCDateStamps - Print absolute date stamps at beginning of GC lines

    • -XX:+PrintGCTimeStamps Print a timestamp reflecting the real time passed in seconds since JVM start

GC Logs

    • New log file every time the JVM starts

Java Garbage Collectors的更多相关文章

  1. 细述 Java垃圾回收机制→Types of Java Garbage Collectors

    细述 Java垃圾回收机制→Types of Java Garbage Collectors 转自:https://segmentfault.com/a/1190000006214497 本文非原创, ...

  2. Garbage Collectors – Serial vs. Parallel vs. CMS vs. G1 (and what’s new in Java 8)

    转自:http://blog.takipi.com/garbage-collectors-serial-vs-parallel-vs-cms-vs-the-g1-and-whats-new-in-ja ...

  3. Garbage Collectors - Serial vs. Parallel vs. CMS vs. G1 (and what's new in Java 8)--转

    The 4 Java Garbage Collectors - How the Wrong Choice Dramatically Impacts Performance The year is 20 ...

  4. Java Garbage Collection Basics--转载

    原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose ...

  5. Java Garbage Collection/垃圾收集 策略查看

    Java 的垃圾收集有各种各样的策略,默认的策略也会经常的改变. --比如到底是 serial , parallel, CMS; 具体到 Minor 怎么样,Old 又怎么样? 命令 java -XX ...

  6. [翻译]Java垃圾收集精粹(Java Garbage Collection Distilled)

    source URL: http://www.infoq.com/articles/Java_Garbage_Collection_Distilled Name: Java Garbage Colle ...

  7. How to Tune Java Garbage Collection--reference

    reference:http://architects.dzone.com/articles/how-tune-java-garbage The Performance Zone is support ...

  8. JVM垃圾收集(Java Garbage Collection / Java GC)

    JVM垃圾收集(Java Garbage Collection / Java GC) Java7 Java8 JDK1.8之后将最初的永久代取消了,由元空间取代. 堆内存调优简介 public sta ...

  9. 四.GC —三分钟认识JAVA回收机制(Java Garbage Collection)

    这里以jdk1.8做讲解.Jdk1.8的分代去掉了永久代,只分为新生代(有的也译为年轻代)和年老代. 名词解释: 系统吞吐量:用于处理应用程序处理事务的线程数与用于GC的线程数的比. pause ti ...

随机推荐

  1. 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes

    从github上下载的工程大部分都使用了cocapods,在install的时候可能会报错. 报错原因: 1.不要使用文本编辑去编辑Podfile文件,使用Xcode编辑,或者使用终端敲命令去编辑. ...

  2. Angularjs 动态添加指令并绑定事件

    先说使用场景,动态生成DOM元素并绑定事件,非常常见的一种场景,用jq实现效果: http://jsbin.com/gajizuyuju/edit?html,js,output var count=0 ...

  3. BZOJ2818 与 BZOJ2301【euler,线性筛,莫比乌斯】

    题目大意: 给一个范围[1,n],从中找出两个数x,y,使得gcd(x,y)为质数,问有多少对(x,y有序) 解法: 不难,欧拉函数练手题,可以定义集合P ={x|x为素数},那么我们枚举gcd(x, ...

  4. 基于Haproxy+Keepalived构建高可用负载均衡集群

    实验环境: 主机名 IP地址 VIP 192.168.200.254 Haproxy-1 192.168.200.101 Haproxy-2 192.168.200.102 Nginx1 192.16 ...

  5. [.NET] 《C# 高效编程》(一) - C# 语言习惯

    C# 语言习惯 目录 一.使用属性而不是可访问的数据成员 二.使用运行时常量(readonly)而不是编译时常量(const) 三.推荐使用 is 或 as 操作符而不是强制类型转换 四.使用 Con ...

  6. C语言的一些基础

    一.C语言基础: 1.1.main函数是入口函数,用于进行link. 1.2..sln是解决方案的管理文件. 1.3.int:32位.short:16位.long:32位.long long:64位. ...

  7. 对于Bootstrap的介绍以及如何使用

    Bootstrap是HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 可以自动适配任何设备,解决了响应式实现的繁琐问题,可以修改其中的各种样式,同样,其内部功能的强 ...

  8. IO调度器原理介绍

    IO调度器(IO Scheduler)是操作系统用来决定块设备上IO操作提交顺序的方法.存在的目的有两个,一是提高IO吞吐量,二是降低IO响应时间.然而IO吞吐量和IO响应时间往往是矛盾的,为了尽量平 ...

  9. linux性能之iostat

    在使用linux系统的过程中,总是可能需要当前io性能的状态信息是怎么样?这里就就是一下iostat,可以通过iostat来初步查看io的状态信息. 1.常用方式 iostat -xdk 1 10 或 ...

  10. Java中多线程同步类 CountDownLatch

    在多线程开发中,常常遇到希望一组线程完成之后在执行之后的操作,java提供了一个多线程同步辅助类,可以完成此类需求: 类中常见的方法: 其中构造方法:CountDownLatch(int count) ...