gpt4 book ai didi

garbage-collection - JNI 代码即与垃圾收集并发执行。

转载 作者:行者123 更新时间:2023-12-01 12:11:45 26 4
gpt4 key购买 nike

它写在 http://psy-lob-saw.blogspot.com/2015/12/safepoints.html

A Java thread is at a safepoint while executing JNI code. Before crossing the native call boundary the stack is left in a consistent state before handing off to the native code. This means that the thread can still run while at a safepoint.



这怎么可能?毕竟,我可以将对象的引用传递给 JNI .
在 JNI 中,我可以在该对象中设置一个字段。

很明显是收不到的(我们有本地引用)。但是,它可以在完整的 gc 收集期间通过 GC 移动到老年代。
所以,我们有以下情况:
GC collector:                          | Thread executing JNI code
compact old generation | modify object fields that can be
and move object from young generation | moved now! A catastrophe.
to old generation. |

JVM 如何处理?

最佳答案

几乎每个 JNI 调用都有一个安全点保护。每当您从 native 方法调用 JNI 函数时,线程都会从 in_native 切换。至 in_vm状态。此转换的一部分是安全点检查。

ThreadStateTransition::transition_from_native()其中调用 JavaThread::check_safepoint_and_suspend_for_native_trans(thread)

// Slow path when the native==>VM/Java barriers detect a safepoint is in
// progress or when _suspend_flags is non-zero.
// Current thread needs to self-suspend if there is a suspend request and/or
// block if a safepoint is in progress.

也就是说,在 GC 处于事件状态时调用 JNI 函数的线程将被挂起,直到 GC 完成。

关于garbage-collection - JNI 代码即与垃圾收集并发执行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51321807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com