Netcat导致tomcat内存泄漏

时间:2015-09-21 13:34:47

标签: java tomcat netty

我使用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.access$400(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$2.run(SingleThreadEventExecutor.java:111)
 io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)

我在关闭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();

任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:1)

我遇到了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);
}

}

答案 1 :(得分:0)

我想你需要这样做:

    trackingDaemon.bossGroup.shutdownGracefully().syncUninterruptibly();
    trackingDaemon.workerGroup.shutdownGracefully().syncUninterruptibly();