[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1117
  • Last Modified:

How to release an array during JNI call

The below C-function program accepts a matrix from Java, and is supposed to do some mathematics on it.
Problems it, I have a major memory leak, and while I'm not used to JNI, I'm guessing the problem is I'm not releasing the memory, correct?

How would I do that?

If that's not likely the issue, there is more code, but I stripped it down to where I think the problem is :)
JNIEXPORT jfloatArray JNICALL Java_ec_app_gpuregression_CudaRegression_CudaCallC(JNIEnv * jenv, jclass jc, jobjectArray matrix) {
   for(int i = 0; i < 2; i++) {
      jfloatArray oneDim = static_cast<jfloatArray>(jenv->GetObjectArrayElement(multiProgs, i)); //Pick "next" array
      jfloat *multiProgs = jenv->GetFloatArrayElements(oneDim, 0);                                //Get out a pointer to the first element
      for(int j = 0; j < 32; j++)
         localArrayCopy[i][j] = matrix[j];
   }
   for(int i = 0; i < 2; i++)
      for(int j = 0; j < 32; j++)
         cout << localArrayCopy[i][j] << flush;
   return 0;
}

Open in new window

0
letharion
Asked:
letharion
  • 6
  • 4
1 Solution
 
alb66Commented:
I think must be:


 for(int i = 0; i < 2; i++) {
      jfloatArray oneDim = static_cast<jfloatArray>(jenv->GetObjectArrayElement(matrix, i)); //Pick "next" array
      jfloat *multiProgs = jenv->GetFloatArrayElements(oneDim, 0);                                //Get out a pointer to the first element
      for(int j = 0; j < 32; j++)
         localArrayCopy[i][j] = multiProgs [j];
      jEnv->ReleaseFloatArrayElements( oneDim, multiProgs );
   }
  

Open in new window

0
 
letharionAuthor Commented:
jenv->ReleaseFloatArrayElements(oneDim, multiProgs);
error: too few arguments in function call
0
 
letharionAuthor Commented:
I got the name of the variable wrong above.
JNIEXPORT jfloatArray JNICALL Java_ec_app_gpuregression_CudaRegression_CudaCallC(JNIEnv * jenv, jclass jc, jobjectArray multiProgs) {
   for(int i = 0; i < 2; i++) {
      jfloatArray oneDim = static_cast<jfloatArray>(jenv->GetObjectArrayElement(multiProgs, i)); //Pick "next" array
      jfloat *multiProgs = jenv->GetFloatArrayElements(oneDim, 0);                                //Get out a pointer to the first element
      for(int j = 0; j < 32; j++)
         localArrayCopy[i][j] = multiProgs[j];
 
      jenv->ReleaseFloatArrayElements(oneDim, multiProgs);
   }
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alb66Commented:
try:
jenv->ReleaseFloatArrayElements(oneDim, multiProgs,0);

!!!!warning!!!!
you have a local variable (jfloat *multiProgs) with the same name as one parameter (jobjectArray multiProgs)).
You should consider to change the parameter name to matrix (or sometingh else)
0
 
letharionAuthor Commented:
jenv->ReleaseFloatArrayElements(oneDim, (jfloat*)multiProgs, 0);
compiles...
I'm running it for a while to see what happens with memory
0
 
letharionAuthor Commented:
alb66:
My (jfloat*) wasn't necessary, your line compiles too.

Good point, I'm not sure what I was thinking about the variables, thanks.
Letting the program run for a while now, I'll get back to you :)
0
 
letharionAuthor Commented:
Works great. What do we do with the points? :)
0
 
alb66Commented:
>>> What do we do with the points?
What does this question mean? If I helped you, you should accept one of my comments as answer...  ;-)


0
 
alb66Commented:
Hey letharion,
do you need more help?
0
 
letharionAuthor Commented:
alb, no sorry, I don't need any more help ;)
I mixed up two different questions, in the other one I got answers from two different people and I meant "How should I divide the points", but that wasn't applicable here. :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now