<p>I need to monitor my container memory usage running on kubernetes cluster. After read some  articles there're two recommendations: &quot;container_memory_rss&quot;, &quot;container_memory_working_set_bytes&quot;</p>
<p>The definitions of both metrics are said (from the cAdvisor code)</p>
<ul>
<li>&quot;container_memory_rss&quot; : The amount of anonymous and swap cache memory</li>
<li>&quot;container_memory_working_set_bytes&quot;: The amount of working set memory, this includes recently accessed memory, dirty memory, and kernel memory</li>
</ul>
<p>I think both metrics are represent the bytes size on the physical memory that process uses. But there are some differences between the two values from my grafana dashboard.</p>
<p><strong>My question is:</strong></p>
<ul>
<li>What is the difference between two metrics?</li>
<li>Which metrics are much proper to monitor memory usage? Some post said both because one of those metrics reaches to the limit, then that container is oom killed.</li>
</ul>

zeroFruit
<p>You are right. I will try to address your questions in more detail.</p>
<blockquote>
<p>What is the difference between two metrics?</p>
</blockquote>
<p><code>container_memory_rss</code> equals to the value of <code>total_rss</code> from <code>/sys/fs/cgroups/memory/memory.status</code> file:</p>
<pre><code>// The amount of anonymous and swap cache memory (includes transparent
// hugepages).
// Units: Bytes.
RSS uint64 `json:&quot;rss&quot;`
</code></pre>
<p>The total amount of anonymous and swap cache memory (it includes transparent hugepages), and it equals to the value of <code>total_rss</code> from <code>memory.status</code> file. This should not be confused with the true <code>resident set size</code> or the amount of physical memory used by the cgroup. <code>rss + file_mapped</code> will give you the resident set size of cgroup. It does not include memory that is swapped out. It does include memory from shared libraries as long as the pages from those libraries are actually in memory. It does include all stack and heap memory.</p>
<hr />
<p><code>container_memory_working_set_bytes</code> (as already mentioned by Olesya) is the <code>total usage</code> - <code>inactive file</code>. It is an estimate of how much memory cannot be evicted:</p>
<pre><code>// The amount of working set memory, this includes recently accessed memory,
// dirty memory, and kernel memory. Working set is &lt;= &quot;usage&quot;.
// Units: Bytes.
WorkingSet uint64 `json:&quot;working_set&quot;`
</code></pre>
<p>Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages touched recently by the threads in the process.</p>
<hr />
<blockquote>
<p>Which metrics are much proper to monitor memory usage? Some post said
both because one of those metrics reaches to the limit, then that
container is oom killed.</p>
</blockquote>
<p>If you are <a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/" rel="noreferrer">limiting the resource usage</a> for your pods than you should monitor both as they will cause an oom-kill if they reach a particular resource limit.</p>
<p>I also recommend <a href="https://faun.pub/how-much-is-too-much-the-linux-oomkiller-and-used-memory-d32186f29c9d" rel="noreferrer">this article</a> which shows an example explaining the below assertion:</p>
<blockquote>
<p>You might think that memory utilization is easily tracked with
<code>container_memory_usage_bytes</code>, however, this metric also includes
cached (think filesystem cache) items that can be evicted under memory
pressure. The better metric is <code>container_memory_working_set_bytes</code> as
this is what the OOM killer is watching for.</p>
</blockquote>
<p><strong>EDIT:</strong></p>
<p>Adding some additional sources as a supplement:</p>
<ul>
<li><p><a href="https://blog.freshtracks.io/a-deep-dive-into-kubernetes-metrics-part-3-container-resource-metrics-361c5ee46e66" rel="noreferrer">A Deep Dive into Kubernetes Metrics — Part 3 Container Resource Metrics</a></p>
</li>
<li><p><a href="https://github.com/google/cadvisor/issues/1744" rel="noreferrer">#1744</a></p>
</li>
<li><p><a href="https://www.magalix.com/blog/memory_working_set-vs-memory_rss" rel="noreferrer">Understanding Kubernetes Memory Metrics</a></p>
</li>
<li><p><a href="https://medium.com/@eng.mohamed.m.saeed/memory-working-set-vs-memory-rss-in-kubernetes-which-one-you-should-monitor-8ef77bf0acee" rel="noreferrer">Memory_working_set vs Memory_rss in Kubernetes, which one you should monitor?</a></p>
</li>
<li><p><a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/" rel="noreferrer">Managing Resources for Containers</a></p>
</li>
<li><p><a href="https://github.com/google/cadvisor/blob/50b23f4ed9bc53cf068316b67bee04c4145f1e73/info/v1/container.go#L379" rel="noreferrer">cAdvisor code</a></p>
</li>
</ul>
