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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With