Java 主要和次要垃圾收集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16549066/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:16:51  来源:igfitidea点击:

Java Major and Minor Garbage Collections

javagarbage-collection

提问by goblinjuice

I have been reading up on Garbage Collection in Java and SO Q&A but I'm confused about types of Garbage Collection.

我一直在阅读 Java 中的垃圾收集和 SO Q&A,但我对垃圾收集的类型感到困惑。

Let's take Throughput Collector as an example. (aka Parallel Collector). The docs say it uses multiple threads to do the Minorcollections and single thread for Majorcollections (same as Serial collector).

我们以吞吐量收集器为例。(又名并行收集器)。文档说它使用多线程来执行次要集合和单线程来执行主要集合(与串行收集器相同)。

Now my questions:

现在我的问题:

  1. What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? Or b) Full GC == Major Collections? Which one is it?
  2. If a), does it mean that the Minor Collection is still done using multiple threads whereas the Major was done using Single?
  3. If b), does it mean both Young & Old Generations were cleared using Single Thread?
  1. Full GC 是什么意思:a) 这是否意味着 Minor 和 Major 收集都已完成?或者 b) Full GC == Major Collections?哪一个?
  2. 如果 a),这是否意味着 Minor Collection 仍然使用多线程完成,而 Major 使用 Single 完成?
  3. 如果 b),这是否意味着使用单线程清除了年轻代和老年代?

Also, 4. Does Full GC only affect OldGeneration or YoungGeneration as well?

另外, 4. Full GC 是否也只影响 OldGeneration 或 YoungGeneration?

Thanks in advance.

提前致谢。

回答by goblinjuice

Let me explain.

让我解释。

Let's take Throughput Collector as an example. (aka Parallel Collector). The docs say it uses multiple threads to do the Minor collections and single thread for Major collections (same as Serial collector).

我们以吞吐量收集器为例。(又名并行收集器)。文档说它使用多线程来执行次要集合和单线程来执行主要集合(与串行收集器相同)。

Here's something to understand. By default, on most newer systems, JVM uses TWO different Garbage Collectors for Young and Old Generations. On my my machine: I have Parallel New Collectorfor the Young Generation and Concurrent Mark and Sweep Collectorfor the Older Generation.

这里有一点需要理解。默认情况下,在大多数较新的系统上,JVM 为年轻代和老年代使用两个不同的垃圾收集器。在我的机器上:我有年轻代的并行新收集器老一代的并发标记和清除收集器

Minor Collection is triggered when then JVM is unable to allocate space for a new Object (Remember: new objects are always allocated in Young Generation's Eden Area).

当 JVM 无法为新对象分配空间时,会触发次要收集(记住:新对象总是分配在年轻代的伊甸园区域)。

Next Question:

下一个问题:

What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? Or b) Full GC == Major Collections? Which one is it?

Full GC 是什么意思:a) 这是否意味着 Minor 和 Major 收集都已完成?或者 b) Full GC == Major Collections?哪一个?

and,

和,

Also, 4. Does Full GC only affect OldGeneration or YoungGeneration as well?

另外, 4. Full GC 是否也只影响 OldGeneration 或 YoungGeneration?

It depends. JVM reports every Major Collection as Full GC. [Try with these flags java -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamp]. The pedantic definition is that Full GC runs Minor first followed by Major (Although the order could be switched if the Older Generation is full in which case it is freed first to allow it to receive objects from the Young Generation).

这取决于。JVM 将每个 Major Collection 报告为 Full GC。[尝试使用这些标志java -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamp]。迂腐的定义是Full GC先运行Minor,然后是Major(尽管如果老年代已满,顺序可能会改变,在这种情况下,它首先被释放以允许它从年轻代接收对象)。

OK, back to the point. JVM considers Major Collection [in the Older (or Perm) Generation] as Full GC. Below are outputs from a program I was able to quickly write to illustrate the point. The first line is Minor GC and the second is Major (Full) GC. You can see that it only happened in the Older Generation (CMS) and was able to reduce old generation from 1082K to 1034K.

好,回到正题。JVM 将 Major Collection [在 Older (or Perm) Generation 中] 视为 Full GC。以下是我能够快速编写以说明这一点的程序的输出。第一行是 Minor GC,第二行是 Major (Full) GC。你可以看到它只发生在老年代 (CMS) 中,并且能够将老年代从 1082K 减少到 1034K。

  • 11.431: [GC 11.431: [ParNew: 1152K->128K(1152K), 0.0009893 secs] 2111K->1210K(6464K), 0.0010182 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  • 17.882: [Full GC (System) 17.882: [CMS: 1082K->1034K(5312K), 0.0212614 secs] 2034K->1034K(6464K), [CMS Perm : 9426K->9410K(21248K)], 0.0213200 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]
  • 11.431: [GC 11.431: [ParNew: 1152K->128K(1152K), 0.0009893 secs] 2111K->1210K(6464K), 0.0010182 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
  • 17.882: [Full GC (System) 17.882: [CMS: 1082K->1034K(5312K), 0.0212614 secs] 2034K->1034K(6464K), [CMS Perm : 9426K->9410K(21248K)], 0.0213200 secs] [Times: user=0.02 sys=0.00, real=0.02 secs]

Next question:

下一个问题:

If a), does it mean that the Minor Collection is still done using multiple threads whereas the Major was done using Single?

如果 a),这是否意味着 Minor Collection 仍然使用多线程完成,而 Major 使用 Single 完成?

Yes. See the beginning of my answer. Young and Older Generations are served by different Collectors. For Young Generation, you can use any one of the following:

是的。见我回答的开头。年轻一代和老一代由不同的收藏家服务。对于年轻代,您可以使用以下任何一种:

  • -XX:+UseSerialGC
  • -XX:+UseParallelGC
  • -XX:+UseParNewGC
  • -XX:+UseSerialGC
  • -XX:+UseParallelGC
  • -XX:+UseParNewGC

For Old Generation, the available choices are:

对于老年代,可用的选择是:

  • -XX:+UseParallelOldGC
  • -XX:+UseConcMarkSweepGC
  • -XX:+UseParallelOldGC
  • -XX:+UseConcMarkSweepGC

回答by the8472

While goblin's answer is still correct in broad strokes at least this part is now outdated:

虽然 goblin 的答案大体上仍然是正确的,但至少这部分现在已经过时了:

It depends. JVM reports every Major Collection as Full GC.

这取决于。JVM 将每个 Major Collection 报告为 Full GC。

Both CMS and G1 distinguish between new generation (minor) collections, concurrent collections of the old generation and full gcs. The latter are something of a last-resort thing and most of the GCing should be handled by new gen and concurrent collections.

CMS 和 G1 都区分了新生代(次要)回收、老年代并发回收和完整 gc。后者是最后的手段,大部分 GC 应该由新一代和并发集合处理。