tomcat OOM

OutOfMemoryError

1
2
3

java.lang.OutOfMemoryError: Java heap space

자바 환경에서 운영중에 OOM을 만난적이 있을것이다.

일반적으로 Heap최대치는 물리메모리 1/4 (8기가기준 약 2기가)

간단한 프로그램을 하나 짜서 테스트 해 보겠다.

간단히 OOM을 발생 시킬수 있는 코드이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package com.github.sejoung.controller;

import java.util.Stack;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class Controller {

@GetMapping("/test")
public void test() {
var stack = new Stack<String>();
int mb = 1024 * 1024;
var runtime = Runtime.getRuntime();
log.info("Free Memory = {} Max Memory = {} totalMemory = {}",
runtime.freeMemory() / mb, runtime.maxMemory() / mb,
runtime.totalMemory() / mb);
IntStream.range(1, Integer.MAX_VALUE).forEach((i) -> stack.push("마구닥 들어가라" + i));
}

}


스텍을 만들어 스텍에 객체를 계속 넣는 것이다.

1
java -server -Xms256m -Xmx512m -jar tomcatOOM.jar

-Xms=N - 최소 힙사이즈

-Xmx=N - 최대 힙사이즈

-XX:MetaspaceSize=N - 메타 공간의 초기 크기 (및 최소 크기)를 설정합니다.

-XX:MaxMetaspaceSize=N - 메타 공간의 최대 크기를 설정합니다.

1
2
3

curl -X GET "http://localhost:8080/apis/test"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
java.lang.OutOfMemoryError: Java heap space
at java.base/jdk.internal.misc.Unsafe.allocateUninitializedArray(Unsafe.java:1269)
at java.base/java.lang.invoke.StringConcatFactory$MethodHandleInlineCopyStrategy.newArray(StringConcatFactory.java:1633)
at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(DirectMethodHandle$Holder)
at java.base/java.lang.invoke.LambdaForm$MH/0x0000000800374c40.invoke(LambdaForm$MH)
at java.base/java.lang.invoke.Invokers$Holder.linkToTargetMethod(Invokers$Holder)
at com.github.sejoung.controller.Controller.lambda$test$0(Controller.java:21)
at com.github.sejoung.controller.Controller$$Lambda$468/0x0000000800537c40.accept(Unknown Source)
at java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104)
at java.base/java.util.stream.IntPipeline$Head.forEach(IntPipeline.java:593)
at com.github.sejoung.controller.Controller.test(Controller.java:21)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

위에 처럼 OOM이 발생된다.

참조