oom caused by Java Tomcat Max HTTP header size configuration

Keywords: Java Tomcat Apache jvm

Max HTTP header size settings

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

JVM parameter configuration

-Xms800m -Xmx800m

Write a rest api

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

post access

 

Server 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, analysis

 

The maximum size of the jvm heap is 800m, which is oom at the first time

In fact, http11InputBuffer also needs to initialize a buffer, which also takes up a lot of memory.

 

In order to demonstrate, the example in this paper is extreme. The heap size is directly smaller than the buffer size, and one request is oom. In general, even if the heap size is larger than the buffer size, if the max size setting is unreasonable and too large, with the increase of request concurrency, the buffer will occupy a large amount of memory, resulting in oom.

Max size setting too small

 

The exception Request header is too large, which causes the post header not to be saved. This value should be set according to the actual situation.

Posted by dellwoodbu on Tue, 21 Apr 2020 08:21:49 -0700