What mechanism JVM use to block threads during sto

2019-03-17 22:13发布

I heard this question on an interview and couldn't provide an answer. Later I searched thru the internet and still didn't find an answer. Can anybody tell me how JVM stops threads during stop-the-world pause when collecting garbage and how it run them again.

1条回答
再贱就再见
2楼-- · 2019-03-17 22:44

For HotSpot and OpenJDK at least, the JVM uses safe points to stop the application's flow in each thread, either introduced in the JITed code or by changing the bytecode mappings for interpreted code (see this post by Alexey Ragozin for more details).

See also this answer by Gil Tene on why safepointing can be an additional issue when dealing with stop-the-world pauses.


Here are more details (as I understand them, I don't claim to be an expert) on the safepointing mechanism in Hotspot/OpenJDK (see for example safepoint.cpp, line 154), based on the above resources, and probably on some articles by Cliff Click on the Azul Systems blog (which seems to have disappeared from the site).

Reaching the safepoint

The JVM needs to get control of the flow from the application, so it depends on the current state of the threads:

  • Blocked

    The JVM already has control of that thread.

  • Running interpreted code

    The interpreter goes into a mode (by swapping its dispatch table) where each bytecode evaluation will be preceded by a safepoint check.

  • Running native code (JNI)

    JNI code runs in a safepoint and can continue running, unless it calls back into Java or calls some specific JVM methods, at which point it may be stopped to prevent leaving the safepoint (thanks Nitsan for the comments).

  • Running compiled code

    The JVM makes a specific memory page (the Safepoint Polling page) unreadable, which makes the periodic reads of that page (inserted into the compiled code by the JIT compiler) fail and go to a JVM handler.

  • Running in the VM or transitioning states (in the VM also)

    The flow goes through the safepoint check at some point anyway, so the VM waits for it.

Stopping at the safepoint

Once a thread is at the safepoint, controlled by the JVM, the JVM simply blocks it from exiting. When all threads have been stopped (i.e. the world is stopped), the JVM can do the garbage collection then release all the threads which resume execution.

For much more details, you can go read this blog post on safepoints written by Nitsan Wakart in the meantime (which itself has even more references).

查看更多
登录 后发表回答