Why does my Java process consumes twice memory inside a docker container vs host
Asked Answered
R

1

8

I faced an interesting problem trying to analyze a memory consumption in my Java application running on docker container vs host machine.

  1. The Java app is web app on the Jetty server 9.4.9
  2. Java version : 1.8
  3. Host : MAC
  4. Docker images: jetty:9.4-jre8
  5. The docker daemon is 18.03.1-ce version.

On the host I'm using Yourkit tool to analyze a memory consumption.

For docker container docker stats <docker id/name>

What I'm getting is that on MAC yourkit shows me 50M Non-heap size + ~40M heap size, in total ~100M

enter image description here

Whereas, when I deploy and run the same war on a container, the stats shows me 200M

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
879fb113ca8d        jetty-app           0.19%               214.6MiB / 1.952GiB   10.74%              1.49MB / 88.9kB     31.7MB / 6.42MB     29

Can anyone shed some light on this phenomenon?

Assuming that stats provides wrong results, I tried to limit the memory on a container using --memory flag doesn't help much, I'm getting OOM.

Thanks in advance

Radiotelegram answered 6/6, 2018 at 11:39 Comment(5)
Showing the exact output of yourkit and docker stats might be useful.Misbelief
the output addedRadiotelegram
There are a couple of things here. 1. Note difference between MB and MiB. 2. docker stats might be including memory used by the threads stacks - this is by default 1MB per thread and is not included in heap memory or non-heap memory, so I would recommend checking thread count in yourkit. 3. Heap memory shown by yourkit looks a bit weird - although app is using only 25MB of heap memory, it might have more than that reserved (green line looks like 75MB).Misbelief
The JVM itself can use 100 MB especially if you have native memory for things like Thread stacks, Sockets, buffers, JARs, the JVM shared libraries etc.Mol
thanks guys for your tips, I will try to run the yourkit on docker container instead of using docker stats commandRadiotelegram
G
3

You might want to try and measure again, with an openJDK 8u212 or more (April, 16th 2019). (no Oracle JDK, since their license has changed)

See "Docker support in Java 8 — finally!" from Grzegorz Kocur.
Now:

There is no need to use any hacky workarounds in a docker entrypoint, nor setting Xmx as fixed value anymore.

Docker support was also backported to Java 8.
Let’s check the newest openjdk image tagged as 8u212. We’ll limit the memory to 1G and use 1 CPU:

docker run -ti --cpus 1 -m 1G openjdk:8u212-jdk

You can fine-tune the heap-size with new flags (already present in Java 10+, but now back ported to Java 8), and explained here.

-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage

If for some reason the new JVM behaviour is not desired it can be switched off using -XX:-UseContainerSupport.

Giselagiselbert answered 15/5, 2019 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.