jdk 10 垃圾回收文档(摘抄)

文档链接:https://docs.oracle.com/javase/10/gctuning/garbage-collector-implementation.htm#JSGCT-GUID-23844E39-7499-400C-A579-032B68E53073

这篇文章到这里就结束了。

再见.jpg

Java Platform, Standard Edition HotSpot Virtual Machine Garbage Collection Tuning Guide

Java平台,标准版HotSpot虚拟机垃圾回收指南

Generational Garbage Collection

分代垃圾回收

An object is considered garbage and its memory can be reused by the VM when it can no longer be reached from any reference of any other live object in the running program.

在运行的程序中,一个对象在它不再被任何其他活跃对象的任何引用访问到的时候就被认为是垃圾,而它的内存可以被虚拟机再利用。

简单来说,没有指向对象的引用的时候,这个对象就是垃圾。

A theoretical, most straightforward garbage collection algorithm iterates over every reachable object every time it runs. Any leftover objects are considered garbage. The time this approach takes is proportional to the number of live objects, which is prohibitive for large applications maintaining lots of live data.

理论上,最直接的垃圾回收算法每次运行时遍历每个可达对象,没被遍历到的就被认为是垃圾。这种方法花费的时间和活跃的对象成正比,在维护拥有这大量活跃对象的大型应用中是不可行的。

The Java HotSpot VM incorporates a number of different garbage collection algorithms that all use a technique called generational collection. While naive garbage collection examines every live object in the heap every time, generational collection exploits several empirically observed properties of most applications to minimize the work required to reclaim unused (garbage) objects. The most important of these observed properties is the weak generational hypothesis, which states that most objects survive for only a short period of time.

Java HotSpot虚拟机包含大量不同的垃圾算法,这些回收算法都使用了一种叫做分代回收的技术。相对于单纯的垃圾回收每次探测堆里的活跃对象的做法,分代回收通过观察应用的特性,利用经验去最小化回收垃圾对象的工作量。其中最为重要的特性是弱分代假说,弱分代假说认为大部分的对象仅存活很短的时间。

The blue area in Figure 3-1 is a typical distribution for the lifetimes of objects. The x-axis shows object lifetimes measured in bytes allocated. The byte count on the y-axis is the total bytes in objects with the corresponding lifetime. The sharp peak at the left represents objects that can be reclaimed (in other words, have "died") shortly after being allocated. For example, iterator objects are often only alive for the duration of a single loop.

Figure 3-1 Typical Distribution for Lifetimes of Objects

上图的蓝色区域是对象寿命的典型分布。x轴显示以分配的字节为单位测量的对象生存期,y轴上的字节数是对应生命周期的对象中的总字节数。图中左边的高峰代表对象在被分配内存不久之后就死亡了。比如,迭代对象只能存在于一此循环里。

Generations

分代

To optimize for this scenario, memory is managed in generations(memory pools holding objects of different ages). Garbage collection occurs in each generation when the generation fills up.

为了优化这种情况,内存是分代管理的(内存池中存有不同年龄的对象)。当一代的空间被填满时,这一代就会发生垃圾回收

分代回收.png

The vast majority of objects are allocated in a pool dedicated to young objects (the young generation), and most objects die there. When the young generation fills up, it causes a minor collection in which only the young generation is collected; garbage in other generations isn't reclaimed. The costs of such collections are, to the first order, proportional to the number of live objects being collected; a young generation full of dead objects is collected very quickly. Typically, some fraction of the surviving objects from the young generation are moved to the old generation during each minor collection. Eventually, the old generation fills up and must be collected, resulting in a major collection, in which the entire heap is collected. Major collections usually last much longer than minor collections because a significantly larger number of objects are involved. Figure 3-2 shows the default arrangement of generations in the serial garbage collector:

大部分对象都是年轻代对象,并且大部分对象在年轻代中就消亡了。当年轻代填满时,就会触发minor collection,minor collection只回收年轻代中的对象,而不会回收其他代的对象。这类垃圾回收的开销主要取决于回收中活跃对象的数量,由于年轻代满是消亡的对象,所以垃圾对象被回收的很快。在每次minor collection中通常会有一部分年轻代的对象被移到老年代。最终,老年代被填满了,必须被回收,这就触发了major collection,marjor collection时,整个堆都会被回收。Major collection通常比minor collection持续时间长的多,因为它涉及到非常大量的对象。分代回收图如上。

At startup, the Java HotSpot VM reserves the entire Java heap in the address space, but doesn't allocate any physical memory for it unless needed. The entire address space covering the Java heap is logically divided into young and old generations. The complete address space reserved for object memory can be divided into the young and old generations.

在初始化阶段,java HotSpot虚拟机在地址空间中保留整个Java堆,但若非需要不分配物理内存。覆盖了Java堆的整个地址空间被逻辑上分成年轻代和老年代。为对象内存预留的整个地址空间被分成年轻代和老年代。

The young generation consists of eden and two survivor spaces. Most objects are initially allocated in eden. One survivor space is empty at any time, and serves as the destination of live objects in eden and the other survivor space during garbage collection; after garbage collection, eden and the source survivor space are empty. In the next garbage collection, the purpose of the two survivor spaces are exchanged. The one space recently filled is a source of live objects that are copied into the other survivor space. Objects are copied between survivor spaces in this way until they've been copied a certain number of times or there isn't enough space left there. These objects are copied into the old region. This process is also called aging.
年轻代由eden区和两个survivor space组成。大部分对象在初始时被分配在eden区。任何时候都存在一个空的survicor space,作为Eden区和另一个survivor space活跃对象垃圾回收期间的去处。垃圾回收后,eden区和源survivor space都空了。在下一次垃圾回收时,源survivor space和目的survivor space的作用的交换了(源变成目的,目的变成源),最近被填满的那个survivor space作为起始点,其中的活跃对象被拷贝到另一个survivor space去。对象在两个幸存者空间之间来回被拷贝,直到对象被拷贝到了一定次数或者是survivor space剩余空间不够了,对象就被拷贝老年代了。这个进程也叫做aging。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,634评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,951评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,427评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,770评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,835评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,799评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,768评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,544评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,979评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,271评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,427评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,121评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,756评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,375评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,579评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,410评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,315评论 2 352

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,322评论 0 10
  • 转载blog.csdn.net/ning109314/article/details/10411495/ JVM工...
    forever_smile阅读 5,363评论 1 56
  • 不知各位大神今天起床 有木有感觉到一股强烈的冷气扑面而来 这是打开冰箱门和打开窗时的强烈对比 又到了要靠顽强毅力才...
    孙苗苗sunny阅读 255评论 0 0
  • 梨花诵: 林深未许冷风羞, 万杆花鞭举树头。 喝令雪凝枝嵌玉, 赶来春色满山沟。
    快乐天成阅读 229评论 2 2
  • 离家久了,独自一人在异乡。什么节日什么习俗,几乎是不太关心的。 只不过刚刚下楼取快递,楼道里弥漫着一股熟悉、久远的...
    夏小缺阅读 210评论 0 0