Hi,
We launch our servers with a --max-memory-restart 700M option. As I understand Node correctly (cf https://futurestud.io/blog/pm2-how-to-start-your-app-with-node-js-v8-arguments), Node will hardly garbage collect anything because its default memory limit is 1.5G.
Should we always set --node-args="max_old_space_size=700 as well? Or PM2 does the job for us?
Also, should we take some extra margin, like 500 instead of 700 on max_old_space_size to not risk it being restarted while GC has not collected everything collectable yet?
Thanks
Hi,
--max-memory-restart doesn't rely on nodejs, it's rather on top of it and pm2 checks your current memory usage. If it's more then the specified value, it'll restart the process.
Pm2 does not add any --node-args by default to avoid related issues. max_old_space_size is a flag that tells to nodejs how much memory it can use. If you can avoid changing it, you should, if not, feel free to increase it. However, pm2's max-memory-restart option won't be affected by the change, so if you set up the limit to 700M in pm2, and a max_old_space_size of 800, it'll still restart at 700M. If you set max_old_space_size to 700 the pm2 max-memory-restart to 800, it'll error at 700 and the pm2 flag will be useless.
IMO, the pm2 max-memory-restart has to be seen as a safety, and max_old_space_size as a feature if you need to use more than 1.5Go memory.
Instead of tweaking this parameter, you might want to add --expose-gc in your node_args and then call pm2 gc to trigger the garbage collector manually. You could even come up with a scheduled script that checks process memory and execute pm2 gc according to the result.
Hope I've addressed your questions, and thx for the great linked resource.
Crystal clear answer thanks!
Dunno if we should mess too much about the GC in a production environment. But that might be necessary... This can be quite CPU intensive. For now we rely a lot on a loadbalancer upfront and kill instances daily.
That's our only choice at the moment to handle those nasty leaks.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
Hi,
--max-memory-restartdoesn't rely on nodejs, it's rather on top of it and pm2 checks your current memory usage. If it's more then the specified value, it'll restart the process.Pm2 does not add any
--node-argsby default to avoid related issues.max_old_space_sizeis a flag that tells to nodejs how much memory it can use. If you can avoid changing it, you should, if not, feel free to increase it. However, pm2'smax-memory-restartoption won't be affected by the change, so if you set up the limit to700Min pm2, and amax_old_space_sizeof 800, it'll still restart at 700M. If you setmax_old_space_sizeto700the pm2max-memory-restartto 800, it'll error at700and the pm2 flag will be useless.IMO, the pm2
max-memory-restarthas to be seen as a safety, andmax_old_space_sizeas a feature if you need to use more than 1.5Go memory.Instead of tweaking this parameter, you might want to add
--expose-gcin your node_args and then callpm2 gcto trigger the garbage collector manually. You could even come up with a scheduled script that checks process memory and executepm2 gcaccording to the result.Hope I've addressed your questions, and thx for the great linked resource.