title: Java memory settings in a docker container
body: <p>I have a Java application, based on Java8 OpenJDK. It's running in a docker container which has a limit of 20GB.</p>
  <p>The -Xms and -Xmx settings of tomcat is set as follows:</p>
  <p>-Xms = 60% of the container memory (as dictated by the cgroup) - so 12GB
  -Xmx = 80% of the container memory (as dictated by the cgroup) - so 16GB</p>
  <p>This leaves 4GB free on the container, which is usually fine, but sometimes under load I see the docker container exited (and java process killed) with OOM due to the fact container memory usage has exceeded 20GB.</p>
  <p>I know that the -Xmx settings is for the heap, and not the whole Java process and JVM, therefore would expect that the 4GB 'headroom' on the container is enough, but it appears not.</p>
  <p>I know all use cases are wildly different, but my question is whether, in general terms, setting the -Xmx setting is too high for a container whose memory limit is 20GB.</p>
  <p>I was toying with the idea of using the MaxRAM setting, which again, I know only dictates the heap memory, but I'm unsure whether that would have any impact in positive terms?</p>
  <p>Is it generally the case that you use either MaxRAM or -Xmx, or is there any benefit to setting both?</p>
  <p>If I were to use MaxRAM instead of -Xmx, how would java allocate memory to the heap? Is there a simple algorithm for this, for example, 50% of the MaxRAM setting? Will java manage memory any more efficiently doing it that way?</p>