How to free memory allocated by native method on Java side?

tilmik

I am trying to pass a byte array (with random data) from native to Java and I am not sure if this causes any memory leak or not. Here's the C++ code.

JNIEXPORT jbyteArray JNICALL Java_com_sample_test_jni_TestJNI_return_1byte_1array
(JNIEnv *env, jobject obj) {
  unsigned char *byteArray = new unsigned char[LENGTH];
  srand(12345);
  for(int i = 0;i < LENGTH;i++) {
    byteArray[i] = rand() % 64;
  }
  jbyteArray data = (env)->NewByteArray(LENGTH);
  env->SetByteArrayRegion(data, 0, LENGTH, (jbyte*)byteArray);
  delete(byteArray);
  return data;
}

And here's the Java code.

class TestJNI {
  static {
    System.loadLibrary("foobar");
  }
  public native byte[] return_byte_array();
  public static void main(String[] args) {
    byte[] data = new TestJNI().return_byte_array();
    System.out.println("Data length " + data.length);
  }
}

My doubt is whether the jbytearray allocated in native code will be garbage-collected by Java. I cannot free it in native side.

Also, are there good docs which describe JNI memory management with examples?

Buddy

The Java GC should clean up any objects you allocate.

See this answer for more details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to free allocated memory in C++ DLL

From Dev

How to free memory allocated to local static pointer

From Dev

How to free the memory allocated by Gdiplus::Bitmap::FromFile

From Dev

How to free the memory allocated by cdev_alloc?

From Dev

How to free the memory allocated by cdev_alloc?

From Dev

How to 'free' memory allocated to a variable in shell

From Dev

free with dynamically allocated memory

From Dev

Free allocated memory in C

From Dev

Releasing Memory Allocated by Native Libraries in Java

From Dev

Where do these java native memory allocated from?

From Dev

how to free memory in C that was allocated using C++ "new" operator

From Dev

How to free allocated memory in a recursive function in C++

From Dev

How free up memory allocated by lua_newuserdata with delete operator?

From Dev

How to free memory in C# that is allocated in C++

From Dev

how to free dynamic memory allocated inside function after finishing execution

From Dev

Is it safe to free() memory allocated by new?

From Dev

Is it safe to free() memory allocated by new?

From Dev

free dynamically allocated memory in QT

From Dev

Is metaspace allocated out of native memory?

From Java

Is it ever OK to *not* use free() on allocated memory?

From Dev

What is the scope of free() in dynamically allocated memory?

From Dev

Where to free allocated memory by jni NewByteArray

From Dev

Freeing allocated memory: realloc() vs. free()

From Dev

Free all the memory allocated by malloc(), realloc() in C

From Dev

cudaFree and cudaFreeHost fails to free heap allocated memory

From Dev

free allocated memory of array (vs normal var?)

From Dev

Freeing allocated memory: realloc() vs. free()

From Dev

What is the scope of free() in dynamically allocated memory?

From Dev

cudaFree and cudaFreeHost fails to free heap allocated memory

Related Related

  1. 1

    How to free allocated memory in C++ DLL

  2. 2

    How to free memory allocated to local static pointer

  3. 3

    How to free the memory allocated by Gdiplus::Bitmap::FromFile

  4. 4

    How to free the memory allocated by cdev_alloc?

  5. 5

    How to free the memory allocated by cdev_alloc?

  6. 6

    How to 'free' memory allocated to a variable in shell

  7. 7

    free with dynamically allocated memory

  8. 8

    Free allocated memory in C

  9. 9

    Releasing Memory Allocated by Native Libraries in Java

  10. 10

    Where do these java native memory allocated from?

  11. 11

    how to free memory in C that was allocated using C++ "new" operator

  12. 12

    How to free allocated memory in a recursive function in C++

  13. 13

    How free up memory allocated by lua_newuserdata with delete operator?

  14. 14

    How to free memory in C# that is allocated in C++

  15. 15

    how to free dynamic memory allocated inside function after finishing execution

  16. 16

    Is it safe to free() memory allocated by new?

  17. 17

    Is it safe to free() memory allocated by new?

  18. 18

    free dynamically allocated memory in QT

  19. 19

    Is metaspace allocated out of native memory?

  20. 20

    Is it ever OK to *not* use free() on allocated memory?

  21. 21

    What is the scope of free() in dynamically allocated memory?

  22. 22

    Where to free allocated memory by jni NewByteArray

  23. 23

    Freeing allocated memory: realloc() vs. free()

  24. 24

    Free all the memory allocated by malloc(), realloc() in C

  25. 25

    cudaFree and cudaFreeHost fails to free heap allocated memory

  26. 26

    free allocated memory of array (vs normal var?)

  27. 27

    Freeing allocated memory: realloc() vs. free()

  28. 28

    What is the scope of free() in dynamically allocated memory?

  29. 29

    cudaFree and cudaFreeHost fails to free heap allocated memory

HotTag

Archive