Currently, running my prod apps in cluster mode with pm2 to create multiple instances. But over time, I can see increase in memory usage. How to solve it as for ow I need to restart the app manually?
pm2 start app.js -i 0 -o "/dev/null"
Currently, running my prod apps in cluster mode with pm2 to create multiple instances. But over time, I can see increase in memory usage. How to solve it as for ow I need to restart the app manually?
pm2 start app.js -i 0 -o "/dev/null"
You can either:
--max-memory-restart
option to have pm2 restart instances of your app when they leak too muchI was facing the same issue. It's because pm2 has an issue with the memory leak.
const cluster = require('cluster')
require('events').EventEmitter.defaultMaxListeners = Infinity;
const OS = require('os');
process.env.UV_THREADPOOL_SIZE = OS.cpus().length
let app = express();
const numCPUs = parseInt(process.env.NO_OF_CPU || 1)
if(cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
// start server on port
let appServer = app.listen(process.env.OUTBOUND_PORT, () => {
console.log(`server listening on ${process.env.OUTBOUND_PORT} `);
});
}