Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

garbage collection - Why does the JVM full GC need to stop-the-world?

I think it is because the JVM needs to move objects, is that correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...