<p>It is known that some applications aren't aware of Linux kernel isolation and virtualization features such as cgroups. This includes system utils like <code>top</code>, <code>free</code> and <code>ps</code>, but also platforms like Java.</p>

<p>I've recently read <a href="https://very-serio.us/2017/12/05/running-jvms-in-kubernetes/" rel="nofollow noreferrer">an article</a> which suggests that when running JVMs in Kubernetes, you should enforce manual limits on the Java heap size to avoid errors.</p>

<p>I cannot find anywhere whether this is also true for NodeJS. Do I need to implement something like above to set <code>--max_old_space_size=XXX</code> on my NodeJS application in Kubernetes?</p>

<p>A NodeJS process will try an allocate memory regardless of the container limits, just like Java. </p>

<p>Setting a limit on the process will help stop the OS from killing the process, particularly in constrained environments where Node might try allocate past the memory limit even though Node could probably run inside the limit. </p>

<p>If you are running an app that is close to using the memory limit then adding the memory limit settings just changes the failure scenario. NodeJS and the JVM will have a chance to exit with an out of memory error (OOM) rather than be killed by the operating system. The process will likely slow to a crawl as it nears the memory limit and the garbage collector tries the best it can to keep the process below the limit. </p>

<p>Note that the old space is only one of multiple memory spaces in NodeJS. Only the new space (semi spaces) and old space can be limited. </p>

<pre><code>--max_semi_space_size (max size of a semi-space (in MBytes), the new space consists of two semi-spaces)
    type: int  default: 0
--max_old_space_size (max size of the old space (in Mbytes))
    type: int  default: 0
</code></pre>

<p>The other heap spaces are generally small and static enough not to worry about. </p>

<p>Modules that run native code can allocate memory outside the heap and can't be limited by an option. </p>
