]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
JNI native memory leak - release array elements (#10981)
authorAlan Paxton <alan.paxton@gmail.com>
Wed, 14 Dec 2022 18:49:32 +0000 (10:49 -0800)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 14 Dec 2022 18:49:32 +0000 (10:49 -0800)
Summary:
Closes https://github.com/facebook/rocksdb/issues/10980

Reproduced as per the suggestion in the ticket, and `$ jcmd <PID> VM.native_memory | grep Internal` reports that we are no longer leaking internal memory with the suggested fix.

I did the repro in `MultiGetTest.java` which I have optimized imports on. It did not seem helpful to leave the test code around as it would be onerous to build a memory leak reproducer, and regression seems a remote possibility.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10981

Reviewed By: riversand963

Differential Revision: D41498748

Pulled By: ajkr

fbshipit-source-id: 8c6dd0d608172879c8bda479c7c9c05c12d34e70

java/rocksjni/rocksjni.cc
java/src/test/java/org/rocksdb/MultiGetTest.java

index 23fa604671371d174c1badd29c98898905325552..a1e0274b4ec163dadb7613b7a825fd555afc8378 100644 (file)
@@ -1836,6 +1836,10 @@ inline bool keys_from_bytebuffers(JNIEnv* env,
     jobject jkey = env->GetObjectArrayElement(jkeys, i);
     if (env->ExceptionCheck()) {
       // exception thrown: ArrayIndexOutOfBoundsException
+      // cleanup jkey_off and jkey_len
+      env->ReleaseIntArrayElements(jkey_lens, jkey_len, JNI_ABORT);
+      env->ReleaseIntArrayElements(jkey_offs, jkey_off, JNI_ABORT);
+
       return false;
     }
     char* key = reinterpret_cast<char*>(env->GetDirectBufferAddress(jkey));
@@ -1844,6 +1848,11 @@ inline bool keys_from_bytebuffers(JNIEnv* env,
 
     env->DeleteLocalRef(jkey);
   }
+
+  // cleanup jkey_off and jkey_len
+  env->ReleaseIntArrayElements(jkey_lens, jkey_len, JNI_ABORT);
+  env->ReleaseIntArrayElements(jkey_offs, jkey_off, JNI_ABORT);
+
   return true;
 }
 
index 323a6b1f405361447d47b568ee13abc6531f3b00..c391d81f631e0c6331663abdc6c1edcaa85149ff 100644 (file)
@@ -11,12 +11,17 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.rocksdb.util.TestUtil;
 
 public class MultiGetTest {
+  @ClassRule
+  public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
+      new RocksNativeLibraryResource();
+
   @Rule public TemporaryFolder dbFolder = new TemporaryFolder();
 
   @Test