Answers: <p><code>SIZE</code> is a bit misleading. Quotes are taken from <code>man ps</code>:</p>
  <blockquote>
  <p>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!</p>
  </blockquote>
  <p>So this only counts writable pages, which may not be really the size taken when the pages are copy on write.</p>
  <p>You may use <code>ps -eo rss,command</code> for something a bit more accurate</p>
  <blockquote>
  <p>RSS       resident set size, the non-swapped physical memory that a task has used (in kilobytes).  (alias rssize, rsz).</p>
  </blockquote>
  <p>For comparison you can do a <code>ps -eo SIZE,RSS,VSZ</code> and check the differences between each value (Swappable, really used, potentially adressable/virtual memory)</p>
  <p>With a small caveat:</p>
  <blockquote>
  <p>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.</p>
  </blockquote>
  <p>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)</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-12-21T13:54:36.670Z
Answers: 2023-12-21T13:54:36.670Z
Answers: Tensibai
Body: <p>I run <code>docker stats</code> and get:</p>
  <pre><code>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
  </code></pre>
  <p>So this tells me that the haproxy container consumes 13MB.</p>
  <p>But when I run <code>ps</code> on the host, I see 2 haproxy processes which occupy 13MB and 282MB each:</p>
  <pre class="lang-bash prettyprint-override"><code>$ 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
  </code></pre>
  <p>My 2nd test with <code>docker stats</code> is this:</p>
  <pre><code>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
  </code></pre>
  <p>Which means that the 3 <code>R</code> containers consume 244MB + 243MB + 239MB. But on the host I get 4.7GB + 4.7GB + 4.7GB:</p>
  <pre class="lang-bash prettyprint-override"><code>$ 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
  </code></pre>
  <p>So which one do I believe? Which is correct?</p>
  
Tags: <docker>
Title: How to get the actual memory usage of a container
UserDisplayName: cherouvim
ThreadText: # How to get the actual memory usage of a container
  
  (Asked by: username_0 on 2023-12-21)
  
  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:
  
  ```bash
  $ 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:
  
  ```bash
  $ 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?
  
  **Tags:** docker
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-21. Score: 2
  
  `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)
  
  
  ---
  
  