Why is Tomcat/JVM using 4.5Gb virtual memory on a server with only 4Gb RAM and no swap?

Posted March 28, 2019 3.3k views
Server OptimizationJavaUbuntu 16.04

I’ve changed /etc/default/tomcat7 to have Xms and Xmx of 2048m on a 4Gb server:

JAVA_OPTS=“ -Djava.awt.headless=true -Xms2048m -Xmx2048m -XX:MaxPermSize=256m -XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/share/tomcat7”

However I see tomcat7 with a VIRT of 4575652 and a RES of 1.150g in “top”.

  • Why is Tomcat/JVM using 4.5Gb virtual memory on a server with only 4Gb?
  • Why does Tomcat/JVM not “page in” the Xms of 2048m?

And I thought I understood virtual memory and the JVM :-(

Any help would be greatly appreciated. Thanks.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

So I would recommend you to use the pmap PID :)

  1. After that you will see the more loaded files.
  2. Because you have heap, meta space. stack

Thanks gonchik,

I actually found out that along with the stack and static/code segments the main culprit was glibc’s use of “arenas” for memory allocation.

The default is to allocate 8 x 64Kb spaces per CPU. So in my case that was a whole gigabyte of unexpected RAM use.

This can be tuned by setting the environment variable MALLOC_ARENA_MAX within the startup scripts of Tomcat and the JVM.

The more “arenas” the better the memory allocation concurrency but I have found little difference in my application from the default 8 to a value of 1. I guess it depends how often the JVM needs memory.