I have a Spring boot app that talks to Mongo DB using spring-boot-starter-data-mongodb. I am using bootBuildImage to build the container. To make the app work under 180 MB, I tried to tweak as follows:
build.gradle
bootBuildImage {
imageName = "gcr.io/kubegcp-256806/coderprabhu-api:${project.version}"
environment = [
"BPL_JVM_HEAD_ROOM" : "2",
"BPL_JVM_LOADED_CLASS_COUNT" : "35",
"BPL_JVM_THREAD_COUNT" : "10"
]
}
deployment.yaml
resources:
requests:
cpu: "0.02"
memory: "64Mi"
limits:
cpu: "0.3"
memory: "180Mi"
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
- name: BPL_JVM_HEAD_ROOM
value: "2"
- name: BPL_JVM_LOADED_CLASS_COUNT
value: "35"
- name: BPL_JVM_THREAD_COUNT
value: "10"
- name: JAVA_OPTS
value: >-
-XX:ReservedCodeCacheSize=80M
-XX:MaxMetaspaceSize=60M
-Xlog:gc
-Xms110m
-Xmx140m
-XX:MaxRAM=180M
-XX:+PrintFlagsFinal
Build pack that bootBuildImage uses has 240M for just ReservedCodeCache size. So tried to squeeze is down and spring boot with spring data mongo db seems to be ok with 80MB. I get OOMKilled if MaxMetaspaceSize is less than 60 MB. I only have 2 tomcat threads to serve here.
Final GC Flags
kubectl logs coderprabhu-api-app-pod | grep command
int ActiveProcessorCount = 1 {product} {command line}
size_t InitialHeapSize = 115343360 {product} {command line}
size_t MaxHeapSize = 146800640 {product} {command line}
size_t MaxMetaspaceSize = 62914560 {product} {command line}
uint64_t MaxRAM = 188743680 {pd product} {command line}
bool PrintFlagsFinal = true {product} {command line}
uintx ReservedCodeCacheSize = 83886080 {pd product} {command line}
The container gets OOMKilled after some idle time and I see following logs.
allocated memory is greater than 180M available for allocation: -XX:MaxDirectMemorySize=10M, -Xmx140M, -XX:MaxMetaspaceSize=60M, -XX:ReservedCodeCacheSize=80M, -Xss1M x 10 threads%
The node/express app on same server is happily serving complex functions at less than 30MB of total usage.
What is the smallest maximum memory a simple Spring Boot container can work with without getting OOMKilled?