Forum Stats

  • 3,840,393 Users
  • 2,262,599 Discussions
  • 7,901,261 Comments

Discussions

How do I manage static variables in C++ from JNI - memory implications

853881
853881 Member Posts: 2
edited Apr 8, 2011 8:28AM in Java Native Interface (JNI)
Hi,

My JNI implementation C++ file has a static variable (map) outside of any class.
I load it with an Initialize method that is called from my JNI implementation functions.
What happens to it after the function returns? Will it be cleaned up, or will it linger unattached soaking up memory?

eg:
// Map to associate the strings with the enum values
static std::map<std::string, StringValue> s_mapStringValues;

// Intialization
static void Initialize();

void Initialize()
{
	  s_mapStringValues["+Rl"] = evrotateleft;
	  s_mapStringValues["+Rr"] = evrotateright;
...
}


some-JNI-method( various JNI arguments ){
   ....
  Initialize();  // load the map
  // use the map
 ...
 return;  // return to Java land
}
Should I explicitly try to clear the map at the end of the JNI method? Will the map be preserved between JNI invocations?

Answers

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    The module is loaded through the JVM and becomes part of the JVM's runtime environment (for lack of a better description). So yes, the memory stays claimed until the Java process ends.

    Dumb question maybe, but what is the use of the static variable anyway? Why not just initialize the map inside your function? Then it is cleaned up as soon as the function ends.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    If it's static memory it is still there after the function returns. This is C 101, nothing to do with Java or JNI whatsoever.
  • 853881
    853881 Member Posts: 2
    But if the memory remains, will it be accessible on the next invocation, or will a completely new static instance be created?
    Its not a C question, its a JNI question - how does JNI attach to the static variables!
    Consider that a separate thread makes each invocation, not the same JNI thread!
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Apr 8, 2011 8:18AM
    But if the memory remains, will it be accessible on the next invocation
    That's what 'remains' means. Isn't it? I certainly don't know what else it could mean.
    or will a completely new static instance be created?
    No, that's contrary to what I said.
    Its not a C question, its a JNI question - how does JNI attach to the static variables!
    It doesn't. It loads the DLL when you execute System.load() or System.loadLibrary(). From there on the DLL exists in exactly the same way that any other loaded DLL exists in any other C or C++ application. Nothing to do with JNI.
    Consider that a separate thread makes each invocation, not the same JNI thread!
    Makes no difference whatsoever. Again this is C DLLs 101.
  • 800381
    800381 Member Posts: 486
    Actually, the way it's coded in the example provided, it'll reinitialize itself every time the JNI method is called.

    And if two Java threads make that call at the same time, the code provided will probably puke all over itself.
This discussion has been closed.