NewObjectArray--java.lang.OutOfMemoryError: Java heap space

Hi,

I have been working on an application where I need to get data from a C++ object. Thus far I have had java call into the native code to extract it, but found that the design would be better to have C++ send the data to java when it deems appropriate. My problem is that now that I am having C++ call into java, the application eventually starts giving the following error: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space

Just before I start seeing this error the following code starts returning NULL, which only happens "if the system is out of memory" according to JNI specs.

array = env->NewObjectArray(max, stringCls, NULL);

if(array == NULL){

// error handling...

}

I don't understand the deep workings of java and JNI, but my only guess is that for some reason when I call from C++ into java, the garbage collector is unable to destroy all of the previous objectarrays. The changes I made are I now pass the array from JNI into java instead of returning it and I connect to the JVM and JNIEnv* as follows:

JNIEnv* env;

jvm->AttachCurrentThread(reinterpret_cast<void**>(&env), NULL);

Where jvm is a global that is set in a previously called function.

As far as I understand, this is the correct way to connect to the jvm.

If anyone has any ideas I would greatly appreciate it.

Mark

Message was edited by:

mebbert

null

[1602 byte] By [mebberta] at [2007-11-26 15:26:12]
# 1

You must have changed more than that to start getting a leak. Are you creating JNI global refs and not properly freeing them? Are you keeping new references up in your Java code that you weren't keeping before? Do you know what type of object is filling up the heap? If so then check through all your code for what references it. Otherwise the troubleshooting page at http://java.sun.com/javase/6/webnotes/trouble/ has lots of links to how to look at what's in your heap. jhat in particular can allow you to look at heap and find out who has references to particular objects. There are options to get a heap dump when an OutOfMemory occurs. That's supported in 1.6 and recent releases of 1.5.

tom

neverevera at 2007-7-8 21:41:54 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thanks for the tip Tom. I'll take a look at the heap.
mebberta at 2007-7-8 21:41:54 > top of Java-index,Java HotSpot Virtual Machine,Specifications...