Tomcat的NIO 连接器中oomParachute的作用
NIO oomParachute的作用
在Server启动的时候,启动NIO连接器,此时判断oomParachute的配置是否大于0,由于默认为 1024 * 1024, 因此在执行NioEndpoint.bind时, bind() { ... if (oomParachute>0) reclaimParachute(true); ... }
protected boolean reclaimParachute(boolean force) {
if ( oomParachuteData != null ) return true;
if ( oomParachute > 0 && ( force || (Runtime.getRuntime().freeMemory() > (oomParachute*2))) )
oomParachuteData = new byte;
return oomParachuteData != null;
}
上面oomParachute > 0 并且 force传入值为true,因此,此处会创建 oomParachuteData,值为1M的byte数组
注意:是每个connector都会创建一个,
java.lang.OutOfMemoryError: Java heap space at com.tongweb.web.webutil.util.buf.ByteChunk.allocate(ByteChunk.java:168) at com.tongweb.web.webutil.util.buf.ByteChunk.(ByteChunk.java:130) at com.tongweb.web.oro.http11.filters.BufferedInputFilter.(BufferedInputFilter.java:43 ) at com.tongweb.web.oro.http11.AbstractHttp11Processor.initializeFilters(AbstractHttp11Proces sor.java:698) at com.tongweb.web.oro.http11.Http11NioProcessor.(Http11NioProcessor.java:76) at com.tongweb.web.oro.http11.ThorHttp11NioProcessor.(ThorHttp11NioProcessor.java:14) at com.tongweb.web.oro.http11.ThorHttp11NioProtocol$ThorHttp11ConnectionHandler.createProces sor(ThorHttp11NioProtocol.java:27) at com.tongweb.web.oro.http11.ThorHttp11NioProtocol$ThorHttp11ConnectionHandler.createProces sor(ThorHttp11NioProtocol.java:17) at com.tongweb.web.oro.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.j ava:569) at com.tongweb.web.webutil.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1653) at com.tongweb.web.webutil.util.threads.TWThreadPoolExecutor.runWorker(TWThreadPoolExecutor. java:1165) at com.tongweb.web.webutil.util.threads.TWThreadPoolExecutor$Worker.run(TWThreadPoolExecutor .java:622) at java.lang.Thread.run(Thread.java:662)
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
目前可以模拟相应的场景,在OOM产生时,该内存块会被置为null进行释放,此时请求不能再处理,但应用服务器并没有马上挂掉,后台仍然在不停的打印SEVERE:Memory usage is low, parachute is non existent, your system may start failing.
catch (OutOfMemoryError oom) { try { oomParachuteData = null; releaseCaches(); log.error("", oom); }catch ( Throwable oomt ) { try { System.err.println(oomParachuteMsg); oomt.printStackTrace(); }catch (Throwable letsHopeWeDontGetHere){ ExceptionUtils.handleThrowable(letsHopeWeDontGetHere); } } }
protected void releaseCaches() {
this.keyCache.clear();
this.nioChannels.clear();
this.processorCache.clear();
if ( handler != null ) handler.recycle();
}
模拟该问题使用NIO通道产生OOM从而使用该参数生效。每个通道在启动时都会根据参数包含这样一个parachute块,默认大小是1M的byte数组,发现OOM产生时,该数组会被设为null,同时清理一些组件的缓存 protected void releaseCaches() { this.keyCache.clear(); this.nioChannels.clear(); this.processorCache.clear(); if ( handler != null ) handler.recycle();
}
之后该通道的请求不会被响应,其它通道仍能正常工作。
protected boolean reclaimParachute(boolean force) {
if ( oomParachuteData != null ) return true;
if ( oomParachute > 0 && ( force || (Runtime.getRuntime().freeMemory() > (oomParachute*2))) )
oomParachuteData = new byte;
return oomParachuteData != null;
}