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

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

這篇文章到這里就結(jié)束了债鸡。

再見(jiàn).jpg

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

Java平臺(tái)誓沸,標(biāo)準(zhǔn)版HotSpot虛擬機(jī)垃圾回收指南

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.

在運(yùn)行的程序中缅茉,一個(gè)對(duì)象在它不再被任何其他活躍對(duì)象的任何引用訪問(wèn)到的時(shí)候就被認(rèn)為是垃圾,而它的內(nèi)存可以被虛擬機(jī)再利用。

簡(jiǎn)單來(lái)說(shuō)诽偷,沒(méi)有指向?qū)ο蟮囊玫臅r(shí)候,這個(gè)對(duì)象就是垃圾歌馍。

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.

理論上,最直接的垃圾回收算法每次運(yùn)行時(shí)遍歷每個(gè)可達(dá)對(duì)象,沒(méi)被遍歷到的就被認(rèn)為是垃圾镰烧。這種方法花費(fèi)的時(shí)間和活躍的對(duì)象成正比翅楼,在維護(hù)擁有這大量活躍對(duì)象的大型應(yīng)用中是不可行的。

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虛擬機(jī)包含大量不同的垃圾算法飞苇,這些回收算法都使用了一種叫做分代回收的技術(shù)。相對(duì)于單純的垃圾回收每次探測(cè)堆里的活躍對(duì)象的做法,分代回收通過(guò)觀察應(yīng)用的特性肩祥,利用經(jīng)驗(yàn)去最小化回收垃圾對(duì)象的工作量。其中最為重要的特性是弱分代假說(shuō)缩膝,弱分代假說(shuō)認(rèn)為大部分的對(duì)象僅存活很短的時(shí)間混狠。

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

上圖的藍(lán)色區(qū)域是對(duì)象壽命的典型分布。x軸顯示以分配的字節(jié)為單位測(cè)量的對(duì)象生存期疾层,y軸上的字節(jié)數(shù)是對(duì)應(yīng)生命周期的對(duì)象中的總字節(jié)數(shù)将饺。圖中左邊的高峰代表對(duì)象在被分配內(nèi)存不久之后就死亡了。比如痛黎,迭代對(duì)象只能存在于一此循環(huán)里予弧。

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.

為了優(yōu)化這種情況,內(nèi)存是分代管理的(內(nèi)存池中存有不同年齡的對(duì)象)湖饱。當(dāng)一代的空間被填滿時(shí)掖蛤,這一代就會(huì)發(fā)生垃圾回收

分代回收.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:

