This content has been marked as final. Show 5 replies
Any object is retained as long a Java thread can get a strong reference to it.
Make sure there is at least one Java thread has a reference to the Map, or some else which does and it cannot be collected.
user3516155 wrote:Why don't you just use memcached?
I need to build one Java standalone application which will act as a server for c++. I mean C++ will make a JNI call to make a use of JAVA methods. Here C++ application is continuous running which will make a JNI call on need basis.
on the JAVA side there is static hashmap with addKey() method. so from C++ we need to call addKey() method using JNI, which will add keys in the existing HashMap. C++ application might call addKey() method continuously or say after 6hrs. So how to make sure that same HashMap will be use and all stored values will be remain in the HashMap which I have added?? How to make sure that it will not be garbage collected? OR any other solution for the same? here we don't want to use file system to store the values.
Or if you don't want to rely on Java code doing that, keep a static GlobalRef to it in your JNI.
You mean to same I need to keep the static global reference the class itself whose method we will going to call from c++ using JNI? What I am planning to do it that I will create one singleton class which will make the HashMap instance and JNI methods which will get called from c++ will instantiate the same indirectly by calling related methods which will going to add kay, values in the saingleton hash map reference. Is this fine?
Keep a GlobalRef to whatever class or object you are worrying about being garbage collected.