garbage collection - What mechanism JVM use to block threads during stop-the-world pause -


i heard question on interview , couldn't provide answer. later searched thru internet , still didn't find answer. can tell me how jvm stops threads during stop-the-world pause when collecting garbage , how run them again.

for hotspot , openjdk @ least, jvm uses safe points stop application's flow in each thread, either introduced in jited code or changing bytecode mappings interpreted code (see this post alexey ragozin more details).

see this answer gil tene on why safepointing can additional issue when dealing stop-the-world pauses.


here more details (as understand them, don't claim expert) on safepointing mechanism in hotspot/openjdk (see example safepoint.cpp, line 154), based on above resources, , on articles cliff click on azul systems blog (which seems have disappeared site).

reaching safepoint

the jvm needs control of flow application, depends on current state of threads:

  • blocked

    the jvm has control of thread.

  • running interpreted code

    the interpreter goes mode (by swapping dispatch table) each bytecode evaluation preceded safepoint check.

  • running native code (jni)

    jni code runs in safepoint , can continue running, unless calls java or calls specific jvm methods, @ point may stopped prevent leaving safepoint (thanks nitsan comments).

  • running compiled code

    the jvm makes specific memory page (the safepoint polling page) unreadable, makes periodic reads of page (inserted compiled code jit compiler) fail , go jvm handler.

  • running in vm or transitioning states (in vm also)

    the flow goes through safepoint check @ point anyway, vm waits it.

stopping @ safepoint

once thread @ safepoint, controlled jvm, jvm blocks exiting. when threads have been stopped (i.e. world stopped), jvm can garbage collection release threads resume execution.

for more details, can go read blog post on safepoints written nitsan wakart in meantime (which has more references).


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -