Java tomcat max-http-header-size配置导致的oom

max-http-header-size设置

server.max-http-header-size=999999999 //953m

JVM参数配置

-Xms800m -Xmx800m

写一个rest api

@RestController("action")
public class HttpHandler {
    @PostMapping("/get")
    public String get() {
        return "get";
    }
}

post访问

 

 服务端OOM

java.lang.OutOfMemoryError: Java heap space
    at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:57) ~[na:1.8.0_152]
    at java.nio.ByteBuffer.allocate(ByteBuffer.java:335) ~[na:1.8.0_152]
    at org.apache.coyote.http11.Http11OutputBuffer.<init>(Http11OutputBuffer.java:107) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.http11.Http11Processor.<init>(Http11Processor.java:162) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.http11.AbstractHttp11Protocol.createProcessor(AbstractHttp11Protocol.java:990) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]

debug,分析

 

而jvm堆的最大大小是800m,第一时间就oom

其实http11InputBuffer也要初始化一个buffer,这个同样要占很多内存。

 

 为了演示,本文例子比较极端,堆的大小直接小于buffer的大小,一次请求就oom,一般情况下就算堆的大小大于buffer的大小,如果这个max-size设置的不合理过大,随着请求并发的上升,buffer也会占用大量内存,从而导致oom。

max-size设置过小

 

 异常Request header is too large,导致post的header存不下来,要根据实际情况进行设置这个值。

posted @ 2020-04-20 23:29  ibrake  阅读(3352)  评论(0编辑  收藏  举报