Netty 在 tomcat 中导致内存泄漏
Netty causing memory leak in tomcat
我正在使用 Netty 4.0。26.Final 和 Tomcat 8 在 Web 应用程序中实现嵌入式 TCP 服务器。
问题是当我停止时 Tomcat 我收到这条消息:
The web application [app] appears to have started a thread named
[nioEventLoopGroup-2-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
sun.nio.ch.WindowsSelectorImpl$SubSelector.access0(WindowsSelectorImpl.java:278)
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310)
io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:111)
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
我试过使用这个 class 在关闭之前关闭线程 tomcat 但我仍然遇到同样的问题。
@Component
public class MyAppServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent arg0) {
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
TrackingDaemon trackingDaemon= (TrackingDaemon) ApplicationContextProvider.getBean("trackingDaemon");
trackingDaemon.bossGroup.shutdownGracefully();
trackingDaemon.workerGroup.shutdownGracefully();
}
}
BossGroup 和 workerGroup 是:
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
任何帮助将不胜感激..
我猜你需要做的是:
trackingDaemon.bossGroup.shutdownGracefully().syncUninterruptibly();
trackingDaemon.workerGroup.shutdownGracefully().syncUninterruptibly();
我在 netty 4 和 tomcat 7 中遇到过这个问题,并通过从 shutdownGracefully() 捕获未来事件并等待其完成来解决它。
关闭应用程序时调用以下方法。
private void stopChannel() {
log.trace("Stopping server socket on port '{}'", port);
sendMessageService.deregisterMessageSender(endpointId);
try {
for (Channel channel : channels) {
if (channel != null) {
channel.close();
}
}
serverChannel.close().sync();
log.trace("Server socket on port '{}' stopped.", port);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
Future<?> fc = connectionGroup.shutdownGracefully();
Future<?> fw = workerGroup.shutdownGracefully();
try {
fc.await(); // when shutting down in tomcat waits for the netty threads to die
fw.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.trace("Server workerGroup has shutdown successfully on port '{}'", port);
}
}
我正在使用 Netty 4.0。26.Final 和 Tomcat 8 在 Web 应用程序中实现嵌入式 TCP 服务器。
问题是当我停止时 Tomcat 我收到这条消息:
The web application [app] appears to have started a thread named
[nioEventLoopGroup-2-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(WindowsSelectorImpl.java:296)
sun.nio.ch.WindowsSelectorImpl$SubSelector.access0(WindowsSelectorImpl.java:278)
sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:159)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:622)
io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:310)
io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:111)
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
我试过使用这个 class 在关闭之前关闭线程 tomcat 但我仍然遇到同样的问题。
@Component
public class MyAppServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent arg0) {
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
TrackingDaemon trackingDaemon= (TrackingDaemon) ApplicationContextProvider.getBean("trackingDaemon");
trackingDaemon.bossGroup.shutdownGracefully();
trackingDaemon.workerGroup.shutdownGracefully();
}
}
BossGroup 和 workerGroup 是:
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
任何帮助将不胜感激..
我猜你需要做的是:
trackingDaemon.bossGroup.shutdownGracefully().syncUninterruptibly();
trackingDaemon.workerGroup.shutdownGracefully().syncUninterruptibly();
我在 netty 4 和 tomcat 7 中遇到过这个问题,并通过从 shutdownGracefully() 捕获未来事件并等待其完成来解决它。 关闭应用程序时调用以下方法。
private void stopChannel() {
log.trace("Stopping server socket on port '{}'", port);
sendMessageService.deregisterMessageSender(endpointId);
try {
for (Channel channel : channels) {
if (channel != null) {
channel.close();
}
}
serverChannel.close().sync();
log.trace("Server socket on port '{}' stopped.", port);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
Future<?> fc = connectionGroup.shutdownGracefully();
Future<?> fw = workerGroup.shutdownGracefully();
try {
fc.await(); // when shutting down in tomcat waits for the netty threads to die
fw.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.trace("Server workerGroup has shutdown successfully on port '{}'", port);
}
}