Solved

Java JNI memory leaks

Posted on 2002-04-30
8
514 Views
Last Modified: 2013-11-23
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
Comment
Question by:LMikl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6979874
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
 

Expert Comment

by:zingo
ID: 6980031
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6980042
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:LMikl
ID: 6980119
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6980126
LMikl
Good Question. Let me refer to VM Documentation and get back to you. It should be anyway will confirm it.

Shyam
0
 

Expert Comment

by:zingo
ID: 6980132
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6980137
LMikl
Good Question. Let me refer to VM Documentation and get back to you. It should be anyway will confirm it.

Shyam
0
 
LVL 2

Accepted Solution

by:
CSuvendra earned 50 total points
ID: 6993856
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question