ShutdownHook没有移除导致内存泄漏

今天做kafka高并发测试的时候发生内存泄漏,直接原因是producer关闭之后内存没有得到释放。根本原因是:

  • 为了确保线程producer的connection断开,在线程里加了ShutdownHook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void addShutdownHook(final Closeable closeable, final Boolean isClosed) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (!isClosed.booleanValue()) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
  • 在producer close之后,由于ShutdownHook里保存着producer的句柄(closeable),导致producer的内存没有办法被JVM回收,线程多了之后,导致OOM

  • 解决办法是,在producer.close()之后,调用Runtime.getRuntime().removeShutdownHook(shutdownHook);