Skip to content

Commit 93c3872

Browse files
authored
[Hello-Java.Base] MonoVM + peer collection (#1319)
Context: a5b229d Now that MonoVM can be used on the desktop (a5b229d), let's show it in action! Update `samples/Hello-Java.Base` to call `GC.Collect()` and `GC.WaitForPendingFinalizers()` to help cause the `MyJLO` instance to be collected before app exit. Which is fine, but what we also need are *GREF logs*. Unfortunately, we can't use `JreRuntimeOptions.JniGlobalReferenceWriter` for that purpose, because `libjava-interop.dylib` is native code and we want both managed and native code to write GREF logs to the same place. Instead, set the `JAVA_INTEROP_GREF_LOG` environment variable to a file path: % JAVA_INTEROP_GREF_LOG=g.txt ./samples/Hello-Java.Base/bin/Release/osx-x64/publish/Hello-Java.Base MonoVM support enabled # jonp: LoadJvmLibrary(/Library/Java/JavaVirtualMachines/microsoft-17.jdk/Contents/Home/lib/libjli.dylib)=1164832560 # jonp: JNI_CreateJavaVM=4554903296; JNI_GetCreatedJavaVMs=4554903376 # jonp: executing JNI_CreateJavaVM=10f7e4f00 # jonp: r=0 javavm=111647690 jnienv=7fbfb7afbaa8 WARNING in native method: JNI call made without checking exceptions when required to from CallStaticObjectMethodV binding? net.dot.jni.sample.MyJLO@575bab75 WARNING in native method: JNI call made without checking exceptions when required to from CallVoidMethodV Then read `g.txt`. Selected output includes: +g+ grefc 5 gwrefc 0 obj-handle 0x7fbfb6104920/L -> new-handle 0x7fbfb57274c0/G from thread '(null)'(4) at Java.Interop.MonoRuntimeObjectReferenceManager.CreateGlobalReference(JniObjectReference reference) at Java.Interop.JniObjectReference.NewGlobalRef() at Java.Interop.JniRuntime.JniValueManager.ConstructPeer(IJavaPeerable peer, JniObjectReference& reference, JniObjectReferenceOptions options) at Java.Interop.JavaObject.Construct(JniObjectReference& reference, JniObjectReferenceOptions options) at Java.Lang.Object..ctor() at Hello.MyJLO..ctor() at Hello.App.<>c.<CreateJLO>b__3_0() at System.Threading.Thread.StartCallback() Created PeerReference=0x7fbfb57274c0/G IdentityHashCode=0x575bab75 Instance=0xef12a08e Instance.Type=Hello.MyJLO, Java.Type=net/dot/jni/sample/MyJLO *take_weak obj=0x10fca0478; handle=0x7fbfb57274c0 +w+ grefc 7 gwrefc 1 obj-handle 0x7fbfb57274c0/G -> new-handle 0x7fbf9570a801/W from thread '(null)'(1) take_weak_global_ref_jni -g- grefc 6 gwrefc 1 handle 0x7fbfb57274c0/G from thread '(null)'(1) take_weak_global_ref_jni *try_take_global obj=0x10fca0478 -> wref=0x7fbf9570a801 handle=0x0 -w- grefc 6 gwrefc 0 handle 0x7fbf9570a801/W from thread '(null)'(1) take_global_ref_jni Finalizing PeerReference=0x0/G IdentityHashCode=0x575bab75 Instance=0xef12a08e Instance.Type=Hello.MyJLO Which shows the creation of JNI handle 0x7fbfb57274c0/G, which is associated with `Hello.MyJLO`, and then we go through the GC process: * Create a JNI Weak Global Reference 0x7fbf9570a801/W (`+w+ …`) * Delete the JNI Global Reference 0x7fbfb57274c0/G (`-g- …`) * Perform a Java-side GC (not mentioned) * Try to create a JNI Global Reference from the Weak Global Reference (`*try_take_global …`) * Delete the JNI Weak Global Reference (`-w- …`) * Finalize the `Hello.MyJLO` instance (`Finalizing PeerReference=0x0/G …`) The `Instance=` value can be used to correlate instances; it's the value of `RuntimeHelpers.GetHashCode()`.
1 parent a5b229d commit 93c3872

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

samples/Hello-Java.Base/Program.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ public static void Main (string[] args)
3838
v => reportTiming = v != null },
3939
{ "v|verbosity:",
4040
$"Set console log verbosity to {{LEVEL}}. Default is 0.",
41-
(int? v) => {
42-
verbosity = v.HasValue ? v.Value : verbosity + 1;
43-
if (verbosity > 0) {
44-
logger = CreateConsoleLogger ();
45-
}
46-
}
47-
},
41+
(int? v) => verbosity = v.HasValue ? v.Value : verbosity + 1 },
4842
{ "h|help",
4943
"Show this message and exit.",
5044
v => showHelp = v != null },
@@ -54,6 +48,9 @@ public static void Main (string[] args)
5448
options.WriteOptionDescriptions (Console.Out);
5549
return;
5650
}
51+
if (verbosity > 0) {
52+
logger = CreateConsoleLogger ();
53+
}
5754
var builder = new JreRuntimeOptions () {
5855
JniAddNativeMethodRegistrationAttributePresent = true,
5956
JvmLibraryPath = jvmPath ?? global::Java.InteropTests.TestJVM.GetJvmLibraryPath (logger),
@@ -79,6 +76,11 @@ public static void Main (string[] args)
7976
}
8077

8178
CreateJLO ();
79+
80+
GC.Collect ();
81+
GC.Collect ();
82+
GC.WaitForPendingFinalizers ();
83+
GC.WaitForPendingFinalizers ();
8284
}
8385

8486
static Action<TraceLevel, string> CreateConsoleLogger ()

0 commit comments

Comments
 (0)