5 Replies Latest reply: Feb 17, 2011 12:35 AM by EJP RSS

    Object Management in JNI

    838507
      Hi,

      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.
      e.g

      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.

      Edited by: user3516155 on Feb 15, 2011 2:21 AM
        • 1. Re: Object Management in JNI
          802316
          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.
          • 2. Re: Object Management in JNI
            jschellSomeoneStoleMyAlias
            user3516155 wrote:
            Hi,

            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.
            e.g

            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.
            Why don't you just use memcached?
            • 3. Re: Object Management in JNI
              EJP
              Or if you don't want to rely on Java code doing that, keep a static GlobalRef to it in your JNI.
              • 4. Re: Object Management in JNI
                838507
                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?
                • 5. Re: Object Management in JNI
                  EJP
                  Keep a GlobalRef to whatever class or object you are worrying about being garbage collected.