Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 9d45c6a

Browse files
committed
Fixes ContextExt referencing itself and not deallocating
Also alleviates NIO direct buffers deallocation slowness Signed-off-by: ncordon <[email protected]>
1 parent 7ab0adf commit 9d45c6a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class ContextExt {
144144
delete (ctx);
145145

146146
if (jCtxExt)
147-
getJNIEnv()->DeleteGlobalRef(jCtxExt);
147+
getJNIEnv()->DeleteWeakGlobalRef(jCtxExt);
148148
}
149149

150150
// lookup searches for a specific node handle.
@@ -159,7 +159,7 @@ class ContextExt {
159159
// We need this because a NodeExt from Scala side includes
160160
// a Scala ContextExt and a handle to the native C node
161161
void setJavaContext(jobject ctx) {
162-
jCtxExt = getJNIEnv()->NewGlobalRef(ctx);
162+
jCtxExt = getJNIEnv()->NewWeakGlobalRef(ctx);
163163
}
164164

165165
// Iterate returns iterator over an external UAST tree.

src/main/scala/org/bblfsh/client/v2/BblfshClient.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,16 @@ object BblfshClient {
149149
def decode(): ContextExt = {
150150
val bufDirectCopy = ByteBuffer.allocateDirect(buf.size)
151151
buf.copyTo(bufDirectCopy)
152-
BblfshClient.decode(bufDirectCopy)
152+
val result = BblfshClient.decode(bufDirectCopy)
153+
// Sometimes the direct buffer can take a lot to deallocate,
154+
// causing Out of Memory, because it is not allocated in
155+
// in the JVM heap and will only be deallocated them when
156+
// JVM does not have more space in its heap
157+
// This line alleviates the problem
158+
// Problem can be tested if we remove it and do a
159+
// loop decoding a file
160+
System.gc()
161+
result
153162
}
154163
}
155164

0 commit comments

Comments
 (0)