我试图理解Java堆术语中年轻、年老和永久代的概念是什么,更具体地说,这三个代之间的交互。

我的问题是:

什么是年轻一代? 什么是老一代? 什么是永久代? 这三代人之间是怎样相互影响的?


The Java virtual machine is organized into three generations: a young generation, an old generation, and a permanent generation. Most objects are initially allocated in the young generation. The old generation contains objects that have survived some number of young generation collections, as well as some large objects that may be allocated directly in the old generation. The permanent generation holds objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves.

这似乎是一个常见的误解。在Oracle的JVM中,永久生成不是堆的一部分。它是用于类定义和相关数据的单独空间。在Java 6和更早的版本中,被存储的字符串也存储在永久生成中。在Java 7中,被分隔的字符串存储在主对象堆中。

这里有一个关于永久生成的好帖子。

我喜欢Oracle JConsole指南中对每个空格的描述:

For the HotSpot Java VM, the memory pools for serial garbage collection are the following. Eden Space (heap): The pool from which memory is initially allocated for most objects. Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space. Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space. Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas. Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

Java使用分代垃圾收集。这意味着如果你有一个对象foo(它是某个类的实例),它存活的垃圾收集事件越多(如果仍然有对它的引用),它就会被提升得越高。它开始于年轻一代(它本身被分为多个空间——伊甸园和幸存者),如果它存活足够长时间,最终将结束于老年一代。

堆分为年轻代和老代如下:

年轻一代:指居住时间较短的地方,分为两个空间:

空间:当对象创建时使用新的关键字内存分配 在这个空间上。 幸存者空间:这是一个包含对象的池 在伊甸园空间的java垃圾收集后幸存下来。

老一代:这个池基本上包含终身和虚拟 (预留的)空间,并将保存那些幸存的对象 在年轻一代的垃圾收集后。

Tenured Space:该内存池包含多次垃圾收集后存活的对象,即幸存者空间垃圾收集后存活的对象。

永久生成:这个内存池名称中也包含永久类元数据和描述符信息,因此永久生成空间总是为类以及那些与类绑定的对象(例如静态成员)保留。

Java8更新:PermGen替换为Metaspace,两者非常相似。 主要的区别是Metaspace可以动态地调整大小,也就是说,它可以在运行时扩展。 Java Metaspace空间:无界(默认)

代码缓存(虚拟或保留):如果您使用的是HotSpot Java VM,则包括包含内存的代码缓存区,该内存将用于编译和存储本机代码。

礼貌

SunHotSpot JVM中的内存被组织成三代:年轻一代、老一代和永久一代。

Young Generation : the newly created objects are allocated to the young gen. Old Generation : If the new object requests for a larger heap space, it gets allocated directly into the old gen. Also objects which have survived a few GC cycles gets promoted to the old gen i.e long lived objects house in old gen. Permanent Generation : The permanent generation holds objects that the JVM finds convenient to have the garbage collector manage, such as objects describing classes and methods, as well as the classes and methods themselves.

供您参考:永久生成不被认为是Java堆的一部分。

这三代人之间是怎样相互影响的? 对象(大型对象除外)首先分配给年轻代。如果一个对象在x之后仍然存活不。因此,我们可以说,年轻代包含寿命较短的对象,而老代包含寿命较长的对象。永久代不与其他两代交互。

什么是年轻一代?

Young Generation是分配和老化所有新对象的地方。当年轻代填满时,这将导致一个小型垃圾收集。充满死对象的年轻代会很快被收集起来。一些幸存的对象会老化,并最终移动到老一代。

什么是老一代?

旧代用于存储长期存在的对象。通常,为年轻代对象设置一个阈值,当达到该年龄时,对象被移动到老代。最终需要收集老一代。此事件称为主要垃圾回收

什么是永久代?

Permanent生成包含JVM描述应用程序中使用的类和方法所需的元数据。JVM在运行时根据应用程序使用的类填充永久生成。

自Java 8发布以来,PermGen已被Metaspace取代。

PermSize和MaxPermSize参数将被忽略

这三代人之间是怎样相互影响的?

图片来源& oracle technetwork教程文章:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

上面文章中的“一般垃圾收集过程”用很多图表解释了它们之间的相互作用。

请看总结图: