本文介绍了PM2的Node.JS内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个小时后,我使用pm2 start ...运行服务器,并且pm2 monit向我显示3GB memory.因此,我连接了memwatch,现在我又等待了2个小时,然后pm2 monit所示的内存再次达到3GB.

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 another 2 hours and again the memory shown by pm2 monit reached 3GB.

因此,我检查了生成的memwatch日志.给我看了

So, I checked the logs memwatch generated. Showed me:

{ before: { nodes: 75659, size_bytes: 11141702, size: '10.63 mb' },
  after: { nodes: 73226, size_bytes: 10840598, size: '10.34 mb' },
  change:
   { size_bytes: -301104,
     size: '-294.05 kb',
     freed_nodes: 5141,
     allocated_nodes: 2708,

另一个:

{ before: { nodes: 72591, size_bytes: 10728318, size: '10.23 mb' },
  after: { nodes: 73284, size_bytes: 10798062, size: '10.3 mb' },
  change:
   { size_bytes: 69744,
     size: '68.11 kb',
     freed_nodes: 5931,
     allocated_nodes: 6620,

现在我真的很困惑,这些是最后的日志,所以我很确定那些是pm2 monit显示大量内存泄漏时生成的日志.

Now I am really confused, those are the last logs so I'm pretty sure those are the logs generated when pm2 monit showed the huge memory leak.

那么,为什么memwatch向我显示10MB+内存,而pm2 monit显示给我3GB+?

So, why is memwatch showing me 10MB+ memory and pm2 monit showing 3GB+?

现在切换到forever监视之类的东西,以查看泄漏是否仍然存在.

Now switching to something like forever or monit to see if the leak still exists.

更多背景

  • 我一直在尝试进行概要分析并查找泄漏,并且概要文件上没有显示任何泄漏.
  • 当客户端连接时会启动memwatch差异,而当客户端断开连接时会采用差异.

推荐答案

  • 我也面临着同样的问题,但是经过很少的研究,我发现在使用pm2时,nodejs没有调用垃圾回收器.
  • 因此,在PM2修复程序发布临时解决方案之前,请使用以下命令强制调用垃圾收集器
  • 自变量-expose-gc 以上将使我们能够从节点js强制调用垃圾回收器,现在使用以下代码进行强制车库回收.

    Above argument --expose-gc will allow us to force call garbage collector from node js, now use following code for force garage collection.

    if (global.gc) {
       global.gc();
    } else {
       console.log('Garbage collection unavailable.  use --expose-gc '
       + 'when launching node to enable forced garbage collection.');
    }
    

    这将解决PM2内存泄漏问题.

    This will solve PM2 meomry leak problem.

    这篇关于PM2的Node.JS内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 12:06
查看更多