title: How to set max non-heap memory in a Java 8 (Spring Boot) application?
body: <p>I have 20 Spring Boot (2.3) embedded Tomcat applications running on a Linux machine with 8GB. All applications are Java 1.8 apps. The machine was running out of memory and Linux started killing some of my app processes as a result.</p>
  <p>Using Linux top and Spring Boot admin, I noticed that the max memory heap was set to 2GB:</p>
  <pre><code>java -XX:+PrintFlagsFinal -version | grep HeapSize
  </code></pre>
  <p>As a result, each of the 20 apps are trying to get 2GB of heap size (1/4th of physical mem). Using Spring Boot admin I could see only ~128 MB is being used. So I reduced the max heap size to 512 via <code>java -Xmx512m ...</code> Now, Spring Boot admin shows:</p>
  <p><a href="https://i.stack.imgur.com/BYaY5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BYaY5.png" alt="enter image description here" /></a></p>
  <p>1.33 GB is allocated to non-heap space but only 121 MB is being used. Why is so much being allocated to non-heap space? How can I reduce?</p>
  <p><strong>Update</strong></p>
  <p>According to top each Java process is taking around 2.4GB (VIRT):</p>
  <pre><code>KiB Mem :  8177060 total,   347920 free,  7127736 used,   701404 buff/cache
  KiB Swap:  1128444 total,  1119032 free,     9412 used.   848848 avail Mem
  
    PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    2547 admin    20   0  2.418g 0.372g 0.012g S  0.0  4.8  27:14.43 java
    .
    .
    .
  </code></pre>
  <p><strong>Update 2</strong></p>
  <p>I ran <code>jcmd 7505 VM.native_memory</code> for one of the processes and it reported:</p>
  <pre><code>7505:
  
  Native Memory Tracking:
  
  Total: reserved=1438547KB, committed=296227KB
  -                 Java Heap (reserved=524288KB, committed=123808KB)
                              (mmap: reserved=524288KB, committed=123808KB)
  
  -                     Class (reserved=596663KB, committed=83423KB)
                              (classes #15363)
                              (malloc=2743KB #21177)
                              (mmap: reserved=593920KB, committed=80680KB)
  
  -                    Thread (reserved=33210KB, committed=33210KB)
                              (thread #32)
                              (stack: reserved=31868KB, committed=31868KB)
                              (malloc=102KB #157)
                              (arena=1240KB #62)
  
  -                      Code (reserved=254424KB, committed=27120KB)
                              (malloc=4824KB #8265)
                              (mmap: reserved=249600KB, committed=22296KB)
  
  -                        GC (reserved=1742KB, committed=446KB)
                              (malloc=30KB #305)
                              (mmap: reserved=1712KB, committed=416KB)
  
  -                  Compiler (reserved=1315KB, committed=1315KB)
                              (malloc=60KB #277)
                              (arena=1255KB #9)
  
  -                  Internal (reserved=2695KB, committed=2695KB)
                              (malloc=2663KB #19903)
                              (mmap: reserved=32KB, committed=32KB)
  
  -                    Symbol (reserved=20245KB, committed=20245KB)
                              (malloc=16817KB #167011)
                              (arena=3428KB #1)
  
  -    Native Memory Tracking (reserved=3407KB, committed=3407KB)
                              (malloc=9KB #110)
                              (tracking overhead=3398KB)
  
  -               Arena Chunk (reserved=558KB, committed=558KB)
                              (malloc=558KB)
  </code></pre>