這篇文章到這里就結(jié)束了债鸡。
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.
上圖的藍(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ā)生垃圾回收
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卦洽。