垃圾优先垃圾收集器如何工作?

2022-01-16 00:00:00 garbage-collection java g1gc

谁能解释一下 G1 垃圾收集器的工作原理?我还没有在任何地方找到任何全面、易于理解的描述.

Can someone explain how the G1 Garbage Collector works please? I haven't been able to find any comprehensive, easy-to-understand descriptions anywhere yet.

谢谢

推荐答案

收集器将堆分成固定大小的区域并跟踪这些区域中的实时数据.它保留了一组指针——记忆集"——进出该区域.当认为有必要进行 GC 时,它首先收集实时数据较少的区域(因此,垃圾优先").通常,这可能意味着一步收集整个区域:如果指向某个区域的指针数量为零,则不需要对该区域进行标记或扫描.

The collector splits the heap up into fixed-size regions and tracks the live data in those regions. It keeps a set of pointers — the "remembered set" — into and out of the region. When a GC is deemed necessary, it collects the regions with less live data first (hence, "garbage first"). Often, this can mean collecting an entire region in one step: if the number of pointers into a region is zero, then it doesn't need to do a mark or sweep of that region.

对于每个区域,它会跟踪描述收集它们需要多长时间的各种指标.您可以给它一个关于暂停时间的软实时约束,然后它会尝试在该受限时间内尽可能多地收集垃圾.

For each region, it tracks various metrics that describe how long it will take to collect them. You can give it a soft real-time constraint about pause times, and it then tries to collect as much garbage as it can in that constrained time.

有JavaOne谈G1,也有几篇关于这个话题的文章:

There is JavaOne talk about G1 and few articles on the topic:

  • http://developers.sun.com/learning/javaoneonline/j1sessn.jsp
  • http://www.fasterj.com/articles/G1.shtml
  • http://www.drdobbs.com/java/219401061
  • http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

相关文章