FIX: Non-Exiting Native Code Results in Memory Leak

ID: Q234329


The information in this article applies to:
  • Microsoft virtual machine


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 this to be a problem in the Microsoft products 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

The Microsoft VM build 3181, or later, is available for download at the Developer Updates for Microsoft SDK for Java 3.2.

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, please see the following pages on the Microsoft Technical Support site:

http://support.microsoft.com/support/visualj/

http://support.microsoft.com/support/java/

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Venkat Sunkara, Microsoft Corporation

Additional query words:

Keywords : kbJavaVM kbJNative kbGrpJava kbDSupport
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.