java - Full Gc过程会发生stop the world,这里jvm是怎么处理的呢?【面试题】
迷茫
迷茫 2017-04-18 10:05:31
0
4
911

在不采用cms垃圾收集器或者在cms垃圾收集器初始标记和重新标记阶段,都会发生stop the world,也就是会暂停用户线程的执行,jvm是如何暂停所有用户线程的?烦请知道的朋友帮忙解答一下~
在实际场景中,可能gc的时间很短,但是暂停所有用户线程会用去很大一部分时间,这个时间又是如何得知的呢?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
Ty80

The JVM has something called "safe point" and "safe zone". When GC occurs, all threads will execute to the "safe point" and stop.
When GC is required, the JVM will set a flag. When the thread reaches a safe point, it will poll and detect this flag. If it is found that GC is needed, the thread will hang on its own and will not resume running until the GC is completed.

Another strategy is to directly suspend all threads when GC occurs, and then detect whether all threads are at a safe point. If they are not at a safe point, resume the execution of the thread, and wait until the execution reaches a safe point before suspending.

But for some threads that have not obtained or cannot obtain CPU time, there is no way to wait until it reaches a safe point. Therefore, as long as the thread is in the safe area at this time, GC can also be performed. The safe area is a section of code. The reference relationship of the objects in this code segment will not change, so it is safe to perform GC at this time.

Ty80

The Java method of safely pausing threads is not stable. I think the native method should be used during Full GC.
Get some attention and learn. .

刘奇

Try to avoid full gc and reduce the use of large arrays.

迷茫

GC will not be very frequent, it will only occur when there is insufficient memory (of course it is not absolute), so stop the world will not be very frequent, which ensures that it will not affect user threads, and user threads have a safe zone , to stop the world, you need to wait for all threads to be in the safe zone. It is possible that some long-term operations will wait a long time to reach the safe zone. This means that stop the world may wait longer than the gc itself. The way to solve this problem is to set the safety zone reasonably. For example, there is a big difference between whether you can pause inside the loop and when the loop is completed. The specific setting of the security zone depends on the decision of the jvm manufacturer

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template