diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 2806bd1..5f6f818 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -23,6 +23,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URI; import java.net.URLClassLoader; import java.util.*; @@ -1262,6 +1264,21 @@ public void close() throws IOException { } dropSessionPaths(conf); + + // HIVE-11408, HADOOP-10513 + // Hadoop's ReflectionUtils caches constructors for the classes it instantiated. + // In UDFs, this can result in classloaders not getting GCed for a temporary function, + // resulting in a PermGen leak when used extensively from HiveServer2 + Method clearCacheMethod; + try { + clearCacheMethod = ReflectionUtils.class.getDeclaredMethod("clearCache"); + if (clearCacheMethod != null) { + clearCacheMethod.setAccessible(true) ; + clearCacheMethod.invoke(null) ; + } + } catch (Exception e) { + LOG.info(e); + } } public AuthorizationMode getAuthorizationMode(){