Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.9K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.4K Development
- 17 Developer Projects
- 139 Programming Languages
- 293.1K Development Tools
- 111 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 161 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 475 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
How do I manage static variables in C++ from JNI - memory implications

853881
Member Posts: 2
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:
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
-
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. -
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.
-
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! -
But if the memory remains, will it be accessible on the next invocationThat'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.
-
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.