* tests/gc/Makefile: Added NativeWeakRef testcase.
* tests/gc/NativeWeakRef.java: Added new testcase.
* tests/gc/native.c: Added native methods for NativeWeakRef testcase.
System.out.println("Cleaning up ...");
v = null;
System.gc();
- //System.gc();
- //System.gc();
System.out.println("Waiting some seconds ...");
try {
} catch (Exception e) {
e.printStackTrace();
}
+
+ System.out.println("Cleaning up again ...");
+ System.gc();
}
public static void main(String args[])
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
--- /dev/null
+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();
+ }
+}
#include <stdio.h>
static jobject ref = NULL;
+static jobject weak_ref = NULL;
JNIEXPORT void JNICALL Java_NativeGlobalRef_setReference(JNIEnv *env, jclass c, jobject o)
{
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;
+}