<p>My node.js application has 16 microservices a <strong>docker Image</strong> and hosted in google cloud platform with kubernetes.</p>

<p>But only for <em>100 user's api request</em>, some main docker images are getting crashed due to <strong>heap out of memory - javascript</strong>.</p>

<p>I checked those images, and that has 1.4 Gb heap memory limit for node.js. But it's getting fully used very soon for a low amount of API traffic also.</p>

<p>How to manage/allocate heap memory docker/kubernetes for node.js ? Alternatively, is there any way find out where the memory leak is happening ?</p>

Janen R
<p>From kubernetes point of view you should consider concept of <a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/" rel="noreferrer">Managing Compute Resources for Containers</a>: </p>

<blockquote>
  <p>When you create a Pod, the Kubernetes scheduler selects a node for the Pod to run on. Each node has a maximum capacity for each of the resource types: <strong>the amount of CPU and memory</strong> <strong>it can provide for Pods</strong>. The scheduler ensures that, for each resource type, the sum of the resource requests of the scheduled Containers is less than the capacity of the node.</p>
  
  <p>The <strong>spec.containers[].resources.limits.memory</strong> is converted to an integer, and used as the value of the <strong>--memory flag in the docker run command</strong>.</p>
  
  <p>A Container can exceed its memory request if the Node has memory available. But a <strong>Container is not allowed to use more than its memory limit</strong>. If a Container allocates more memory than its limit, the Container becomes a candidate for termination. <strong>If the Container continues to consume memory beyond its limit, the Container is terminated</strong>. </p>
</blockquote>

<p>Why to use memory limits:</p>

<blockquote>
  <p>The Container has no upper bound on the amount of memory it uses. The Container could use all of the memory available on the Node where it is running which in turn could invoke the OOM Killer.</p>
</blockquote>

<p>As an example of using <a href="https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/pods/resource/memory-request-limit-2.yaml" rel="noreferrer">resource requests and limits</a>: </p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: memory-demo-2
  namespace: mem-example
spec:
  containers:
  - name: memory-demo-2-ctr
    image: polinux/stress
    resources:
      requests:
        memory: "50Mi"
      limits:
        memory: "100Mi"
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]
</code></pre>

<p>To find out more infomration about your pods/containers state you can use:</p>

<pre><code>kubectl describe pod your_pod 
If metric server was installed:
kubectl top pod your_pod ## to see memory usage.
</code></pre>

<p>From node.js perspective probably you will be interested with:</p>

<ul>
<li><a href="https://developer.ibm.com/articles/nodejs-memory-management-in-container-environments/" rel="noreferrer">memory management in container environments</a></li>
<li><a href="https://medium.com/tech-tajawal/memory-leaks-in-nodejs-quick-overview-988c23b24dba" rel="noreferrer">how to detect detect memory leaks in NodeJS</a> </li>
<li><a href="https://www.valentinog.com/blog/memory-usage-node-js/" rel="noreferrer">How to inspect the memory usage of a process in Node.Js</a>  </li>
</ul>

<p>Hope this help.</p>

<p>Additional resources:</p>

<ul>
<li><a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/" rel="noreferrer">Assign Memory Resources to Containers and Pods</a> </li>
<li><a href="https://www.replex.io/blog/kubernetes-in-production-the-ultimate-guide-to-monitoring-resource-metrics" rel="noreferrer">Kubernetes in Production: The Ultimate Guide to Monitoring Resource Metrics with Prometheus</a> </li>
</ul>
