We're using the async driver in a webapp deployed to Tomcat (v8, along with Java 8). After Tomcat is shut down, its log file (catalina.out) has several entries that read something like this:
The web application 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.
The culprit is com.mongodb.connection.netty.NettyStreamFactoryFactory. It creates an instance of NioEventLoopGroup but never shuts it down.
We're using Spring and our solution was to write our own StreamFactoryFactory that is identical to NettyStreamFactoryFactory except in two ways. It's annotated with @Component (so its lifecycle is managed by Spring) and it has the following method:
@PreDestroy
public void shutdown()
{
eventLoopGroup.shutdownGracefully().awaitUniterruptibly();
}
For those not using Spring I recommend creating doing something like this...
EventLoopGroup myEventLoopGroup = new NioEventLoopGroup(); StreamFactoryFactory myStreamFactoryFactory = new NettyStreamFactoryFactory(myEventLoopGroup, ByteBufAllocator.DEFAULT);
...and shut down myEventLoopGroup yourself at the appropriate time.
The async driver should properly manage the EventLoopGroup it creates...we had to kill Tomcat manually until we figured out the simple fix.