Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether it is a memory leak or not when calling native code in Java?

I am calling a native function in my main and it is inside a while loop.

public static void main (String[] args) throws Throwable {

        testDLL test = new testDLL();
        String ar[];
        while (true){
            System.out.println("Memory before garbage collection: " + Runtime.getRuntime().freeMemory());           
            ar = test.GetSomething("###");      
            test.finalize();
            System.gc();
            Thread.sleep(5000);
            System.out.println("Memory after garbage collection: " + Runtime.getRuntime().freeMemory());
            System.out.println();
        }
}

the output of the following program is: (running for about 1 min)

Memory before garbage collection: 1915288
Memory after garbage collection: 1915136

Memory before garbage collection: 1915136
Memory after garbage collection: 1914984

Memory before garbage collection: 1914984
Memory after garbage collection: 1916624

Memory before garbage collection: 1916624
Memory after garbage collection: 1916472

Memory before garbage collection: 1916472
Memory after garbage collection: 1916320

Memory before garbage collection: 1916320
Memory after garbage collection: 1916168

Memory before garbage collection: 1916168
Memory after garbage collection: 1916624

Memory before garbage collection: 1916624
Memory after garbage collection: 1916472

I guess it is not a memory leak. But when i open task manager of windows, size of the process javaw.exe keeps on increasing (100 KB on each while iteration). Want to know whether it is a memory leak or should i just ignore it? OR Does that mean there is a memory leak in the native function?

FYI i have double checked my native function of any memory leaks!

Thanks!

EDIT:

Native function:

JNIEXPORT jobjectArray JNICALL Java_testDLL_GetSomething
(JNIEnv * env, jobject jobj, jstring approvedJString){

    const int num = 100;

    jboolean * isCopy;
    jobjectArray serialNumArrJobj;
    const char* approved = env->GetStringUTFChars(approvedJString, isCopy);
    string serialNumArr[num];

    //*
    *   Long lengthy code here
    *   Populates the string array "serialNumArr"
    *//

    // ========

    env->ReleaseStringUTFChars(approvedJString, approved);
    env->DeleteLocalRef(approvedJString);
    env->DeleteLocalRef(jobj);

    ////////////

    int i, sizeOfArr = 0;

    for( i = 0; i < num; i++) {
        if (serialNumArr[i].empty())
            break;
        else
            sizeOfArr++;
    }

    serialNumArrJobj = (jobjectArray)env->NewObjectArray(sizeOfArr,
         env->FindClass("java/lang/String"),
         env->NewStringUTF(""));


    for( i = 0; i < sizeOfArr; i++) {
        env->SetObjectArrayElement(serialNumArrJobj,
            i,
            env->NewStringUTF(serialNumArr[i].c_str()));
    }

    return serialNumArrJobj;
}
like image 512
HashimR Avatar asked Nov 23 '11 06:11

HashimR


1 Answers

Your expectations are misplaced. You can't expect System.gc() to be more than a hint to the garbage collector, and calling finalize() yourself is entirely incorrect.

like image 100
user207421 Avatar answered Oct 28 '22 10:10

user207421