Java 年轻、终身和烫发一代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2070791/
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
Young , Tenured and Perm generation
提问by Tony
I'm confused with Heap,Young,Tenured and Perm generation.
我对 Heap、Young、Tenured 和 Perm 代感到困惑。
Could anyone please explain?
有人可以解释一下吗?
采纳答案by Tendayi Mawushe
The Java garbage collector is referred to as a Generational Garbage Collector. Objects in an application live for varying lengths of time depending on where they are created and how they are used. The key insight here is that using different garbage collection strategies for short lived and long lived objects allows the GC to be optimised specifically for each case.
Java 垃圾收集器被称为分代垃圾收集器。应用程序中的对象存在的时间长短取决于它们的创建位置和使用方式。这里的关键见解是对短期和长期对象使用不同的垃圾收集策略允许针对每种情况专门优化 GC。
Loosely speaking, as objects "survive" repeated garbage collections in the Young Generationthey are migrated to the Tenured Generation. The Permanent Generationis a special case, it contains objects, that are needed by the JVM, that are not necessarily represented in your program, for example objects that represent classes and methods.
粗略地说,当对象在年轻代中“幸存”重复的垃圾收集时,它们会迁移到年老代。该永久代是一个特例,它包含对象,是由JVM需要的,这不一定是在你的程序来表示,代表的类和方法的示例对象。
Since the Young Generationwill usually contain a lot of garbage in it, it is optimised for getting rid of a lot of unused objects at once. The Tenured Generationsince it contains longer lived objects is optimised for speedy garbage collection without wasting a lot of memory.
由于年轻代通常会在其中包含大量垃圾,因此对其进行了优化以一次性清除大量未使用的对象。在年老代,因为它包含更长的寿命对象进行快速的垃圾收集不浪费大量的内存进行了优化。
With improvements in garbage collection technology the details have become pretty complex and vary depending on your JVM and how it has been configured. You should read the documentationfor the specific JVM you are using if you need to know exactly what is happening.
随着垃圾收集技术的改进,细节变得非常复杂,并且因您的 JVM 及其配置方式而异。如果您需要确切地知道发生了什么,您应该阅读您正在使用的特定 JVM的文档。
That said, there is a simple historical arrangement this is still useful at a conceptual level. Historically the Young Generationwould be a copy collectorand the Tenured Generationbe a mark and sweep collector. A copy collectorpays essentially no CPU cost for getting rid of garbage, most of the cost is in maintaining live objects, the price of this efficiency is heavier memory usage. A mark and sweep collectorpays some CPU cost for both live and unused objects but utilizes memory more efficiently.
也就是说,有一个简单的历史安排,这在概念层面仍然有用。从历史上看,年轻一代是复制收集器,而老生代是标记和清除收集器。一个副本收集基本支付摆脱垃圾没有CPU成本,大部分的成本是保持活动对象,这种效率的价格是较重的内存使用情况。一个标记和清除收集支付一些CPU成本现场和未使用的对象,但更有效地利用内存。
回答by helios
All objects in the heap survive when they are being referenced. When they're not more, the garbage collector (GC) will reclaim their memory.
堆中的所有对象在被引用时都存活下来。当它们不多时,垃圾收集器 (GC) 将回收它们的内存。
PermGen, Young and Tenured are diferent clasifications of objects (or spaces in the heap where they can be).
PermGen、Young 和 Tenured 是对象的不同分类(或堆中它们可能存在的空间)。
PermGen:these objects will be always there, they're not garbage collected. Classes objects are there, interned strings, etc. I don't know if there is a GC there (when system UNloads classes... but it's not a normal thing)
PermGen:这些对象将永远存在,它们不会被垃圾收集。类对象在那里,实习字符串等。我不知道那里是否有 GC(当系统卸载类时......但这不是正常的事情)
Young:when an object is created it's here.
Young:当一个对象被创建时,它就在这里。
Tenured:an object goes to this classification/category when it survives N GC passes (survive = GC passes but this object is referenced so it can't be reclaimed).
Tenured:对象在 N 次 GC 传递中幸存下来时进入此分类/类别(存活 = GC 通过但此对象已被引用,因此无法回收)。
Depending of GC used and some parametrization, GC passes more or less often.
根据使用的 GC 和一些参数化,GC 或多或少地通过。
Then garbage collection can have different approaches to maange objects in the heap. This classification of objects helps to do it.
然后垃圾收集可以有不同的方法来管理堆中的对象。对象的这种分类有助于做到这一点。
回答by simonlord
Here's another excellent (though long) article on how to tune/size your GC parameters, which may help you understand even more:
这是关于如何调整/调整 GC 参数的另一篇优秀(虽然很长)的文章,它可能会帮助您进一步理解:
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/
A very useful read if you're having GC issues and need to know how to read GC logs, or need to understand how your current GC collector works.
如果您遇到 GC 问题并且需要知道如何读取 GC 日志,或者需要了解您当前的 GC 收集器是如何工作的,这是非常有用的读物。
If you want to hook up remote monitoring of a running system to see realtime memory usage and GC runs check this tool out:
如果您想连接正在运行的系统的远程监控以查看实时内存使用情况和 GC 运行,请查看此工具:
回答by Ravindra babu
Java Heap Memoryis part of memory allocated to JVM by Operating System. Whenever we create objects they are created inside heap in java.
Java堆内存是操作系统分配给JVM的一部分内存。每当我们创建对象时,它们都是在 java 的堆内创建的。
Java Heap spaceis divided into three regions or generation for sake of garbage collection called Young Generation, Old or tenured Generation and Permanent Generation. Permanent generation is garbage collected during full gc in hotspot JVM
Java 堆空间为了垃圾收集而分为三个区域或代,称为年轻代、老年代或年老代和永久代。永久代在热点JVM的full gc期间被垃圾收集
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 surviving objects are aged and eventually move to 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.
在老一代用来存放长幸存的对象。通常,为年轻代对象设置一个阈值,当达到该年龄时,对象将移动到年老代。最终需要收集老年代。此事件称为主要垃圾收集。
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 在运行时根据应用程序使用的类填充永久代。
PermGenhas been replaced with Metaspacesince Java 8 release. PermSize & MaxPermSizeparameters will be ignored now. Have a look this dzone articleby Pierre - Hugues Charbonneau to understand about Metaspace.
自 Java 8 发布以来,PermGen已被Metaspace取代。现在将忽略PermSize 和 MaxPermSize参数。查看Pierre-Hugues Charbonneau 的这篇dzone 文章,以了解有关 Metaspace 的信息。
Image source:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
图片来源:http: //www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Refer to same article for more details.
有关更多详细信息,请参阅同一文章。