简体   繁体   English

Java 中的“单代”垃圾收集器是什么?

[英]What is a 'single-generation' garbage collector in Java?

In reading about ZGC I notice that it brags about being a 'single-generation garbage collector,' but I rarely see any details on exactly what this term means.在阅读有关 ZGC 的文章时,我注意到它吹嘘自己是“单代垃圾收集器”,但我很少看到有关该术语确切含义的任何细节。

Normal Generational GC正常的分代GC

I'm familiar with Eden, the survivor space, the nursery, hospice care, metaspace, permgen, zombified objects, Noah's Ark and the old age home, so I don't need an explanation of how concurrent mark sweep (CMS GC) or the garbage first (G1 GC) algorithms work.我熟悉伊甸园、幸存者空间、托儿所、临终关怀、元空间、permgen、僵尸对象、诺亚方舟和养老院,所以我不需要解释并发标记扫描 (CMS GC) 或垃圾优先(G1 GC)算法起作用。 My understanding is those are both multi-generational, and I'm good with that.我的理解是这些都是多代的,我对此很满意。

Personally, I always liked being able to go through Java Mission Control and see how many generations of GC cycles an object has lived through.就个人而言,我一直很喜欢能够从 go 到 Java 任务控制,看看 object 经历了多少代 GC 周期。 That was always helpful in troubleshooting a memory leak or GC problem.这总是有助于解决 memory 泄漏或 GC 问题。

Single vs Multi-Generational GC单代与多代 GC

So what exactly is a 'single-generation' garbage collector, and how does that differ from how objects are currently tracked through multiple garbage collection cycles in CMS and G1?那么究竟什么是“单代”垃圾收集器,它与当前在 CMS 和 G1 中通过多个垃圾收集周期跟踪对象的方式有何不同?

tl;dr tl;博士

ZGC has much to brag about, but being single-generation is not among that. ZGC有很多值得吹嘘的地方,但单代不在其中。

Adding multi-generational is planned as future improvement.计划添加多代人作为未来的改进。

Details细节

Some garbage collector implementations distinguish between new objects and old objects, the “young generation” versus “old generation”.一些垃圾收集器实现区分新对象和旧对象,即“年轻一代”和“老一代”。 The goal is to make it cheaper for the programmer to create short-lived objects as they will be more quickly and efficiently disposed of to free up memory.目标是让程序员更便宜地创建短期对象,因为它们将被更快、更有效地处理以释放 memory。

ZGC does not “brag” about being single-generational. ZGC并不“吹嘘”自己是单代的。 Quite the opposite, the team has indicated their desire to add generational features in future versions of ZGC.恰恰相反,该团队表示他们希望在 ZGC 的未来版本中添加分代功能。 See a discussion in this section of a talk by Per Liden of Oracle (wait a few seconds for new slide “Generational ZGC”): https://youtu.be/88E86quLmQA请参阅 Oracle 的 Per Liden 演讲的本部分中的讨论(等待几秒钟以查看新幻灯片“Generational ZGC”): https://youtu.be/88E86quLmQA

For more info on generations, see this Question: Java heap terminology: young, old and permanent generations?有关代的更多信息,请参阅此问题: Java 堆术语:年轻代、年老代和永久代?

You do know the details, but do you know why generational garbage collectors were invented?你知道细节,但你知道为什么要发明分代垃圾收集器吗? Specifically, why G1GC is generational?具体来说,为什么G1GC是分代的?

The idea at the time was that scanning the entire heap was expensive, were expensive meant STW.当时的想法是扫描整个堆是昂贵的,昂贵意味着 STW。 G1 as a matter of fact started its life a non-generational (or single-generation as you call it), but the size of remembered sets and their time to go over them, triggered it to become generational.事实上,G1 的生命始于非世代(或你称之为单世代),但记忆集的大小及其到 go 的时间超过了它们,触发了它成为世代。 The fact that there are "generations" + the fact that G1 scans all the young regions in a single cycle (or better said if it "committed" to scan some X number of young regions at the beginning of some cycle - it must scan them all), means that you do not need for remembered sets to have connections between them in the young space (also card table is a lot smaller).存在“世代”的事实 + G1在单个周期中扫描所有年轻区域的事实(或者更好地说,如果它“承诺”在某个周期开始时扫描一些 X 数量的年轻区域 - 它必须扫描它们all),意味着您不需要记住的集合在年轻空间中的它们之间建立联系(卡片表也小得多)。

As to you direct question, ZGC (as well as Shenandoah ) do not "track" how many cycles an Object has survived, they do not need to do that, as they have no generations, so no need to move Objects somewhere in the "old" to be scanned "later".至于你的直接问题, ZGC (以及Shenandoah )不会“跟踪” Object 存活了多少个周期,他们不需要这样做,因为他们没有世代,所以不需要在“旧”以“稍后”扫描。 They scan the entire heap on each cycle.他们在每个周期扫描整个堆。 They do this concurrently (in a very interesting way), so there is simply no need, at the moment, for them to be generational.他们同时这样做(以一种非常有趣的方式),所以目前根本没有必要让他们世代相传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM