javaw memory usage grows by the usage of native method

Hello!

I have a very simple jni - method, which puts a string into the java String field:

JNIEXPORTvoid JNICALL Java_de_getStr(JNIEnv *env, jobject obj){

jclassclazz = (*env)->GetObjectClass(env, obj);

char myStr[5] ="aaaa";

jobject aString = (*env)->NewStringUTF(env, myStr);

jfieldID jfid = (*env)->GetFieldID(env, clazz,"drg","Ljava/lang/String;");

(*env)->SetObjectField(env, obj, jfid, aString);

(*env)->ReleaseStringUTFChars(env, aString, myStr);

(*env)->DeleteLocalRef(env, aString);

}

In java I have the following class:

publicclass MyClass

{

String drg;

privatenativevoid getStr();

public String getDRG()

{

getStr();

return drg;

}

}

When the getDRG() - Method will be called in a loop of ca.1000000 times you can see that the memory usage of the javaw grows.

Do you have any idea? I have to process a very large ammount of such strings and it finally comes to the "C:/BUILD_AREA/jdk1.5.0_03/hotspot\src\share\vm\utilities\growableArray.cpp.

Out of swap space?" - error

Thank you for any help

[1715 byte] By [RGAnna] at [2007-11-27 9:15:12]
# 1
You forget to delete the class reference:(*env)->DeleteLocalRef(env, clazz);
vitallisa at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thank you, I tried it immediately. It didn't change anything.I'm getting desparate!!!
RGAnna at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Remove (*env)->ReleaseStringUTFChars(env, aString, myStr);because nothing to release.
vitallisa at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Of cource I have to release the aString object! Anyway, when I remove this command, the javaw grows much faster
RGAnna at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

I repeated your code with JSE 1.5.0_07, OS - MS Windows 2003, MS VC++ 6.0 and no memory growth.

Java Code:

public class MyClass {

static {

System.loadLibrary("JavaMemory");

}

String drg;

private native void getStr();

public String getDRG()

{

getStr();

return drg;

}

public static void main(String[] args) {

MyClass mc = new MyClass();

for(int i = 0; i < 1000000; i++)

mc.getDRG();

}

}

C++ Code:

MyClass.h

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class MyClass */

#ifndef _Included_MyClass

#define _Included_MyClass

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:MyClass

* Method:getStr

* Signature: ()V

*/

JNIEXPORT void JNICALL Java_MyClass_getStr

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

StdAfx.h

// stdafx.h : include file for standard system include files,

// or project specific include files that are used frequently, but

//are changed infrequently

//

#if !defined(AFX_STDAFX_H__33F99590_F1FD_4C7C_B29F_61A308202C74__INCLUDED_)

#define AFX_STDAFX_H__33F99590_F1FD_4C7C_B29F_61A308202C74__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// Insert your headers here

#define WIN32_LEAN_AND_MEAN// Exclude rarely-used stuff from Windows headers

#include <windows.h>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__33F99590_F1FD_4C7C_B29F_61A308202C74__INCLUDED_)

JavaMemory.cpp

#include "stdafx.h"

#include "MyClass.h"

BOOL APIENTRY DllMain( HANDLE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

return TRUE;

}

JNIEXPORT void JNICALL Java_MyClass_getStr(JNIEnv *env, jobject obj){

jclass clazz = env->GetObjectClass(obj);

char myStr[5] = "aaaa";

jobject aString = env->NewStringUTF(myStr);

jfieldID jfid = env->GetFieldID(clazz, "drg", "Ljava/lang/String;");

env->SetObjectField(obj, jfid, aString);

env->DeleteLocalRef(aString);

env->DeleteLocalRef(clazz);

}

vitallisa at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Thank you!!!I have WindowsXP Professional SP2, library is built with mingw32-make (gcc compiler), java version 1.5.0_03-b07Thus, meanwhile I'm starting to believe that the error has to be somewhere else..........
RGAnna at 2007-7-12 22:04:04 > top of Java-index,Java HotSpot Virtual Machine,Specifications...