Solved

Java JNI memory leaks

Posted on 2002-04-30
8
510 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
Independent Software Vendors: 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!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 54
Error in @AspectJ Based AOP with Spring 2 24
eclipse buid path vs tomcat lib path 10 38
Facing this issue for maven proxy setting 2 27
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

730 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