为什么JVM全面GC需要停止世界? [英] Why does the JVM full GC need to stop-the-world?

查看:615
本文介绍了为什么JVM全面GC需要停止世界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被问到了这个问题,并且我刚刚回答因为JVM需要移动对象,但后来我使用了Google,并且似乎找不到任何合理的答案。

解决方案

首先,在wikipedia上垃圾回收文章是真的很好的阅读。



说到。



一般来说GC不需要Stop世界暂停。
有几乎是免费的JVM实现(例如 Azul Zing JVM )。 JVM要求STW收集垃圾取决于它使用的算法。


$ b Mark Sweep Compact(MSC)是默认情况下用于HotSpot的算法。它以STW方式实现,分为3个阶段:
$ b $ ul
  • MARK - 遍历活动对象图来标记可达对象
  • SWEEP - 扫描内存以找到未标记的内存

  • 紧缩 - 重新定位标记的对象以对可用内存进行碎片整理



  • 在堆中重新定位对象时,JVM应修正对该对象的所有引用。在重定位过程中,对象图不一致,这就是STW暂停的必要条件。

    HotSpot JVM,它不利用STW暂停旧的空间集合(与完整集合不完全相同)。



    CMS利用写入屏障(触发器在您每次写入在Java堆中引用)来实现MARK的并发版本并且不使用COMPACT。压缩不足可能会导致碎片,并且如果后台垃圾回收速度不够快,应用程序仍然可能被阻止。在这些情况下,CMS将回退到STW mark-sweep-compact集合。



    还有 G1 ,这是MSC的增量变化。您可以在HotSpot JVM中阅读有关 GC算法的详细信息在我的博客中。


    I was asked this question, and I just answered "because the JVM needs to move objects", but later I Googled and it seems I couldn't find any reasonable answer.

    解决方案

    First, Garbage Collection article at wikipedia is really good reading.

    Speaking of the.

    In general GC does not require Stop-the-World pause. There are JVM implementations which are (almost) pause free (e.g. Azul Zing JVM). Whenever JVM require STW to collect garbage depends on algorithm it is using.

    Mark Sweep Compact (MSC) is popular algorithm used in HotSpot by default. It is implemented in STW fashion and has 3 phases:

    • MARK - traverse live object graph to mark reachable objects
    • SWEEP - scans memory to find unmarked memory
    • COMPACT - relocating marked objects to defragment free memory

    When relocating objects in the heap, the JVM should correct all references to this object. During the relocation process the object graph is inconsistent, that is why STW pause is required.

    Concurrent Mark Sweep (CMS) is another algorithm in HotSpot JVM which does not utilize STW pause for old space collection (not exactly same thing as full collection).

    CMS is utilizing write barrier (trigger acting each time you are writing reference in Java heap) to implement concurrent version of MARK and does not use COMPACT. Lack of compaction may result in fragmentation and if background garbage collection is not fast enough application can still be blocked. In these cases CMS will fallback to STW mark-sweep-compact collection.

    There is also G1 which is an incremental variation of MSC. You can read more about GC algorithms in HotSpot JVM in my blog.

    这篇关于为什么JVM全面GC需要停止世界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