jdk 10 垃圾回收文档(摘抄)
这篇文章到这里就结束了。
再见.jpgJava 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.
为了优化这种情况,内存是分代管理的(内存池中存有不同年龄的对象)。当一代的空间被填满时,这一代就会发生垃圾回收
分代回收.pngThe 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。