title: java 8 process memory analysis(Tomcat)
body: <p>Tomcat : apache-tomcat-8.0.21 <br/>
  Java : 1.8.0_91-b14</p>
  
  <p>We have a tomcat deployed with few wars. We are using java 8 version. We want to track the different memory regions and compare it with physical memory(ram) used by the application.</p>
  
  <p><strong>Our observation:</strong><br/>
  Physical memory(RAM) used: 2070M <br/>
  Heap: 1024M <br/>
  Metaspace: ~200M <br/>
  Thread(Stacks): ~116M <br/>
  Code: ~117M <br/>
  GC related: ~90M <br/>
  Others as per jcmd output(Compiler+Internal+Symbol+NMT+Arena chunk+unknown): ~45MB</p>
  
  <p>Adding up the memory regions, it is around 1592M. But this is not equal to 2070M i.e. physical memory(RAM) used. </p>
  
  <p>How can we track the unknown memory, 478M(= 2070M - 1592M)? In the data posted here the difference is 478M, but I can see the difference go upto 1500M. How to track/measure other memory regions. How to track native memory, byte buffers?</p>
  
  <p>Following are some data about application configuration and monitoring tool(like jstat, jcmd) output. Please let me know if any other data is required.</p>
  
  <p><strong>jvm specific values</strong></p>
  
  <pre><code>export CATALINA_OPTS="-Xms1g  -Xmx1g  -XX:MetaspaceSize=256m 
  -XX:MaxMetaspaceSize=512m -XX:+UnlockDiagnosticVMOptions 
  -XX:NativeMemoryTracking=summary -XX:+HeapDumpOnOutOfMemoryError 
  -XX:HeapDumpPath=/opt/tomcat0/temp/heap_dump_${date}.hprof 
  -verbose:gc -XX:+UseG1GC -Xloggc:/opt/tomcat0/logs/gc.log 
  -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails 
  -XX:+UseGCLogFileRotation -XX:GCLogFileSize=10M 
  -XX:NumberOfGCLogFiles=20 -XX:NativeMemoryTracking=detail 
  -XX:+StartAttachListener"
  </code></pre>
  
  <p><strong>unix htop command output</strong>:- ram consumption is 2070mb.
  <a href="https://i.stack.imgur.com/EoMJm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EoMJm.png" alt="unix htop command output"></a></p>
  
  <p><strong>jcmd native memory statistics</strong> </p>
  
  <pre><code>/opt/jdk1.8.0_91/bin/jcmd 22190 VM.native_memory summary
  22190:
  
  Native Memory Tracking:
  
  Total: reserved=2804603KB, committed=1626391KB
  -                 Java Heap (reserved=1048576KB, committed=1048576KB)
                              (mmap: reserved=1048576KB, committed=1048576KB) 
  
  -                     Class (reserved=1229991KB, committed=205991KB)
                              (classes #34804)
                              (malloc=5287KB #65986) 
                              (mmap: reserved=1224704KB, committed=200704KB) 
  
  -                    Thread (reserved=116338KB, committed=116338KB)
                              (thread #113)
                              (stack: reserved=115136KB, committed=115136KB)
                              (malloc=368KB #565) 
                              (arena=834KB #224)
  
  -                      Code (reserved=270143KB, committed=117979KB)
                              (malloc=20543KB #26939) 
                              (mmap: reserved=249600KB, committed=97436KB) 
  
  -                        GC (reserved=90059KB, committed=90059KB)
                              (malloc=18379KB #80926) 
                              (mmap: reserved=71680KB, committed=71680KB) 
  
  -                  Compiler (reserved=393KB, committed=393KB)
                              (malloc=262KB #777) 
                              (arena=131KB #3)
  
  -                  Internal (reserved=15328KB, committed=15328KB)
                              (malloc=15296KB #70770) 
                              (mmap: reserved=32KB, committed=32KB) 
  
  -                    Symbol (reserved=19510KB, committed=19510KB)
                              (malloc=15858KB #174053) 
                              (arena=3652KB #1)
  
  -    Native Memory Tracking (reserved=6889KB, committed=6889KB)
                              (malloc=255KB #3982) 
                              (tracking overhead=6633KB)
  
  -               Arena Chunk (reserved=5329KB, committed=5329KB)
                              (malloc=5329KB) 
  
  -                   Unknown (reserved=2048KB, committed=0KB)
                              (mmap: reserved=2048KB, committed=0KB) 
  </code></pre>
  
  <p><br/></p>
  
  <p><strong>Jstat cmd for metaspace capacity and usage</strong></p>
  
  <pre><code>/opt/jdk1.8.0_91/bin/jstat -gc 22190
   S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
   0.0    0.0    0.0    0.0   11264.0    0.0    1037312.0  1036942.5  200704.0 195572.8 24832.0 23916.7   9140  403.122 1832  6057.159 6460.280
  </code></pre>
  
  <p><br/>
  <strong>Jstat cmd for class stats</strong></p>
  
  <pre><code>/opt/jdk1.8.0_91/bin/jstat -class 22190
  Loaded  Bytes  Unloaded  Bytes     Time   
   38219 69583.9     3360  4243.3     180.27
  </code></pre>