3 Replies Latest reply: Mar 28, 2013 8:02 PM by jschellSomeoneStoleMyAlias RSS

    Memory management while implementing JNI interfaces.

    999713
      The assumption here is that java code calls a C function from a
      shared library in Linux.

      How should memory management be done in the native code called
      from Java using a JNI interface?

      What role does the Garbage Collector play in handling memory that
      was allocated in the native C code?

      If I allocate memory using malloc() in the C program, should I
      explicitly free it? Will the GC take care of it?

      Does anyone have an idea of how memory management be done while
      implementing a JNI interface?

      Thanks,
      Nisahnt
        • 1. Re: Memory management while implementing JNI interfaces.
          EJP
          Moderator action: moved this where it belongs, and deleted two duplicates.
          • 2. Re: Memory management while implementing JNI interfaces.
            rukbat
            ... your 4th cross-post of this same inquiry
            https://forums.oracle.com/forums/thread.jspa?threadID=2518066
            is now locked.

            Edit:
            Upon reconsideration, you don't need a JNI question sitting in the Oracle Linux forum.
            That 4th post has now been deleted.

            Edited by: rukbat on Mar 28, 2013 8:08 AM
            • 3. Re: Memory management while implementing JNI interfaces.
              jschellSomeoneStoleMyAlias
              NishantMungse wrote:
              The assumption here is that java code calls a C function from a
              shared library in Linux.

              How should memory management be done in the native code called
              from Java using a JNI interface?
              C: Alloc something giving a pointer
              C: Return pointer to Java as a long.
              C: As needed methods take a long parameter and cast to correct pointer.
              C: Provide a destroy() method that takes a long, casts to correct pointer and deallocates correctly.

              Java: class keeps long
              Java: As needed JNI methods are passed long
              Java: Provide a destroy() method which passes long to C destroy() method if long is not zero. After call set long to zero.
              Java: Optional: Add a finalizer. It calls destroy()

              Last step is optional if your programming environment is strict AND the usage of the class has a restricted scope.

              The above assumes that you are going to use the memory in a 'normal' way. For instance there allocations are relatively small, and exist for short amounts of time. If that isn't true then you might need to tune the usage in much the same way that you might if you had a Java class that consumed a large amount of memory.
              What role does the Garbage Collector play in handling memory that
              was allocated in the native C code?
              It doesn't.

              At some point it can interfere with the heap though.
              If I allocate memory using malloc() in the C program, should I
              explicitly free it? Will the GC take care of it?
              You must explicitly free it.
              Does anyone have an idea of how memory management be done while
              implementing a JNI interface?
              See above.