Solved

Java JNI memory leaks

Posted on 2002-04-30
8
503 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

778 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