简体   繁体   English

Java GC停止并复制

[英]Java GC Stop and copy

This question is a follow up to my previous Java GC question: Java Garbage Collector clarification 这个问题是我之前的Java GC问题的后续活动: Java Garbage Collector澄清

This question is also referring to the same article. 这个问题也指的是同一篇文章。

I'm a little confused on why the stop and copy method for defragmenting object heap allocation is so commonly used. 我对为什么对对象堆分配进行碎片整理的stop and copy方法如此常用感到有些困惑。 Yes it defragments the heap however it seems like there is tons of overhead because basically you cut the total amount of heap size in half. 是的,它对堆进行了碎片整理,但是似乎有很多开销,因为基本上可以将堆大小的总和减少一半。 Also you need to copy ALL the live objects when one half has run out of space. 当一半空间用完时,您还需要复制所有活动对象。

Other than defragmentation is there any other fundamental reason why 'stop and copy' is better than say 'mark and sweep'? 除了碎片整理,还有其他根本原因导致“停止并复制”比说“标记并清除”更好吗?

Actually, fragmentation is fundamental, and the ability of some GC to defeat it is a considerable asset. 实际上,碎片化最基本的,某些GC击败它的能力是相当重要的。

The stop-and-copy algorithm used to be popular in GC implementations because: 停止复制算法曾经在GC实现中流行,因为:

  1. it is simple to implement; 实现起来很简单;
  2. it automatically defragments memory; 它会自动对内存进行碎片整理;
  3. its running time is proportional to the amount of live objects, which makes it asymptotically very efficient. 它的运行时间与活动物体的数量成正比,这使得它渐近地非常有效。

More modern GC, including those used in Java, use much more complex strategies because they want to make short pauses (rather than making total GC time low, they prefer never to stop the application for a long time, because pauses are bad for interactivity), to interact more cleanly with caches and virtual memory, and to benefit from systems with multiple CPU. 较现代的GC(包括Java中使用的GC)使用更复杂的策略,因为它们想使短暂的暂停 (而不是使GC的总时间变 ,他们宁愿从不停止长时间的应用程序,因为暂停不利于交互性) ,以便与缓存和虚拟内存进行更清晰的交互,并受益于具有多个CPU的系统。

The Jones and Lins book is a must-read for whoever wants to understand garbage collection. 琼斯和林斯的书对于任何想了解垃圾收集的人来说都是必读的。

A great tutorial on the garbage collector is Tuning Garbage Collection (unfortunately the new oracle website has messed its formatting up quiet a lot). 关于垃圾收集器的一个很棒的教程是Tuning Garbage Collection (不幸的是,新的oracle网站将其格式化变得非常混乱)。

Your question is handled in chapter V. This basically explains which types of strategies you can use in the Java garbage collector and which are default. 您的问题在第五章中进行了处理。这基本上解释了可以在Java垃圾收集器中使用的策略类型,以及哪些是默认策略。 Most desktop applications will be interested in a stop that is as small as possible, because this is what the user might notice. 大多数桌面应用程序都会对尽可能小的停止感兴趣,因为这可能会引起用户注意。

Note that your question is not about defragmentation. 请注意,您的问题与碎片整理无关。 Both will eventually compress the memory space. 两者最终都会压缩内存空间。

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

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