timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
163 stars 44 forks source link

Fix JNI local refs warning by deleting local ref to returned object #47

Closed emetsds closed 3 years ago

emetsds commented 3 years ago

I get this warning all the time WARNING: JNI local refs: 363, exceeds capacity: 362 when trying to use methods that return an object It seems that local references to the returned values are not deleted. This small fix avoids the warning, but I'm not sure it's completely correct. Please take a look at this

timob commented 3 years ago

I think the references are being handled correctly in CallMethod, you could try using https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#EnsureLocalCapacity and/or make sure you are deleting references. There are methods in JNIGI to do this.

emetsds commented 3 years ago

Sorry for my english, it's a bit hard for me to explain, but I can provide simple test that produces the memory leak. And this can be fixed by my pull request. This test consist of java method that returns empty byte array and go method that infinitely calls this method. If you call this test, it will crash in about 10 seconds. Because CallMethod returns me a go array, I can`t delete local reference manually localRefLeak.zip

timob commented 3 years ago

Ahh ok thanks yes the reference to the array is leaked. Thanks 👍 I will look at the PR tomorrow.

timob commented 3 years ago

🎉 Great! thanks for finding that, explaining the problem and fixing.