问题描述
当垃圾收集器在清理未引用的对象之前冻结应用程序线程时,所有线程都必须在其执行过程中处于安全点".我发现了大量描述安全点概念的文章,但很少有例子.在典型的 Java 方法中,安全点应该放在哪里?为什么?更重要的是,哪里不能出现安全点?
When a garbage collector freezes the application threads before cleaning up unreferenced objects, all threads are required to be at a "safepoint" in their execution. I found a wealth of writing describing the concept of a safepoint, but very few examples. Where will a safepoint be placed within a typical Java method and why? And more importantly, where can a safepoint not occur?
推荐答案
safepoint
的确切定义和实现从一个 VM 实现到另一个,但是考虑到 Hotspot VM,你可以找到一个不错的定义在:HotSpot JVM 中的安全点.
The exact definition and implementation of a safepoint
changes from one VM implementation to another, but considering Hotspot VM, you can find a nice definition in: Safepoints in HotSpot JVM.
HotSpot 词汇表 说:
程序执行过程中所有 GC 根都已知且所有堆对象内容一致的点.从全局的角度来看,所有线程都必须在 GC 运行之前阻塞在安全点.
A point during program execution at which all GC roots are known and all heap object contents are consistent. From a global point of view, all threads must block at a safepoint before the GC can run.
通常情况下,安全点是通过 JVM 向方法中注入安全点检查来实现的,大多数调用站点都符合安全点的条件 - 当到达安全点检查时,线程将检查是否需要安全点(例如调度 FullGC),如果是,然后线程阻塞.当 VM 中的所有线程都阻塞时,您已经到达了 VM 中的所有对象都可以完全访问的安全点.然后,执行请求安全点的 VM 操作(例如 FullGC),然后恢复线程.
Typically, the safepoint is implemented by JVM injecting a safepoint check into a method, most call sites qualify as safepoints - when reaching the safepoint check, the thread will check if the safepoint is required (e.g. FullGC is scheduled), if yes, then the thread blocks. When all the threads in the VM block, you have reached the safepoint where all the the objects in the VM are perfectly reachable. Then, the VM operation that requested the safepoint is performed (e.g. a FullGC), after that the threads are resumed.
检查需要安全点的 VM 操作列表:安全第一:安全点.
Check for the list of VM operations requiring a safepoint : Safety First: Safepoints.
您可以使用 -XX:+PrintSafepointStatistics –XX:PrintSafepointStatisticsCount=1
研究 Hotspot 中的安全点行为.
You can study safepoint behavior in Hotspot by using -XX:+PrintSafepointStatistics –XX:PrintSafepointStatisticsCount=1
.
这篇关于Java GC 安全点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!