Java虚拟机 safepoints 初探

时间:2022-12-27 16:40:55

safepoint的定义很不规范,还跟JVM的具体实现有关,我们的讨论主要针对Hotspot VM。  

先看看openjdk的官方解释:  http://openjdk.java.net/groups/hotspot/docs/HotSpotGlossary.html

Safepoint    :在程序执行过程中,该点处的所有GC roots 是已知的 和 所有heap object contents是 一致的(consistent)。
从全局观点来看,所有线程必须在GC 运行前,在一个safepoint处阻塞(block)。
从局部观点来看,safepoint是一个代码块中特殊的一点,该处正在执行的线程可以因GC而阻塞。
很多调用点都满足要求,可以成为safepoints。存在 strong invariants在所有safepoints中都成立,而在non-safepoints中可能就被无视了。
Java code 和c/c++ code的优化都是在safepoints之间,很少跨过safepoints。
JIT编译器在每个safepoint处生成(emit)了一个GC map。
VM中的C/C++ code 使用格式化的基于宏的约定来标记可能的safepoints。

 什么时候使用safepoints:

  1. GC时的停顿 Garbage collection pauses  
  2. JIT生成的代码反优化  Code deoptimization
  3. Flushing code cache
  4. 类重定义 Class redefinition (e.g. hot swap or instrumentation)
  5. 偏向锁  Biased lock revocation
  6. Various debug operation (e.g. deadlock check or stacktrace dump)

Troubleshooting in safepoints:

通常情况下,safepoints正常工作。有问题时,可以使用下面两个选项帮助诊断问题:

  1. -XX:+PrintGCApplicationStoppedTime – this will actually report pause time for all safepoints (GC related or not). Unfortunately output from this option lacks timestamps, but it is still useful to narrow down problem to safepoints.
  2. -XX:+PrintSafepointStatistics –XX:PrintSafepointStatisticsCount=1 – this two options will force JVM to report reason and timings after each safepoint (it will be reported to stdout, not GC log).

参考文章:

openjdk  官方文档

safepoints in hotspot jvm