From 8427aa723a2068d9a04f92ae5c81073afe44613d Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 30 Jun 2014 10:39:37 +0200 Subject: [PATCH] [#2623] Release local references to guard against StackOverflow in JNI Motivation: When we do a (env*)->GetObjectArrayElement(...) call we may created many local references which will only be cleaned up once we exist the native method. Thus a lot of memory can be used and so a StackOverFlow may be triggered. Beside this the JNI specification only say that an implementation must cope with 16 local references. Modification: Call (env*)->ReleaseLocalRef(...) to release the resource once not needed anymore. Result: Less memory usage and guard against StackOverflow --- .../src/main/c/io_netty_channel_epoll_Native.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c index f50bcab23b..bc2f4b093e 100644 --- a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c +++ b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c @@ -692,6 +692,12 @@ JNIEXPORT jlong JNICALL Java_io_netty_channel_epoll_Native_writev(JNIEnv * env, iov[iovidx].iov_base = buffer + pos; iov[iovidx].iov_len = (size_t) (limit - pos); iovidx++; + + // Explicit delete local reference as otherwise the local references will only be released once the native method returns. + // Also there may be a lot of these and JNI specification only specify that 16 must be able to be created. + // + // See https://github.com/netty/netty/issues/2623 + (*env)->DeleteLocalRef(env, bufObj); } ssize_t res; @@ -728,6 +734,12 @@ JNIEXPORT jlong JNICALL Java_io_netty_channel_epoll_Native_writev(JNIEnv * env, incrementPosition(env, bufObj, len); written -= len; } + + // Explicit delete local reference as otherwise the local references will only be released once the native method returns. + // Also there may be a lot of these and JNI specification only specify that 16 must be able to be created. + // + // See https://github.com/netty/netty/issues/2623 + (*env)->DeleteLocalRef(env, bufObj); } return res; }