Node.JS memory leak with PM2

前端 未结 2 1220
轻奢々
轻奢々 2021-02-02 02:02

I was running my server with pm2 start ... and pm2 monit was showing me 3GB memory after 2 hours. So I attached memwatch, now I waited for

2条回答
  •  情歌与酒
    2021-02-02 02:42

    There are a lot of really bad answers on this thread. Really, really bad.

    The answer is simple: your code has a memory leak that you need to find and remove, and it's VERY UNLIKELY that the GC is wrong, it's far more likely your code is the culprit.

    So first off:

    1. "I found that with PM2 the garbage collector doesn't work" - this is patently nonsensical, PM2 runs node, which uses V8, which has its own garbage collector built-in. This isn't disabled just because you run PM2.
    2. "So until that is fixed call the GC" - No! Calling the GC manually is lazy if your code has leaks, you should fix them.
    3. "After removing pm2 there is no more memory leak for now." - No, you're simply not monitoring it in PM2; the leak will still be there, you just don't see the error because you're running it using node and not looking at the results.

    Secondly, the actual solution should be:

    Profile your code's memory and CPU usage using the tool of your choice. For most people, that is going to be Chrome's debugging tools connected to the running node instance, and look at which functions are causing memory usage to spike. Simply calling the GC manually is lazy because your code still has a leak but you're just telling V8 to constantly hoover up memory after your app leaks it, which is very lazy practice.

提交回复
热议问题