<p>I have a NodeJS application running on a k8s pod. The actual size of the pod is 2GB, but in environment variables, we set this value to 4GB <code>--max-old-space-size=4096</code> (which will not be true in my case - for some tenants we do allocate 4GB but most pods have 2GB).</p>
<p>Now I tried 2 ways to detect the memory usage and the total memory and both are providing different stats.</p>
<ol>
<li>I'm fetching memory usage from this system file: <code>/sys/fs/cgroup/memory/memory.usage_in_bytes</code> and total memory from this file : <code>/sys/fs/cgroup/memory/memory.limit_in_bytes</code></li>
</ol>
<p><code>limit_in_bytes</code> is returning 2GB correctly, but the value of <code>usage_in_bytes</code> is fluctuating too much, It's around 1gb in few mins and spikes to 2GB in next minute even though nothing changed in that minute(no stress on the system).</p>
<p>Stats of a process</p>
<pre><code>Memory Usage POD: 2145124352
shlog - memLimit 214748364
</code></pre>
<ol>
<li><p>The second option I tried is using this <code>V8</code> built in node js library to get heap statistics : <a href="https://nodejs.org/api/v8.html#v8getheapstatistics" rel="nofollow noreferrer">https://nodejs.org/api/v8.html#v8getheapstatistics</a>.
Usage:</p>
<pre><code> const initialStats = v8.getHeapStatistics();
 console.log(&quot;heap_size_limit: &quot;, (initialStats.heap_size_limit)); // total memory
 console.log(&quot;total_heap_size: &quot;, (initialStats.total_heap_size)); // current usage
</code></pre>
</li>
</ol>
<p>Now in total memory, it's return 4G, which is not right in my case. but the current usage here seems much true.</p>
<p>Stats of the same process</p>
<pre><code>total_heap_size: 126312448,
heap_size_limit: 4320133120,
</code></pre>
<p>Complete response of v8 getHeapStatistics method:</p>
<pre><code>HeapStats:  {
    total_heap_size: 126312448,
    total_heap_size_executable: 1097728,
    total_physical_size: 124876920,
    total_available_size: 4198923736,
    used_heap_size: 121633632,
    heap_size_limit: 4320133120,
    malloced_memory: 73784,
    peak_malloced_memory: 9831240,
    does_zap_garbage: 0,
    number_of_native_contexts: 1,
    number_of_detached_contexts: 0
}
</code></pre>
<p>My goal is to detect the memory usage according to the total memory of the pod, and so some throttling when the memory consumption reached 85%. I'm willing to use the first method but please tell me why there is so much difference in the memory usage and how to get the accurate memory usage of the pod.</p>
<p>Really looking forward to get some help on this. Thank you.</p>

ShahtajK
<p>To get overall memory consumption of a process, look to (and trust) the operating system's facilities.</p>
<p>Node's <code>v8.getHeapStatistics</code> tell you about the managed (a.k.a. garbage-collected) heap where all the JavaScript objects live. But there can be quite a bit of other, non-garbage-collected memory in the process, for example Node buffers and certain strings, and various general infrastructure that isn't on the managed heap. In the average Chrome renderer process, the JavaScript heap tends to be around a third of total memory consumption, but with significant outliers in both directions; for Node apps it depends a lot on what your app is doing.</p>
<p>Setting V8's max heap size (which, again, is just the garbage-collected part of the process' overall memory usage) to a value larger than the amount of memory available to you doesn't make much sense: it invites avoidable crashes, because V8 won't spend as much time on garbage collection when it thinks there's lots of available memory left, but the OS/pod as a whole could already be facing an out-of-memory situation at that point. That's why I linked the other answer: you very likely want to set the max heap size to something a little less than available memory, to give the garbage collector the right hints about when to work harder to stay under the limit.</p>
