<p>I have a Kubernetes pod which shuts down and restarts itself because it went out of memory. There are multiple child processes which run asynchronously and use memory. I want each of them to log how much memory is available/used on the Kubernetes pod. The logs should be with the other Node js logs.</p>
<p>I know of <code>process.memoryUsage()</code>, <code>os.totalmem()</code>, <code>os.freemem()</code></p>
<p>Which provide the following output respectively</p>
<pre><code>{&quot;ts&quot;:&quot;2021-04-21T10:52:50.299Z&quot;,&quot;msg&quot;:&quot;Node Memory Stats:{\&quot;rss\&quot;:125792256,\&quot;heapTotal\&quot;:94720000,\&quot;heapUsed\&quot;:48927816,\&quot;external\&quot;:1668086}&quot;,&quot;logger&quot;:&quot;___&quot;,&quot;level&quot;:&quot;info&quot;}
{&quot;ts&quot;:&quot;2021-04-21T10:52:50.300Z&quot;,&quot;msg&quot;:&quot;OS/POD Total Memory:17179869184, OS/POD Free memory:87195648&quot;,&quot;logger&quot;:&quot;___&quot;,&quot;level&quot;:&quot;info&quot;}
</code></pre>
<p>I was wondering if there is any way I could do a system call to the pod, get its resources, and log in Node?</p>

Farhan Qasim
<p>You can simply check the values from <code>/sys/fs/cgroup/cpu/</code> and <code>/sys/fs/cgroup/memory/</code>:</p>
<p>For the current <strong>CPU</strong> usage do a</p>
<pre><code>$ cat /sys/fs/cgroup/cpu/cpuacct.usage
</code></pre>
<p>For the current <strong>memory</strong> usage do a</p>
<pre><code>$ cat /sys/fs/cgroup/memory/memory.usage_in_bytes
</code></pre>
<p>Hint: Make sure you have added the resources section (requests and limits) to the deployment so that it can calculate the usage based on cgroup.</p>
<p>see <a href="https://stackoverflow.com/a/62717009/1560953">https://stackoverflow.com/a/62717009/1560953</a></p>
