Frequent Heap Out of memory in node.js application inside a docker container Frequent Heap Out of memory in node.js application inside a docker container kubernetes kubernetes

Frequent Heap Out of memory in node.js application inside a docker container


From kubernetes point of view you should consider concept of Managing Compute Resources for Containers:

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: the amount of CPU and memory it can provide for Pods. 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.

The spec.containers[].resources.limits.memory is converted to an integer, and used as the value of the --memory flag in the docker run command.

A Container can exceed its memory request if the Node has memory available. But a Container is not allowed to use more than its memory limit. If a Container allocates more memory than its limit, the Container becomes a candidate for termination. If the Container continues to consume memory beyond its limit, the Container is terminated.

Why to use memory limits:

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.

As an example of using resource requests and limits:

apiVersion: v1kind: Podmetadata:  name: memory-demo-2  namespace: mem-examplespec:  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"]

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

kubectl describe pod your_pod If metric server was installed:kubectl top pod your_pod ## to see memory usage.

From node.js perspective probably you will be interested with:

Hope this help.

Additional resources: