Solved

Java JNI memory leaks

Posted on 2002-04-30
8
500 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
countX 22 70
IT Company 5 69
maven project error 5 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now