JVM 中的垃圾收集器 运行 是从 C++ 创建的吗?

Does Garbage Collector run in JVM created from C++?

我有一个 C++ 代码库,我在其中使用 JNI 创建 JVM 并偶尔与 Java 中实现的库进行交互。我很好奇,在这个用例中,Java 的垃圾收集器是否仍会可靠地 运行 并清理?

我在网上找到的关于 JNI 的大部分信息似乎都是关于“相反”用例的,人们通常似乎主要有 Java 代码,这些代码有时通过 JNI 与本机代码交互。对于这样的用例,我发现例如以下 online:

The automatic garbage collection of local references that are no longer in scope prevents memory leaks in most situations. This automatic garbage collection occurs when a native thread returns to Java (native methods) or detaches from the JVM (Invocation API). Local reference memory leaks are possible if automatic garbage collection does not occur. A memory leak might occur if a native method does not return to the JVM, or if a program that uses the Invocation API does not detach from the JVM.

我不确定在这种情况下“returns 到 Java”到底是什么意思。只是偶尔从 C++ 中调用基于 Java 的方法就足够了吗,这是否已经算作“返回到 Java”?如果没有,是否有任何方法可以确保垃圾收集器在我的用例中有机会 运行?

使用 JNI 创建的 JVM 是一个完整的 JVM,包括 GC。

这样想:通常用于 运行 Java 程序的 java 命令只不过是一个创建 JVM 的小型 JNI 程序,定位 class 在 command-line 上命名,并对 main(String[]) 方法进行静态调用。