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.
相关问题
- C# GC not freeing memory [duplicate]
- garbage collection best practices
- Too many Garbage collection threads
- Java 7 G1GC strange behaviour
- JVM abnormal exit - cleanup of system resources
相关文章
- java项目突然挂掉,日志无报错信息
- Java引用分为强引用、软引用、弱引用、虚引用,我怎么能判断出一个对象是哪个引用?
- java.lang.VerifyError: Stack map does not match th
- Do errors thrown within UncaughtExceptionHandler g
- What is the up-front cost of an object being final
- Running Eclipse on Mac - JVM Version 1.7 or greate
- module not found: com.eed3si9n#sbt-assembly;0.14.3
- Why do we need Dispose() method on some object? W
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).