FIX: Non-Exiting Native Code Results in Memory Leak (234329)



The information in this article applies to:

  • Microsoft 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.

MORE INFORMATION

Steps to Reproduce Behavior

Create a file Test.cpp with the following code:
#include "jni.h"
#include <stdlib.h>

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.

REFERENCES

To download the latest build of the Microsoft virtual machine, visit the following Microsoft Web site: For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbbug kbfix kbJNative KB234329