Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inconsistency between docker stats command and docker rest api memory stats

Tags:

docker

ram

when looking at a running container with docker stats command, I can see that the memory usage of a container is 202.3MiB. However, when looking at the same container through the REST API with GET /containers/container_name/stats -> memory_stats-> usage , the usage there shows 242.10 MiB.

there is a big difference between those values. What might be the reason for the difference? I know that the docker client uses the REST API to get its stats, but what am I missing here?

like image 250
Roy Cohen Avatar asked Oct 27 '25 08:10

Roy Cohen


1 Answers

Solved my problem. Initially, I did not take into account cache memory when calculating memory usage.

Say "stats" is the returned json from GET /containers/container_name/stats,

the correct formula is:

memory_usage = stats["memory_stats"]["usage"] - stats["memory_stats"]["stats"]["cache"]

limit = stats["memory_stats"]["limit"]

memory_utilization = memory_usage/limit * 100
like image 101
Roy Cohen Avatar answered Oct 29 '25 21:10

Roy Cohen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!