* tests/gc/LockRecords.java: Added additional cleanup pass after waiting.
authorMichael Starzinger <michi@complang.tuwien.ac.at>
Sat, 13 Oct 2007 11:32:30 +0000 (13:32 +0200)
committerMichael Starzinger <michi@complang.tuwien.ac.at>
Sat, 13 Oct 2007 11:32:30 +0000 (13:32 +0200)
* tests/gc/Makefile: Added NativeWeakRef testcase.
* tests/gc/NativeWeakRef.java: Added new testcase.
* tests/gc/native.c: Added native methods for NativeWeakRef testcase.

tests/gc/LockRecords.java
tests/gc/Makefile
tests/gc/NativeWeakRef.java [new file with mode: 0644]
tests/gc/native.c

index 96e78682d9dffd16c19d093e96ac5012ddd0b87c..c3c04a506baceed6dd4e10de28fb44528d93638e 100644 (file)
@@ -70,8 +70,6 @@ public class LockRecords {
                System.out.println("Cleaning up ...");
                v = null;
                System.gc();
-               //System.gc();
-               //System.gc();
 
                System.out.println("Waiting some seconds ...");
                try {
@@ -79,6 +77,9 @@ public class LockRecords {
                } catch (Exception e) {
                        e.printStackTrace();
                }
+
+               System.out.println("Cleaning up again ...");
+               System.gc();
        }
 
        public static void main(String args[])
index ac126fe2db2511868e9f2fad8c908ff3fc3aa323..346c589c8bc18881d72b8480ca56f52210ea3261 100644 (file)
@@ -2,8 +2,8 @@ CC=gcc
 JAVAC=javac
 
 all:
-       $(JAVAC) NativeGlobalRef.java
+       $(JAVAC) NativeGlobalRef.java NativeWeakRef.java
        $(CC) -shared -O2 native.c -o libnative.so -fPIC
 
 clean:
-       rm -rf NativeGlobalRef.class libnative.so
+       rm -rf NativeGlobalRef.class NativeWeakRef.java libnative.so
diff --git a/tests/gc/NativeWeakRef.java b/tests/gc/NativeWeakRef.java
new file mode 100644 (file)
index 0000000..ce5575a
--- /dev/null
@@ -0,0 +1,31 @@
+public class NativeWeakRef {
+       public native static void setWeakReference(Object o);
+       public native static Object getWeakReference();
+       public native static void delWeakReference();
+
+       public static void main(String args[]) {
+               Object o;
+               System.loadLibrary("native");
+
+               /* create the object we want to deal with */
+               o = new String("I am not important, you can forget me!");
+
+               /* pass the object to the native world */
+               setWeakReference(o);
+
+               /* is the object still there? */
+               o = getWeakReference();
+               System.out.println(o);
+
+               /* now forget about it and see if it gets collected */
+               o = null;
+               System.gc();
+
+               /* is the object still there? */
+               o = getWeakReference();
+               System.out.println(o);
+
+               /* delete the reference inside the native world */
+               delWeakReference();
+       }
+}
index 6451ec01fcc40364b2f917226f633d1193e0baf8..07063b2e7694f8bbd40c8fb19636f4cd3ca8c907 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 
 static jobject ref = NULL;
+static jobject weak_ref = NULL;
 
 JNIEXPORT void JNICALL Java_NativeGlobalRef_setReference(JNIEnv *env, jclass c, jobject o)
 {
@@ -28,3 +29,33 @@ JNIEXPORT void JNICALL Java_NativeGlobalRef_delReference(JNIEnv *env, jclass c)
 
        return;
 }
+
+JNIEXPORT void JNICALL Java_NativeWeakRef_setWeakReference(JNIEnv *env, jclass c, jobject o)
+{
+       printf("Native-World: setWeakReference()\n");
+
+       weak_ref = (*env)->NewWeakGlobalRef(env, o);
+
+       return;
+}
+
+JNIEXPORT jobject JNICALL Java_NativeWeakRef_getWeakReference(JNIEnv *env, jclass c)
+{
+       jobject local_ref;
+
+       printf("Native-World: getWeakReference()\n");
+
+       //local_ref = weak_ref;
+       local_ref = (*env)->NewLocalRef(env, weak_ref);
+
+       return local_ref;
+}
+
+JNIEXPORT void JNICALL Java_NativeWeakRef_delWeakReference(JNIEnv *env, jclass c)
+{
+       printf("Native-World: delWeakReference()\n");
+
+       (*env)->DeleteWeakGlobalRef(env, weak_ref);
+
+       return;
+}