Garbage Collection and JNI

974 views
Skip to first unread message

Jeremiah Sellars

unread,
Apr 23, 2010, 3:42:53 PM4/23/10
to android-ndk
Apologies for pulling up another thread right away, but I realize I
kind of closed up the other one without getting the first question
answered....


If I declare a global array (or structure, pointer, variable, etc...)
in native source like:

jint my_array[10];
jint* my_pointer;
//etc...

//Function prototypes and definitions below....
JNIEXPORT void JNICALL Java_com_testing_tests3d_Shape_mdLoader(JNIEnv*
env, jobject thiz){

//Stuff...

}

Are the global items susceptible to Garbage Collection even though
they are not a part of any Class?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Hans-Werner Hilse

unread,
Apr 23, 2010, 3:54:33 PM4/23/10
to andro...@googlegroups.com
Hi,


If I declare a global array (or structure, pointer, variable, etc...)
in native source like:

jint my_array[10];
jint* my_pointer;
  
Those would then be global (within your JNI native code) symbols...
[...]
Are the global items susceptible to Garbage Collection even though
they are not a part of any Class?
  
No.
But beware: We're only talking about the primitive types here. When dealing with Java Objects, however, then they would be subject to GC. In that case, the jobject type only stores an object identifier, the referenced object is managed by the JVM. JNI is not going to suddenly start GC'ing the memory you allocated in JNI code, _except_ for the case that you use JNI functions to have the JVM allocate something for you (creating objects, directly allocated bytebuffers) or you use JNI functions to "get" Java objects (you get references, but not objects by using these functions). There are JNI functions to make the GC aware that you're using those objects and to keep the GC from collecting them.

But it's all pretty well documented in Sun's JNI documentation:
http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

-hwh

Jeremiah Sellars

unread,
Apr 23, 2010, 4:23:55 PM4/23/10
to android-ndk
Thank you so much, I've been in and out of that doc along with the
specific tips for JNI on Android, but have yet to really push myself
to give it a full read through.

Ultimately, I'm really only planning on using JNI to run my OpenGL
calls and store vertex/normal/texture coordinate data in primitive
arrays. I just want to be sure I don't make any weird mistakes (which
it looks like it could be very easy to do) along the way.

Thanks again,
Jeremiah

Hans-Werner Hilse

unread,
Apr 23, 2010, 5:26:30 PM4/23/10
to andro...@googlegroups.com
Hi,
> Thank you so much, I've been in and out of that doc along with the
> specific tips for JNI on Android, but have yet to really push myself
> to give it a full read through.
>
You're welcome. I think it's well written interface documentation and of
course is worth a read :-)
> Ultimately, I'm really only planning on using JNI to run my OpenGL
> calls and store vertex/normal/texture coordinate data in primitive
> arrays. I just want to be sure I don't make any weird mistakes (which
> it looks like it could be very easy to do) along the way.
>
Have a look at the part in the JNI docs about arrays. There might be
some pitfalls, depending on whether you need to interface those arrays
from/to the Java side of things.
Reply all
Reply to author
Forward
0 new messages