[kaffe] JNI Memory leak

Guilhem Lavaux guilhem at kaffe.org
Wed Mar 15 13:32:23 PST 2006

Hi Fernando,

ExceptionOccured() returns a reference to the exception object. The 
object is pushed on the local reference table (as do some other VM, 
which is logical as it is a reference). You should use ExceptionCheck 
instead: it returns a boolean which is true if an exception occured and 
no reference is added in the table. I know that Sun's JVM has a pretty 
large local reference table but it is referenced in the official 
documentation that the table by default should have a size of 16. The 
table can be dynamically growed using EnsureLocalCapacity.



Fernando Silveira wrote:
> Hi,
> I'm developing a JNI library to work with Kaffe and I'm having a kind of
> memory leak issue.
> Sometimes my lib needs to instantiate Java objects and call some
> methods. If the objects are created and the methods are called with
> success (without exceptions or errors) everything works fine. Sometimes
> the JNI library needs to try to call an inexistant method and if the
> method does not exist, it must not return to the JVM and must retry to
> call other methods.
> The issue occurs when it calls many times the GetMethodID() function and
> it returns exceptions: Kaffe shows the following error:
> FATAL ERROR: No more room for local references
> I know this happens when I don't DeleteLocalRef() everything I have
> created, but this situation seems to be different. The following code
> proves what I'm trying to say:
> jmethodID
> test_GetMethodID(JNIEnv *jenv, jclass jcls, const char *name,
>     const char *sig)
> {
> 	jmethodID jmid;
> 	/* Force java.lang.NoSuchMethodError error. */
> 	name = "NoSuchMethod";
> 	do {
> 		jmid = (*jenv)->GetMethodID(jenv, jcls, name, sig);
> 		if ((*jenv)->ExceptionOccurred(jenv)) {
> 			(*jenv)->ExceptionDescribe(jenv);
> 			(*jenv)->ExceptionClear(jenv);
> 		}
> 	} while (jmid == 0);
> 	return jmid;
> }
> This function causes many exceptions and a final "FATAL ERROR: No more
> room for local references" message.
> I'm not sure but I think SUN JVM and SableVM also behaves this way,
> because I can see the JVM memory growing up slowly when I call this
> function but they don't limit the "reference room" size.
> I tried Google and all the JNI documentation I have but I could not find
> anything related to this. Am I missing something or doing something
> wrong? Can't I call a lot of GetMethodID() with invalid method names? I
> hope you can give me a hint or solution for my problem.
> Thanks a lot.

More information about the kaffe mailing list