• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 521
  • Last Modified:

Java JNI memory leaks

I run from my java code C++ code with JNI engine. My question: If I have memory leaks in the C++ code and garbage collector cleans java object those run JNI engine in this case memory leaks of the C++ code leaves the memory or persists in the memory.
0
LMikl
Asked:
LMikl
1 Solution
 
shyamkumarreddyCommented:
LMikl
Yes you r right. JNI Engine can handle the Java Code but not the C++ code.
Where as u need to clear ur memory or handle memory leaves in C++.
What is your questiona abt?.
Thanks
Shyam
0
 
zingoCommented:
I am not quite sure I understand exactly what you are asking, but I'll try to explain a situation which might cause memory leaks when using JNI, perhaps it is what you are after.

If you allocate data in your C-code, for example by using NewStringUTF to allocate a jstring object, you have to delete the reference to the object when you are done with it, in order for the garbage collector to clean up the memory. You do this by calling

tmp_env->DeleteLocalRef(jstr);
0
 
shyamkumarreddyCommented:
Yeah Zingo is right.
If you need more info abt this. Pls go to this link and u will get good info abt it.

http://java.sun.com/docs/books/tutorial/native1.1/TOC.html#implementing

It will give u how to use the variables and how to release the memory in programming. And good tips. Which i used in while coding.

Shyam
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.

 
LMiklAuthor Commented:
I have a java class that it is calling to the C++ class by the JNI engine.
Is a memory which was allocated from the c++ class, holding by the VM?
0
 
shyamkumarreddyCommented:
LMikl
Good Question. Let me refer to VM Documentation and get back to you. It should be anyway will confirm it.

Shyam
0
 
zingoCommented:
It depends on how it was allocated. If it was allocated using the new operator, malloc function or other standard method it is not handled by the VM.

If it was allocated by a JNI method it is handled by the VM.
0
 
shyamkumarreddyCommented:
LMikl
Good Question. Let me refer to VM Documentation and get back to you. It should be anyway will confirm it.

Shyam
0
 
CSuvendraCommented:
Java has something called Conservative Garbage Collection. This is quoted from one Java technical article.

<QUOTE>

<B>Conservative Garbage Collection:</B>

If you use the Java Native Interface (JNI) API to make C calls, the garbage collector might see something in memory created by the C code that looks like a pointer, but is actually garbage. In this case, the memory is not garbage collected because the Java VM is conservative and does not reclaim memory that looks like it could be allocated to a pointer.

</QUOTE>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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