Java 中的多个垃圾收集器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16032328/
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
Multiple garbage collectors in Java
提问by Aditya
Every Java process I start on my machine seems to have two garbage collectors by default. I'm checking this via JConsole.
默认情况下,我在我的机器上启动的每个 Java 进程似乎都有两个垃圾收集器。我正在通过 JConsole 进行检查。
Example - for my currently running Eclipse.
示例 - 对于我当前运行的 Eclipse。
PS MarkSweep
PS 标记扫描
Collection Count - 221
Collection Time - 102118
Memory Pool Names - java.lang.String[4]
PS Scavenge
PS清道夫
Collection Count - 241
Collection Time - 2428
Memory Pool Names - java.lang.String[2]
I am assuming they have overlapping pools. How do two garbage collectors work together when using the same pools (Eden, survivor, old gen)? Is there no overlap in movement of objects between pools (like movement from one survivor to another when the second algorithm is called)? Even if it is not, why do we need more than one collector per pool?
我假设他们有重叠的池。使用相同的池(Eden、survivor、old gen)时,两个垃圾收集器如何协同工作?池之间的对象移动是否没有重叠(例如调用第二个算法时从一个幸存者移动到另一个幸存者)?即使不是,为什么每个池需要一个以上的收集器?
I have read this article on GC. They refer to using different collectors for different major and minor GC, but there seems to be no reference to using multiple collectors on the same pool.
我已经阅读了这篇关于 GC 的文章。他们指的是为不同的主要和次要 GC 使用不同的收集器,但似乎没有提到在同一池上使用多个收集器。
回答by Dave Webb
I am assuming they have overlapping pools.
我假设他们有重叠的池。
This assumption is wrong. PS Scavenge will be used on the young (eden, survivor) generation and PS MarkSweep will be used on the old generation. The only "overlap" is that PS Scavenge will move objects into the old generation once they've been around a while and let PS MarkSweep deal with them then.
这个假设是错误的。PS Scavenge 将用于年轻代(伊甸园、幸存者),而 PS MarkSweep 将用于年老代。唯一的“重叠”是 PS Scavenge 将在对象存在一段时间后将对象移动到老年代,然后让 PS MarkSweep 处理它们。
The benefit of having different garbage collectors for different pools is that an algorithm that works well for objects in the eden pool isn't necessarily going to work well for old generation objects.
为不同的池使用不同的垃圾收集器的好处是,对 eden 池中的对象运行良好的算法不一定适用于老年代对象。
This article covers the various options for different garbage collectors working together.
As far as "major" collections which occur when there is no space to move objects into the old generation, this (admittedly old) whitepaper from Sunsays the following:
至于当没有空间将对象移动到老年代时发生的“主要”集合,来自 Sun 的这份(公认的旧)白皮书说如下:
...the young generation collection algorithm is not run. Instead, the old generation collection algorithm is used on the entire heap.
...年轻代收集算法没有运行。相反,在整个堆上使用老年代收集算法。
回答by Mikhail
Form the article you've provided:
形成您提供的文章:
Typically some fraction of the surviving objects from the young generation are moved to the tenured generation during each minor collection. Eventually, the tenured generation will fill 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.
通常,在每个次要收集期间,年轻代中幸存对象的一部分被移动到年老代。最终,年老代将填满并且必须被收集,从而产生一个主要收集,在其中收集整个堆。主要集合的持续时间通常比次要集合长得多,因为涉及的对象数量要多得多。

