Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 296
cedrics Avatar asked May 22 '13 15:05

cedrics


People also ask

What does stop the world mean GC?

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events. The Old Generation is used to store long surviving objects.

Is Major GC stop the world?

Stop the World Event: It is a time when all the application threads are stopped until the garbage collection operation completes. Both minor and major garbage collections are “Stop the World” events.

Is parallel GC stop the world?

Parallel GC is a parallel stop-the-world collector, which means that when a GC occurs, it stops all application threads and performs the GC work using multiple threads. The GC work can thus be done very efficiently without any interruptions.

What is JVM GC pause?

Garbage collection (GC) is the process by which Java removes data that is no longer needed from memory. A garbage collection pause, also known as a stop-the-world event, happens when a region of memory is full and the JVM requires space to continue. During a pause all operations are suspended.


2 Answers

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.

like image 117
Alexey Ragozin Avatar answered Oct 06 '22 10:10

Alexey Ragozin


Using the throughput GC, the JVM needs STW pauses to free as much memory as possible. It is only using such pauses that it is the most effective.

Using the low-pauses collector (CMS), you clean the old generation concurrently, without pausing your application. The drawback is that the old generation become fragmented. If it is too fragmented and need a compaction, a Full GC (STW) happens. However, you can always tune your application so that you do not get any Full GC.

G1 GC is a special case. Its current primary goal is to have a low fragmentation on the heap, while still being concurrent (like CMS). When it cannot reach this goal, the JVM also reverts to a STW pause so that the heap is entirely cleaned and compacted.

like image 10
Pierre Laporte Avatar answered Oct 06 '22 11:10

Pierre Laporte