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
-
Port-forward the pod’s Node.js inspector port:
kubectl port-forward pod/{pod-name} 9229:9229 -n {namespace} -
Open
chrome://inspectin Chrome. -
Click Open dedicated DevTools for Node.
-
Go to the Memory tab.
-
Take a Heap snapshot.
-
Wait for memory usage to increase, then take another snapshot.
-
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.0or otherwise reachable inside the container - the pod name and namespace are correct