欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tomcat的NIO 连接器中oomParachute的作用

程序员文章站 2022-07-15 16:35:47
...
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[oomParachute]; 
        return oomParachuteData != null;
    }
上面oomParachute > 0 并且  force传入值为true,因此,此处会创建 oomParachuteData,值为1M的byte数组
注意:是每个connector都会创建一个,


[2014-04-16 16:01:18] [WARNING] [System.out] [java.lang.OutOfMemoryError: Java heap space]
[2014-04-16 16:01:19] [WARNING] [System.out] [Exception in thread "RMI TCP Connection(idle)" ]
[2014-04-16 16:01:19] [WARNING] [System.out] [java.lang.OutOfMemoryError: Java heap space]
[2014-04-16 16:01:19] [WARNING] [System.out] [Exception in thread "RMI TCP Connection(idle)" ]
[2014-04-16 16:01:19] [WARNING] [System.out] [java.lang.OutOfMemoryError: Java heap space]
[2014-04-16 16:02:54] [SEVERE] [web-container] []
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.<init>(ByteChunk.java:130)
        at com.tongweb.web.oro.http11.filters.BufferedInputFilter.<init>(BufferedInputFilter.java:43
)
        at com.tongweb.web.oro.http11.AbstractHttp11Processor.initializeFilters(AbstractHttp11Proces
sor.java:698)
        at com.tongweb.web.oro.http11.Http11NioProcessor.<init>(Http11NioProcessor.java:76)
        at com.tongweb.web.oro.http11.ThorHttp11NioProcessor.<init>(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)
[2014-04-16 16:03:05] [SEVERE] [web-container] []
java.lang.OutOfMemoryError: Java heap space
[2014-04-16 16:05:34] [SEVERE] [web-container] []
java.lang.OutOfMemoryError: Java heap space
[2014-04-16 16:05:56] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:10] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:20] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:22] [SEVERE] [web-container] []
java.lang.OutOfMemoryError: Java heap space
[2014-04-16 16:06:26] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:30] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:31] [SEVERE] [web-container] []
java.lang.OutOfMemoryError: Java heap space
[2014-04-16 16:06:36] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:40] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]
[2014-04-16 16:06:46] [SEVERE] [web-container] [SEVERE:Memory usage is low, parachute is non existen
t, your system may start failing.]

目前可以模拟相应的场景,在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[oomParachute];
        return oomParachuteData != null;
    }