iammerlijn online

Node.js memory leak debugging on Kubernetes

You can debug a Node.js memory leak running in Kubernetes by exposing the Node inspector port locally and connecting to it with Chrome DevTools.

This only works if the Node.js process was started with the inspector enabled, for example:

node --inspect=0.0.0.0:9229 app.js

Steps

  1. Port-forward the pod’s Node.js inspector port:

    kubectl port-forward pod/{pod-name} 9229:9229 -n {namespace}
    
  2. Open chrome://inspect in Chrome.

  3. Click Open dedicated DevTools for Node.

  4. Go to the Memory tab.

  5. Take a Heap snapshot.

  6. Wait for memory usage to increase, then take another snapshot.

  7. Compare the snapshots to identify objects that keep growing or are not being released.

Example

kubectl port-forward pod/{pod-name} 9229:9229 -n {namespace}

This forwards your local port 9229 to the pod’s inspector port 9229, so Chrome DevTools can attach to the remote Node.js process as if it were running locally.

Debug

If chrome://inspect does not show the process, verify that:

  • the app was started with --inspect
  • the inspector is bound to 0.0.0.0 or otherwise reachable inside the container
  • the pod name and namespace are correct