in category Programming
How to gather GC metrics in NodeJS
We can trace NodeJS GC by using
node --trace-gc app.js
And use the performance tool to get the data in runtime.
const { PerformanceObserver } = require('perf_hooks');
// Create a performance observer
const obs = new PerformanceObserver((list) => {
const entry = list.getEntries()[0]
/*
The entry would be an instance of PerformanceEntry containing
metrics of garbage collection.
For example:
PerformanceEntry {
name: 'gc',
entryType: 'gc',
startTime: 2820.567669,
duration: 1.315709,
kind: 1
}
*/
});
// Subscribe notifications of GCs
obs.observe({ entryTypes: ['gc'] });
// Stop subscription
obs.disconnect();
To gather memory usage we can use the following code
console.log(process.memoryUsage());
// Prints:
// {
// rss: 4935680,
// heapTotal: 1826816,
// heapUsed: 650472,
// external: 49879,
// arrayBuffers: 9386
// }
Buy me a coffeeTags:
#nodejs
#gc
#observability