HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to get the actual memory usage of a container

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thecontaineractualusagegetmemoryhow

Problem

I run docker stats and get:

CONTAINER ID   NAME      CPU %    MEM USAGE / LIMIT     MEM %   NET I/O           BLOCK I/O         PIDS
b5ebb4b467f6   haproxy   0.06%    13.84MiB / 78.57GiB   0.02%   35.2MB / 59.2MB   12.1MB / 6.12MB   33


So this tells me that the haproxy container consumes 13MB.

But when I run ps on the host, I see 2 haproxy processes which occupy 13MB and 282MB each:
$ ps -eo size,command | grep -E 'SIZE|haproxy'
SIZE COMMAND
13772 haproxy -W -db -f /usr/local/etc/haproxy/haproxy.cfg
282764 haproxy -W -db -f /usr/local/etc/haproxy/haproxy.cfg


My 2nd test with docker stats is this:

CONTAINER ID   NAME            CPU %   MEM USAGE / LIMIT     MEM %    NET I/O           BLOCK I/O         PIDS
c3fbca04c3a4   R.processing.3  0.22%   248.8MiB / 78.57GiB   0.31%    3.33MB / 1.73MB   18.5MB / 0B       36
88b39d761182   R.processing.2  0.20%   243.9MiB / 78.57GiB   0.30%    3.33MB / 1.73MB   5.98MB / 0B       42
b5065a2f3ffd   R.processing.1  0.25%   239.7MiB / 78.57GiB   0.30%    3.32MB / 1.72MB   1.53MB / 0B       42


Which means that the 3 R containers consume 244MB + 243MB + 239MB. But on the host I get 4.7GB + 4.7GB + 4.7GB:
$ ps -eo size,command | grep -E 'SIZE|R'
SIZE COMMAND
4708864 /opt/R/4.3.2/lib/R/bin/exec/R --no-save
4707860 /opt/R/4.3.2/lib/R/bin/exec/R --no-save
4707680 /opt/R/4.3.2/lib/R/bin/exec/R --no-save


So which one do I believe? Which is correct?

Solution

SIZE is a bit misleading. Quotes are taken from man ps:

SIZE approximate amount of swap space that would be required if the process were to dirty all writable pages and then be swapped out. This number is very rough!

So this only counts writable pages, which may not be really the size taken when the pages are copy on write.

You may use ps -eo rss,command for something a bit more accurate

RSS resident set size, the non-swapped physical memory that a task has used (in kilobytes). (alias rssize, rsz).

For comparison you can do a ps -eo SIZE,RSS,VSZ and check the differences between each value (Swappable, really used, potentially adressable/virtual memory)

With a small caveat:

The SIZE and RSS fields don't count some parts of a process including the page tables, kernel stack, struct thread_info, and struct task_struct. This is usually at least 20 KiB of memory that is always resident.

I don't have a satisfactory answer to give about your docker info vs ps. I'm not sure if the docker commands only takes the first container command (entrypoint) or if it should report the whole cgroup usage. (This is assuming you have only one haproxy container running)

Context

StackExchange DevOps Q#18693, answer score: 2

Revisions (0)

No revisions yet.