简体   繁体   English

Java主要和次要垃圾回收

[英]Java Major and Minor Garbage Collections

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中的Garbage Collection,但是对于Garbage Collection的类型感到困惑。

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). 文档说它使用多个线程来进行次要集合,而使用单个线程来进行主要集合(与串行收集器相同)。

Now my questions: 现在我的问题是:

  1. What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? 完整GC的含义是什么:a)是否意味着次要收藏和主要收藏都已完成? Or b) Full GC == Major Collections? 或b)完整GC ==主要收藏? 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? 如果是a),是否表示次要集合仍使用多个线程完成,而主要集合则使用单个线程完成?
  3. If b), does it mean both Young & Old Generations were cleared using Single Thread? 如果是b),是否意味着年轻一代和老一代都使用单线程清除了?

Also, 4. Does Full GC only affect OldGeneration or YoungGeneration as well? 另外,4. Full GC是否也只影响OldGeneration或YoungGeneration?

Thanks in advance. 提前致谢。

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. 默认情况下,在大多数较新的系统上,JVM使用两个不同的年轻代和老一代垃圾收集器。 On my my machine: I have Parallel New Collector for the Young Generation and Concurrent Mark and Sweep Collector for the Older Generation. 在我的机器上:我有年轻一代的并行新收集 ,老一代有并行标记和扫描收集器

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无法为新对象分配空间时会触发次要集合(请记住:新对象始终在Young Generation的Eden区域中分配)。

Next Question: 下一个问题:

What does it mean by a Full GC: a) Does it mean both Minor and Major collections are done? 完整GC的含义是什么:a)是否意味着次要收藏和主要收藏都已完成? Or b) Full GC == Major Collections? 或b)完整GC ==主要收藏? Which one is it? 哪一个?

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. JVM将每个主要集合报告为完整GC。 [Try with these flags java -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamp ]. [尝试使用这些标志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). 根深蒂固的定义是,Full GC首先运行Minor,然后运行Major(尽管如果Old Generation已满,则可以切换顺序,在这种情况下,它首先被释放以允许其接收Young Generation的对象)。

OK, back to the point. 好,回到重点。 JVM considers Major Collection [in the Older (or Perm) Generation] as Full GC. JVM将[较旧(或Perm)代中的] Major Collection视为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. 第一行是次要GC,第二行是主要(完整)GC。 You can see that it only happened in the Older Generation (CMS) and was able to reduce old generation from 1082K to 1034K. 您可以看到它仅在较老的一代(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]

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),是否表示次要集合仍使用多个线程完成,而主要集合则使用单个线程完成?

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

For Old Generation, the available choices are: 对于旧一代,可用的选择是:

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

While goblin's answer is still correct in broad strokes at least this part is now outdated: 尽管妖精的答案在大范围内仍然是正确的,至少现在这部分已经过时了:

It depends. 这取决于。 JVM reports every Major Collection as Full GC. JVM将每个主要集合报告为完整GC。

Both CMS and G1 distinguish between new generation (minor) collections, concurrent collections of the old generation and full gcs. CMS和G1都区分了新一代(次要)集合,旧代的并发集合和完整gcs。 The latter are something of a last-resort thing and most of the GCing should be handled by new gen and concurrent collections. 后者是最后手段,大多数GC应该由新一代和并发集合处理。

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

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