大部分對(duì)象都是年輕代對(duì)象,并且大部分對(duì)象在年輕代中就消亡了井厌。當(dāng)年輕代填滿時(shí)坠七,就會(huì)觸發(fā)minor collection,minor collection只回收年輕代中的對(duì)象旗笔,而不會(huì)回收其他代的對(duì)象彪置。這類垃圾回收的開(kāi)銷主要取決于回收中活躍對(duì)象的數(shù)量,由于年輕代滿是消亡的對(duì)象蝇恶,所以垃圾對(duì)象被回收的很快拳魁。在每次minor collection中通常會(huì)有一部分年輕代的對(duì)象被移到老年代。最終撮弧,老年代被填滿了潘懊,必須被回收,這就觸發(fā)了major collection贿衍,marjor collection時(shí)授舟,整個(gè)堆都會(huì)被回收。Major collection通常比minor collection持續(xù)時(shí)間長(zhǎng)的多贸辈,因?yàn)樗婕暗椒浅4罅康膶?duì)象释树。分代回收?qǐng)D如上。

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虛擬機(jī)在地址空間中保留整個(gè)Java堆奢啥,但若非需要不分配物理內(nèi)存秸仙。覆蓋了Java堆的整個(gè)地址空間被邏輯上分成年輕代和老年代。為對(duì)象內(nèi)存預(yù)留的整個(gè)地址空間被分成年輕代和老年代桩盲。

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區(qū)和兩個(gè)survivor space組成寂纪。大部分對(duì)象在初始時(shí)被分配在eden區(qū)。任何時(shí)候都存在一個(gè)空的survicor space赌结,作為Eden區(qū)和另一個(gè)survivor space活躍對(duì)象垃圾回收期間的去處捞蛋。垃圾回收后,eden區(qū)和源survivor space都空了柬姚。在下一次垃圾回收時(shí)襟交,源survivor space和目的survivor space的作用的交換了(源變成目的,目的變成源)伤靠,最近被填滿的那個(gè)survivor space作為起始點(diǎn),其中的活躍對(duì)象被拷貝到另一個(gè)survivor space去啼染。對(duì)象在兩個(gè)幸存者空間之間來(lái)回被拷貝宴合,直到對(duì)象被拷貝到了一定次數(shù)或者是survivor space剩余空間不夠了,對(duì)象就被拷貝老年代了迹鹅。這個(gè)進(jìn)程也叫做aging卦洽。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市斜棚,隨后出現(xiàn)的幾起案子阀蒂,更是在濱河造成了極大的恐慌,老刑警劉巖弟蚀,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蚤霞,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡义钉,警方通過(guò)查閱死者的電腦和手機(jī)昧绣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)捶闸,“玉大人夜畴,你說(shuō)我怎么就攤上這事∩咀常” “怎么了贪绘?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)央碟。 經(jīng)常有香客問(wèn)我税灌,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任垄琐,我火速辦了婚禮边酒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘狸窘。我一直安慰自己墩朦,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布翻擒。 她就那樣靜靜地躺著氓涣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪陋气。 梳的紋絲不亂的頭發(fā)上劳吠,一...
    開(kāi)封第一講書(shū)人閱讀 52,682評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音巩趁,去河邊找鬼痒玩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛议慰,可吹牛的內(nèi)容都是我干的蠢古。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼别凹,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼草讶!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起炉菲,我...
    開(kāi)封第一講書(shū)人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤堕战,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后拍霜,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體嘱丢,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年祠饺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屿讽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡吠裆,死狀恐怖伐谈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情试疙,我是刑警寧澤诵棵,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站祝旷,受9級(jí)特大地震影響履澳,放射性物質(zhì)發(fā)生泄漏嘶窄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一距贷、第九天 我趴在偏房一處隱蔽的房頂上張望柄冲。 院中可真熱鬧,春花似錦忠蝗、人聲如沸现横。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)戒祠。三九已至,卻和暖如春速种,著一層夾襖步出監(jiān)牢的瞬間姜盈,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工配阵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留馏颂,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓棋傍,卻偏偏與公主長(zhǎng)得像救拉,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子舍沙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,347評(píng)論 0 10
  • 轉(zhuǎn)載blog.csdn.net/ning109314/article/details/10411495/ JVM工...
    forever_smile閱讀 5,373評(píng)論 1 56
  • 不知各位大神今天起床 有木有感覺(jué)到一股強(qiáng)烈的冷氣撲面而來(lái) 這是打開(kāi)冰箱門和打開(kāi)窗時(shí)的強(qiáng)烈對(duì)比 又到了要靠頑強(qiáng)毅力才...
    孫苗苗sunny閱讀 255評(píng)論 0 0
  • 梨花誦: 林深未許冷風(fēng)羞, 萬(wàn)桿花鞭舉樹(shù)頭剔宪。 喝令雪凝枝嵌玉拂铡, 趕來(lái)春色滿山溝。
    快樂(lè)天成閱讀 234評(píng)論 2 2
  • 離家久了葱绒,獨(dú)自一人在異鄉(xiāng)感帅。什么節(jié)日什么習(xí)俗,幾乎是不太關(guān)心的地淀。 只不過(guò)剛剛下樓取快遞失球,樓道里彌漫著一股熟悉、久遠(yuǎn)的...
    夏小缺閱讀 216評(píng)論 0 0