NewStringUTF Crashing When Called Many Times

1,287 views
Skip to first unread message

John

unread,
Sep 14, 2010, 10:37:04 AM9/14/10
to android-ndk
http://groups.google.com/group/android-ndk/browse_thread/thread/967629d2fdddf8d3?fwc=1

The thread linked above was never answered and I am having the same
issue.
I've isolated the code down to the following for loop.

for (int i=0; i<1000; i++) {
jstring str1 = env->NewStringUTF("Unique Id");
}

When i=505, my Java application inexplicably terminates. Apparently
JNI cannot handle so many NewStringUTF calls.
I'm guessing I'll just have to rearrange my code so that I'm not
creating so many jstrings, but it would be nice if anybody can offer
any pointers.

fadden

unread,
Sep 14, 2010, 4:58:21 PM9/14/10
to android-ndk
On Sep 14, 7:37 am, John <jo-d...@hotmail.com> wrote:
> I've isolated the code down to the following for loop.
>
>         for (int i=0; i<1000; i++) {
>           jstring str1 = env->NewStringUTF("Unique Id");
>         }
>
> When i=505, my Java application inexplicably terminates. Apparently
> JNI cannot handle so many NewStringUTF calls.

Use "adb logcat" to see the failure message from the VM, which will
tell you that you've overflowed your JNI local refs table.

Add "env->DeleteLocalRef(str1)" at an appropriate point in the loop to
avoid the issue.

Read the "JNI Tips" document to understand more about local/global
references.
http://android.git.kernel.org/?p=platform/dalvik.git;a=blob_plain;f=docs/jni-tips.html;hb=HEAD

See in particular the "not excessively allocate" paragraph. (The doc
also has links to the canonical JNI documentation, which you should
definitely read.)

John

unread,
Sep 21, 2010, 10:46:03 AM9/21/10
to android-ndk
Thank you.
Reply all
Reply to author
Forward
0 new messages