title: What is the smallest maximum memory a simple Spring Boot container can work with without getting OOMKilled?
body: <p>I have a <a href="https://github.com/CoderPraBhu/coderprabhu-api/blob/master/k8s/coderprabhu-api-deployment.yaml" rel="nofollow noreferrer">Spring boot app</a> 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:   </p>
  
  <p><strong>build.gradle</strong></p>
  
  <pre><code>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"
          ]
  }
  </code></pre>
  
  <p><strong>deployment.yaml</strong></p>
  
  <pre><code>        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: &gt;-
                  -XX:ReservedCodeCacheSize=80M
                  -XX:MaxMetaspaceSize=60M
                  -Xlog:gc
                  -Xms110m
                  -Xmx140m
                  -XX:MaxRAM=180M
                  -XX:+PrintFlagsFinal
  </code></pre>
  
  <p>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. </p>
  
  <p><strong>Final GC Flags</strong></p>
  
  <pre><code>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}
  </code></pre>
  
  <p>The container gets OOMKilled after some idle time and I see following logs. </p>
  
  <pre><code>allocated memory is greater than 180M available for allocation: -XX:MaxDirectMemorySize=10M, -Xmx140M, -XX:MaxMetaspaceSize=60M, -XX:ReservedCodeCacheSize=80M, -Xss1M x 10 threads%                                                                                    
  </code></pre>
  
  <p>The node/express app on same server is happily serving complex functions at less than 30MB of total usage.</p>
  
  <p>What is the smallest maximum memory a simple Spring Boot container can work with without getting OOMKilled?</p>