გადაწყდაSpring Boot in a 512 MB container keeps getting OOMKilled

AK Anna Kor წევრი რეგისტრაცია: 4 ივლ. 2026
პოსტები: 93
#1

Trying to understand this properly before touching anything. The setup: a Spring Boot service in Docker, memory limit 512 MB, and the orchestrator kills it with OOMKilled. There is no OutOfMemoryError in the Java logs.

What I have gathered so far:
✅ modern JVMs read the container limit
✅ -Xmx limits only the heap
✅ there is also -XX:MaxRAMPercentage

Questions:
1. -Xmx or MaxRAMPercentage, and what value?
2. Where does the rest of the memory go if the heap is fine?
3. How do I check what the JVM actually sees inside the container?

YS Yury Slinkin თანამშრომელი · ადმინი რეგისტრაცია: 6 ივნ. 2026
პოსტები: 94
#2

Before the details: which JDK and base image, and is 512 MB the limit of this container alone or shared with a sidecar?

AK Anna Kor წევრი რეგისტრაცია: 4 ივლ. 2026
პოსტები: 93
#3

>>2
Eclipse Temurin 25 JRE image, the limit is for this one container, no sidecars.

ER Evgeny Rudenko წევრი რეგისტრაცია: 14 აგვ. 2026
პოსტები: 86
#4

>>3
Then 1 and 2 together. The JVM is container-aware, and by default the maximum heap is 25% of the limit. The heap is only part of the process: metaspace, the code cache, thread stacks, GC structures and direct buffers all live outside it. A kill without an OutOfMemoryError usually means the whole process went over the limit, not the heap.

I prefer MaxRAMPercentage over a fixed -Xmx: it follows the limit if someone changes it. For 512 MB, 60–70% leaves room for the rest:

FROM eclipse-temurin:25-jre
WORKDIR /app
COPY target/app.jar app.jar
USER 1000
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=70", "-XX:+ExitOnOutOfMemoryError", "-jar", "app.jar"]

ExitOnOutOfMemoryError makes the JVM exit on a heap OOM instead of limping on, so the orchestrator restarts it cleanly.

If 70% still gets killed, lower the percentage before raising the limit, and look at the native part (next reply).

YS Yury Slinkin თანამშრომელი · ადმინი რეგისტრაცია: 6 ივნ. 2026
პოსტები: 94
#5

>>3
On 3. What the JVM sees with the same limit, without starting the app:

docker run --rm -m 512m eclipse-temurin:25-jre \
  java -XX:MaxRAMPercentage=70 -XX:+PrintFlagsFinal -version \
  | grep -E 'MaxHeapSize|UseSerialGC|UseG1GC'

Note which collector it prints. On JDK 25, if the container gets one CPU or less than about 1792 MB, the JVM picks Serial instead of G1. JDK 27 makes G1 the default everywhere (https://openjdk.org/projects/jdk/27/); on 25, set -XX:+UseG1GC explicitly if that is what you want.

For the memory outside the heap, Native Memory Tracking:

# add to the java command: -XX:NativeMemoryTracking=summary
docker exec <container> jcmd 1 VM.native_memory summary

It shows heap, metaspace, threads, code cache and the rest as separate lines, which tells you what to cut. NMT adds a little overhead, so switch it on while investigating, not permanently. One catch: jcmd is a JDK tool and is not in the JRE image, so for the investigation build the same app on eclipse-temurin:25-jdk.

AK Anna Kor წევრი რეგისტრაცია: 4 ივლ. 2026
პოსტები: 93
#6

Summary for whoever finds this later:
✅ OOMKilled without OutOfMemoryError = the whole process over the limit, not the heap
✅ -XX:MaxRAMPercentage=60..70 instead of a fixed -Xmx
✅ -XX:+ExitOnOutOfMemoryError for a clean restart
✅ check the chosen GC: on JDK 25 a small container may get Serial
✅ Native Memory Tracking to see where non-heap memory goes

სწრაფი პასუხი

წერა შეუძლიათ მხოლოდ შესულ წევრებს. შესვლა