JNI Memory Problem
843829Sep 4 2003 — edited Sep 15 2003Hi:
Problem: Java memory usage seems to gradually increase when using JNI.
Description:
I am using Visual Studio C++ and Java 1.4 on WinXP.
The JVM gets invoked in C++. There is a C++ thread that
listens to events coming from a java server.
This is the source code of the thread.
-------
while ( true )
{
jobject event;
event = (jobject)env->CallObjectMethod( javaobject, eventCallback_id );
if ( event != NULL ) {
if ( env->IsInstanceOf( event, AttributeSetEvent ) ) {
cout << "Event received" << endl;
// do somethind with event
} else {
cout << "Unknown event" << endl;
}
env->DeleteLocalRef( event );
event = NULL;
}
}
----------
The env environment is properly attached to JVM.
The method CallObjectMethod issues a call to eventCallback function which return an event whenevet it occurs.
It seems that when I issue env->DeleteLocalRef( event ), the garbage collector does not clean up the event.
I check the JVM memory usage and it constantly increases.
The JVM starts up with around 2MB of memory and then after several thousand events it jumps up to 11 MB.
When I remove CallObjectMethod, JVM memory is fine.
My only explanation is that the garbage collector does not clean up events after I call DeleteLocalRef( event ).
I would appreciate any suggestions regarding this problem.
Thank you,
-Ivan