Memory Leak while using Get(Release)PrimitiveArrayCritical
843829Jan 11 2002 — edited Jan 13 2004From http://java.sun.com/products/jdk/1.2/docs/guide/jni/jni-12.html#arrayops I understand that "After calling GetPrimitiveArrayCritical, the native code should not run for an extended period of time before it calls ReleasePrimitiveArrayCritical. "
I have java application which passes an array to C++(JNI) and uses GetPrimitiveArrayCritical and ReleasePrimitiveArrayCritical to get and release this array. I am using the JDK1.3.1 on Solaris 8. Here I have to have my code run for a while in between Get(Release)PrimitiveArrayCritical calls.
Here's the JNI code (in C++). When I am doing load test with code below (approximately 7000 calls to the native method) I am getting OutOfMemory Error in the Java Application.
JNIEXPORT jint JNICALL Java_com_xyz_MyClass_compare
(JNIEnv * env, jclass, jint size, jfloatArray jWeights)
{
jint status = 0;
my_float * weights = (my_float *) env->GetPrimitiveArrayCritical(jWeights,0);
for (int j=0;j<1000000;j++) { }
env->ReleasePrimitiveArrayCritical(jWeights, weights, JNI_ABORT);
return status;
}
----------------
However if I comment the for loop (as shown below) I am NOT getting the OutOfMemory Error.
JNIEXPORT jint JNICALL Java_com_xyz_MyClass_compare
(JNIEnv * env, jclass, jint size, jfloatArray jWeights)
{
jint status = 0;
my_float * weights = (my_float *) env->GetPrimitiveArrayCritical(jWeights,0);
/* for (int j=0;j<1000000;j++){ } */
env->ReleasePrimitiveArrayCritical(jWeights, weights, JNI_ABORT);
return status;
}
----------------
Also When I comment just the statements containing GetPrimitiveArrayCritical and ReleasePrimitiveArrayCritical (shown below) I am NOT getting OutOfMemory error.
JNIEXPORT jint JNICALL Java_com_xyz_MyClass_compare
(JNIEnv * env, jclass, jint size, jfloatArray jWeights)
{
jint status = 0;
//my_float * weights = (my_float *) env->GetPrimitiveArrayCritical(jWeights,0);
for (int j=0;j<1000000;j++) { }
//env->ReleasePrimitiveArrayCritical(jWeights, weights, JNI_ABORT);
return status;
}
-----------------
I think that I might be voilating the restrictions imposed by GetPrimitiveArrayCritical/ReleasePrimitiveArrayCritical methods and hence I am getting the OutOfMemory Error. Am I right in thinking so ?
If so, do you think if there is any alternate way of solving above problem. I had to do some time consuming job between GetPrimitiveArrayCritical and ReleasePrimitiveArrayCritical calls.
Appreciate your reply