Now blogging at diego's weblog. See you over there!

reference handling in JNI

As I was working on memory optimizations for spaces I was also trying to fix a memory leak that seemed to be coming from the DLL used to import data from Outlook. Of course, this would only affect the Outlook import process, but it was nevertheless an important part. So I started looking at the JNI code.

The Outlook DLL import reads Outlook information using MAPI (actually wrapping the MAPI objects with ATL) and creates string pairs (field/value) that are then passed to Java. The C++ call looks like this:

Where fieldValue is a char pointer. Now, the JNI documentation doesn't say anything at all of having to release strings or data created with this particular method, but they have to be. The way to do it is by calling
const char* reschr2 = jniEnv->GetStringUTFChars(fieldName, JNI_FALSE);
jniEnv->ReleaseStringUTFChars(fieldName, reschr2);
Now, once we've seen this code it sounds straightforward, but the JNI documentation does not specify that calling the ReleaseStringUTFChars and DeleteLocalRef methods is necessary, or, in fact, required to release the object allocated. Online discussions in various places are also silent on this. The JNI tutorial doesn't mention it when talking about how to create strings from the C/C++ side. Which raises the unsettling thought that there are thousands of JNI apps out there that almost certainly have memory leaks of this type in them, because unless you're creating tens of thousands of objects, it becomes difficult to see that the memory leak is actually there.

Anyway, the bug is now fixed and I just did an import from Outlook into spaces of 10,000 items (emails, calendar items, contacts, etc.) with the default memory settings for the JVM (64 MB max heap), and it worked fine, finishing in 20 minutes. According to different tests I've done that speed is linear, so about 500 items imported per minute. At some point I should create a bigger Outlook database to see how far it'll go.

Categories: clevercactus,
Posted by diego on March 22 2003 at 6:47 PM

Copyright © Diego Doval 2002-2011.
Powered by
Movable Type 4.37