--- /dev/null
+CC=gcc
+JAVAC=javac
+
+all:
+ $(JAVAC) NativeGlobalRef.java
+ $(CC) -shared -O2 native.c -o libnative.so -fPIC
+
+clean:
+ rm -rf NativeGlobalRef.class libnative.so
--- /dev/null
+public class NativeGlobalRef {
+ public native static void setReference(Object o);
+ public native static Object getReference();
+
+ public static void main(String args[]) {
+ Object o;
+ System.loadLibrary("native");
+
+ /* create the object we want to deal with */
+ o = new String("I am an important object, don't forget me!");
+ //String s = "";
+ //for (int i=0; i<100; i++)
+ // s = s + "I am an important object, don't forget me!";
+ //o = s;
+ //s = null;
+
+ /* pass the object to the native world */
+ setReference(o);
+
+ /* now forget about it and see if it gets collected */
+ o = null;
+ System.gc();
+
+ /* fill up the heap */
+ //for (long i=0; i<100000000l; i++)
+ // o = new String("I am simply an heap filler!");
+
+ /* is the object still there? */
+ o = getReference();
+ System.out.println(o);
+ }
+}
--- /dev/null
+#include <jni.h>
+#include <stdio.h>
+
+static jobject ref = NULL;
+
+JNIEXPORT void JNICALL Java_NativeGlobalRef_setReference(JNIEnv *env, jclass c, jobject o)
+{
+ printf("Native-World: setReference()\n");
+
+ //ref = o;
+ ref = (*env)->NewGlobalRef(env, o);
+
+ return;
+}
+
+JNIEXPORT jobject JNICALL Java_NativeGlobalRef_getReference(JNIEnv *env, jclass c)
+{
+ printf("Native-World: getReference()\n");
+
+ return ref;
+}