Search code examples
javadockermemoryjvmlimit

Docker run -m doesnt set the limit (JVM takes up the entire host machine's size for xms and xmx)


I have a server with 16GB RAM. I am trying to set the memory limit to the container by running

docker run -it -m 500M <image-name>

On the docker stats, it shows the container has the memory limit of 500MB. But when i try to do

docker exec -it <containerID> /bin/sh

and do a "top", it shows the available memory as 16GB..

Should it not be set to 500MB ?

The main reason why i need it to be 500 MB is I have java application running in each container. And by default the JVM takes 16gb/64 as the Xms and 4gb as Xmx. :(


Solution

  • I think this article will help you to understand why free and top doesn't work correctly inside containers.

    Most of the Linux tools providing system resource metrics were created before 
    cgroups even existed (e.g.: free and top, both from procps). They usually read 
    memory metrics from the proc filesystem: /proc/meminfo, /proc/vmstat, 
    /proc/PID/smaps and others.
    
    Unfortunately /proc/meminfo, /proc/vmstat and friends are not containerized. 
    Meaning that they are not cgroup-aware. They will always display memory 
    numbers from the host system (physical or virtual machine) as a whole, which 
    is useless for modern Linux containers
    
    This causes a lot of confusion for users of Linux containers. Why does free 
    say there is 32GB free memory, when the container only allows 512MB to be 
    used?
    

    https://fabiokung.com/2014/03/13/memory-inside-linux-containers/