Microsoft KB Archive/234329

= FIX: Non-Exiting Native Code Results in Memory Leak =

Article ID: 234329

Article Last Modified on 6/14/2006

-

APPLIES TO


 * Microsoft Java Virtual Machine

-



This article was previously published under Q234329



SYMPTOMS
Native code that uses Java Native Interface (JNI) methods such as NewStringUTF to allocate memory may not free that memory after a call to DeleteLocalRef until the program exits.



CAUSE
Although DeleteLocalRef releases the memory used to store the object, that memory is never reused for new objects.



RESOLUTION
Install version 3181, or later, of the Microsoft virtual machine for Java, where this problem has been fixed.

Please refer to the "References" section in this article for more information on how to obtain the latest Microsoft VM.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior
Create a file Test.cpp with the following code:
 * 1) include "jni.h"
 * 2) include 

static JNIEnv* env; static JavaVM* jvm;

void main(int argc,char *argv[]) {

MS_JDK1_1InitArgs vm_args; vm_args.nVersion= 0x00010001; JNI_GetDefaultJavaVMInitArgs(&vm_args);

if(JNI_CreateJavaVM(&jvm, &env, &vm_args) < 0) { printf("Could not create JVM\n"); exit(1); }

// Infinite loop. Program never exits. while(true) { jstring jstr = env->NewStringUTF("Once upon a time..."); env->DeleteLocalRef(jstr); }   } Compile and link using a command similar to this: cl -GX test.cpp -Ic:\Program Files\Microsoft SDK for Java 3.2\include c:\Program Files\Microsoft SDK for Java 3.2\lib\i386\msjava.lib Now run the Test.exe file and observe the memory usage in Task Manager in Windows NT or in the System Monitor in Windows 95 and 98.

