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

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

上圖的藍色區(qū)域是對象壽命的典型分布。x軸顯示以分配的字節(jié)為單位測量的對象生存期,y軸上的字節(jié)數(shù)是對應生命周期的對象中的總字節(jié)數(shù)。圖中左邊的高峰代表對象在被分配內存不久之后就死亡了。比如,迭代對象只能存在于一此循環(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)化這種情況,內存是分代管理的(內存池中存有不同年齡的對象)。當一代的空間被填滿時,這一代就會發(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:

大部分對象都是年輕代對象,并且大部分對象在年輕代中就消亡了。當年輕代填滿時,就會觸發(fā)minor collection,minor collection只回收年輕代中的對象,而不會回收其他代的對象。這類垃圾回收的開銷主要取決于回收中活躍對象的數(shù)量,由于年輕代滿是消亡的對象,所以垃圾對象被回收的很快。在每次minor collection中通常會有一部分年輕代的對象被移到老年代。最終,老年代被填滿了,必須被回收,這就觸發(fā)了major collection,marjor collection時,整個堆都會被回收。Major collection通常比minor collection持續(xù)時間長的多,因為它涉及到非常大量的對象。分代回收圖如上。

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區(qū)和兩個survivor space組成。大部分對象在初始時被分配在eden區(qū)。任何時候都存在一個空的survicor space,作為Eden區(qū)和另一個survivor space活躍對象垃圾回收期間的去處。垃圾回收后,eden區(qū)和源survivor space都空了。在下一次垃圾回收時,源survivor space和目的survivor space的作用的交換了(源變成目的,目的變成源),最近被填滿的那個survivor space作為起始點,其中的活躍對象被拷貝到另一個survivor space去。對象在兩個幸存者空間之間來回被拷貝,直到對象被拷貝到了一定次數(shù)或者是survivor space剩余空間不夠了,對象就被拷貝老年代了。這個進程也叫做aging。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,847評論 0 10
  • 轉載blog.csdn.net/ning109314/article/details/10411495/ JVM工...
    forever_smile閱讀 5,505評論 1 56
  • 不知各位大神今天起床 有木有感覺到一股強烈的冷氣撲面而來 這是打開冰箱門和打開窗時的強烈對比 又到了要靠頑強毅力才...
    孫苗苗sunny閱讀 287評論 0 0
  • 梨花誦: 林深未許冷風羞, 萬桿花鞭舉樹頭。 喝令雪凝枝嵌玉, 趕來春色滿山溝。
    快樂天成閱讀 261評論 2 2
  • 離家久了,獨自一人在異鄉(xiāng)。什么節(jié)日什么習俗,幾乎是不太關心的。 只不過剛剛下樓取快遞,樓道里彌漫著一股熟悉、久遠的...
    夏小缺閱讀 260評論 0 0

友情鏈接更多精彩內容