Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:31:43+00:00 2026-05-27T02:31:43+00:00

I have some JNI code in a thread that calls from Java to C.

  • 0

I have some JNI code in a thread that calls from Java to C. The code works but if I put too much pressure on it (execute too many instructions), it will crash:

W/dalvikvm( 1502): JNI local reference table summary (512 entries):
W/dalvikvm( 1502):   512 of Ljava/lang/Class; 164B (1 unique)
W/dalvikvm( 1502): Memory held directly by tracked refs is 164 bytes
E/dalvikvm( 1502): Failed adding to JNI local ref table (has 512 entries)
I/dalvikvm( 1502): "Thread-17" prio=5 tid=10 RUNNABLE
I/dalvikvm( 1502):   | group="main" sCount=0 dsCount=0 s=N obj=0x40114910 self=0xd3d40
I/dalvikvm( 1502):   | sysTid=1542 nice=0 sched=0/0 cgrp=default handle=821104
I/dalvikvm( 1502):   | schedstat=( 359863282 630188006 2660 )
I/dalvikvm( 1502):   at dalvik.system.NativeStart.run(Native Method)
I/dalvikvm( 1502): 
E/dalvikvm( 1502): VM aborting
I/DEBUG   ( 1011): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   ( 1011): Build fingerprint: 'verizon/voles/sholes/sholes:2.2.2/FRG83G/91102:user/release-keys'
I/DEBUG   ( 1011): pid: 1502, tid: 1542  >>> app_process <<<
I/DEBUG   ( 1011): signal 11 (SIGSEGV), fault addr deadd00d
I/DEBUG   ( 1011):  r0 00000026  r1 afd14629  r2 afd14629  r3 00000000
I/DEBUG   ( 1011):  r4 805a23f4  r5 805a23f4  r6 000d3d40  r7 000d3d90
I/DEBUG   ( 1011):  r8 00100000  r9 80601f45  10 449f1000  fp 4453ed90
I/DEBUG   ( 1011):  ip deadd00d  sp 44af0dc0  lr afd15673  pc 805420b8  cpsr 20000030

In my call from Java to C, I keep a GlobalReference to the class I want to call and then create a new thread:

JNIEXPORT void JNICALL Java_com_device_client_HostConnection_initialize
(JNIEnv * env, jobject obj, jobject inject)
{
    JavaVM *tmpVM;
    jint result = env->GetJavaVM(&tmpVM);
    if (result < 0) {
        LOGE("Error using GetJavaVM\n");
        exit(-1);
    }

    jobject tmpHostObject = env->NewGlobalRef(inject);
    if (tmpHostObject == NULL) {
        LOGE("ERROR: Run out of memory for weak global ref\n");
        exit(-1);
    }

    vm = tmpVM;
    hostObject = tmpHostObject;

    pthread_t pth;
    pthread_create(&pth, NULL, startServer, NULL);
    pthread_join(pth, NULL);
}

Here vm and hostObject are global variables. In my new thread, I call a Java method from the inject object that I passed along and created a GobalRef to:

void *startServer(void* arg)
{
    JNIEnv* env = NULL;
    jmethodID mid;
    jclass hostClass;

    vm->AttachCurrentThread(&env, NULL);

    while (run) {
        /* some code */

        /* Calls a new thread */
        pthread_t pth;
        pthread_create(&pth, NULL, receive, NULL);

        /* more code */
    }
    vm->DetachCurrentThread();
}

void *receive()
{
    jmethodID mid;
    jclass hostClass;
    JNIEnv* env = NULL;

    vm->AttachCurrentThread(&env, NULL)

    while (1) {
        /* receive x, y and action */

        hostClass = env->GetObjectClass(hostObject);
        mid = env->GetMethodID(hostClass, "executeTouch", "(III)V");
        if (mid == 0) {
            LOGD("ERROR: GetMethodID\n");
            exit(-1);
        }
        env->CallVoidMethod(hostObject, mid, x * 2, y * 2, action);
    }

    vm->DetachCurrentThread();
}

Does anyone please know what is going on? I am running out of memory please? Why would that be and how could I please avoid it?

EDIT:

I realized that I didn’t need to get the MethodID all the time, I could just store it locally before the while (1). This fixed my problem! Sorry for the issue, thanks.

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:31:43+00:00Added an answer on May 27, 2026 at 2:31 am

    I realized that I didn’t need to get the MethodID all the time, I could just store it locally before the while (1). This fixed my problem! Sorry for the issue, thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a java application that uses some jni calls. I have on C
I have a C program that stores some object in java store using JNI.
I have written some code which passes values between java and c using jni.
JAVA Code Here is some part of my code that I have written in
I have to code a Java program that will receive messages from network and
I have some image processing Java code in Android that acts upon two large
I have some UI in VB 2005 that looks great in XP Style, but
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some objective-c code that uses an NSLock to implement a sort of
I have a java applet. A class inside that applet is creating a thread

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.