Java 堆术语:年轻代、年老代和永久代?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2129044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java heap terminology: young, old and permanent generations?
提问by knorv
I'm trying to understand What the concepts of young, oldand permanent generationsare in the Java heap terminology, and more specifically the interactions between the three generations.
我试图了解Java 堆术语中年轻代、年老代和永久代的概念是什么,更具体地说是三代之间的交互。
My questions are:
我的问题是:
- What is the young generation?
- What is the old generation?
- What is the permanent generation?
- How does the three generations interact/relate to each other?
- 什么是年轻一代?
- 什么是老一代?
- 什么是永久代?
- 三代人如何相互作用/相互关联?
采纳答案by Joshua McKinnon
This seems like a common misunderstanding. In Oracle's JVM, the permanent generation is not part of the heap. It's a separate space for class definitions and related data. In Java 6 and earlier, interned strings were also stored in the permanent generation. In Java 7, interned strings are stored in the main object heap.
这似乎是一个普遍的误解。在 Oracle 的 JVM 中,永久代不是堆的一部分。它是用于类定义和相关数据的单独空间。在 Java 6 及更早版本中,实习字符串也存储在永久代中。在 Java 7 中,实习字符串存储在主对象堆中。
Here is a good post on permanent generation.
这是关于永久代的好帖子。
I like the descriptions given for each space in Oracle's guide on JConsole:
我喜欢 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.
对于 HotSpot Java VM,串行垃圾回收的内存池如下。
- Eden Space(堆):最初为大多数对象分配内存的池。
- Survivor Space(堆):包含在 Eden 空间的垃圾收集中幸存下来的对象的池。
- Tenured Generation(堆):包含在幸存者空间中存在一段时间的对象的池。
- 永久代(非堆):包含虚拟机本身所有反射数据的池,例如类和方法对象。对于使用类数据共享的Java VM,这一代分为只读和读写区域。
- 代码缓存(非堆):HotSpot Java VM 还包括一个代码缓存,其中包含用于编译和存储本机代码的内存。
Java uses generational garbage collection. This means that if you have an object foo (which is an instance of some class), the more garbage collection events it survives (if there are still references to it), the further it gets promoted. It starts in the young generation (which itself is divided into multiple spaces - Eden and Survivor) and would eventually end up in the tenured generation if it survived long enough.
Java 使用分代垃圾收集。这意味着如果你有一个对象 foo(它是某个类的一个实例),它存活的垃圾收集事件越多(如果仍然有对它的引用),它得到的提升就越大。它从年轻代开始(它本身被分为多个空间 - Eden 和 Survivor),如果它存活的时间足够长,最终会进入年老代。
回答by Mark R
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.
Java 虚拟机分为三代:年轻代、老年代和永久代。大多数对象最初在年轻代中分配。老年代包含在一些年轻代集合中幸存下来的对象,以及一些可能直接在老年代分配的大对象。永久代保存 JVM 认为方便垃圾收集器管理的对象,例如描述类和方法的对象,以及类和方法本身。
回答by Premraj
The Heap is divided into young and old generations as follows :
Heap 分为年轻代和年老代如下:
Young Generation: It is place where lived for short period and divided into two spaces:
年轻一代:是短暂居住的地方,分为两个空间:
- Eden Space: When object created using new keyword memory allocated on this space.
- Survivor Space: This is the pool which contains objects which have survived after java garbage collection from Eden space.
- 伊甸园空间:当使用在此空间上分配的新关键字内存创建对象时。
- Survivor Space:这是一个池,其中包含从 Eden 空间进行 java 垃圾收集后幸存下来的对象。
Old Generation: This pool basically contains tenured and virtual (reserved) space and will be holding those objects which survived after garbage collection from Young Generation.
老年代:这个池基本上包含永久和虚拟(保留)空间,并将保存那些从年轻代垃圾收集后幸存下来的对象。
- Tenured Space:This memory pool contains objects which survived after multiple garbage collection means object which survived after garbage collection from Survivor space.
- Tenured Space:这个内存池包含多次垃圾回收后幸存的对象,意思是从 Survivor 空间垃圾回收后幸存的对象。
Permanent Generation :This memory pool as name also says contain permanent class metadata and descriptors information so PermGen space always reserved for classes and those that is tied to the classes for example static members.
永久代:这个内存池的名称也表示包含永久类元数据和描述符信息,因此永久代空间始终保留给类和那些与类相关的类,例如静态成员。
Java8 Update:PermGenis replaced with Metaspacewhich is very similar.
Main difference is that Metaspace re-sizes dynamically i.e., It can expand at runtime.
Java Metaspace space: unbounded (default)
Java8 更新:PermGen被非常相似的Metaspace取代。
主要区别在于元空间动态调整大小,即它可以在运行时扩展。
Java Metaspace 空间:无界(默认)
Code Cache(Virtual or reserved) : If you are using HotSpot Java VM this includes code cache area that containing memory which will be used for compilation and storage of native code.
代码缓存(虚拟或保留):如果您使用的是 HotSpot Java VM,这包括代码缓存区域,其中包含用于编译和存储本机代码的内存。
回答by KrityAg
Memory in SunHotSpot JVM is organized into three generations: young generation, old generation and permanent generation.
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.
- 年轻代:新创建的对象分配给年轻代。
- 老一代:如果新对象请求更大的堆空间,它会直接分配到老一代。此外,在几个 GC 周期中幸存下来的对象也会被提升到旧代,即旧代中的长寿命对象房屋。
- 永久代:永久代持有 JVM 认为方便垃圾收集器管理的对象,例如描述类和方法的对象,以及类和方法本身。
FYI: The permanent gen is not considered a part of the Java heap.
仅供参考:永久代不被视为 Java 堆的一部分。
How does the three generations interact/relate to each other?Objects(except the large ones) are first allocated to the young generation. If an object remain alive after x no. of garbage collection cycles it gets promoted to the old/tenured gen. Hence we can say that the young gen contains the short lived objects while the old gen contains the objects having a long life. The permanent gen does not interact with the other two generations.
三代人如何相互作用/相互关联?对象(大对象除外)首先分配给年轻代。如果一个对象在 x 之后仍然活着。在垃圾收集周期中,它被提升为旧/终身代。因此我们可以说年轻代包含寿命较短的对象,而老代包含寿命较长的对象。永久代不与其他两代交互。
回答by Ravindra babu
What is the young generation?
什么是年轻一代?
The Young Generationis where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. A young generation full of dead objects is collected very quickly. Some survived objects are aged and eventually move to the old generation.
在年轻一代是所有新对象分配和老化。当年轻代填满时,这会导致少量垃圾收集。一个充满死对象的年轻代被收集得非常快。一些幸存的对象会老化并最终移动到老年代。
What is the old generation?
什么是老一代?
The Old Generationis used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually the old generation needs to be collected. This event is called a major garbage collection
在老一代用来存放长幸存的对象。通常,为年轻代对象设置一个阈值,当达到该年龄时,对象将移动到年老代。最终需要收集老年代。此事件称为主要垃圾收集
What is the permanent generation?
什么是永久代?
The Permanent generationcontains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application.
的常驻代包含元数据需要由JVM来描述应用程序使用的类和方法。JVM 在运行时根据应用程序使用的类填充永久代。
PermGen has been replaced with Metaspace since Java 8 release.
自 Java 8 发布以来,PermGen 已被 Metaspace 取代。
PermSize & MaxPermSizeparameters will be ignored now
现在将忽略PermSize 和 MaxPermSize参数
How does the three generations interact/relate to each other?
三代人如何相互作用/相互关联?
Image source & oracle technetwork tutorial article: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
图片来源&oracle technetwork教程文章:http: //www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
"The General Garbage Collection Process" in above article explains the interactions between them with many diagrams.
上面文章中的“一般垃圾收集过程”用许多图表解释了它们之间的相互作用。
Have a look at summary diagram:
看一下汇总图: