2016-01-22 104 views
0

我在使用NodeJS API时遇到了一些麻烦。内存泄漏会导致getaddrinfo EMFILE

有时候,它返回,ConnectionError: getaddrinfo EMFILE并且在此之后都是fuc **。

所以,我已经开始调查了。我发现这将是由“打开多个文件描述符”引起的。我们显然可以增加已授权的打开文件的数量,但它不会明确解决问题。

我在this article中发现,我们可以增加文件描述符设置和ulimit。但有什么区别?

然后,为了尝试隔离我的问题,我运行了lsof -i -n -P | grep nodejs命令。确实,建立的连接数量正在增加,所以我想我的代码中有一些地方没有关闭连接。

我有一些fs.readFileSyncfs.readDirSync等...但我没有设置autoClose:true。你认为这会是什么?

您有什么想法或建议吗?

PS:Ubuntu的机器


编辑在App运行,16-02-2016

我有我的生产机器上运行此命令lsof -i -n -P | grep nodejs

我看到了什么是这样的:

... 
nodejs 27596 root 631u IPv4 109781565  0t0 TCP 127.0.0.1:45268->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 632u IPv4 109782317  0t0 TCP 172.31.58.93:4242->172.31.55.229:61616 (ESTABLISHED) 
nodejs 27596 root 633u IPv4 109779882  0t0 TCP 127.0.0.1:45174->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 634u IPv4 109779884  0t0 TCP 127.0.0.1:45175->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 635u IPv4 109781569  0t0 TCP 127.0.0.1:45269->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 636u IPv4 109781571  0t0 TCP 127.0.0.1:45270->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 637u IPv4 109782319  0t0 TCP 127.0.0.1:45293->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 642u IPv4 109781790  0t0 TCP 127.0.0.1:45283->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 643u IPv4 109781794  0t0 TCP 127.0.0.1:45284->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 644u IPv4 109781796  0t0 TCP 127.0.0.1:45285->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 645u IPv4 109781798  0t0 TCP 172.31.58.93:4242->172.31.55.229:61602 (ESTABLISHED) 
nodejs 27596 root 646u IPv4 109781800  0t0 TCP 127.0.0.1:45286->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 647u IPv4 109781802  0t0 TCP 172.31.58.93:4242->172.31.0.198:1527 (ESTABLISHED) 
nodejs 27596 root 648u IPv4 109781804  0t0 TCP 127.0.0.1:45287->127.0.0.1:7272 (ESTABLISHED) 
nodejs 27596 root 649u IPv4 109781806  0t0 TCP 127.0.0.1:45288->127.0.0.1:7272 (ESTABLISHED) 

但我不知道这是什么意思,你有什么想法吗?

非常感谢。

回答

1

所以我想我知道发生了什么事情!

我使用Redis的& Redis的会议NPM模块我的购物车的存储,但我创建的时候,更新,我创建的Redis,每次连接,使用它之前。

var session = new Sessions({ port : conf.redisPort, host : conf.redisHost}); 

session.get({ 
    app : rsapp, 
    token : this.sessionToken }, 
    function(err, resp) { 
     // here some workin' 
    }) 

现在我刚刚创建了连接,当我的应用程序启动并将其存储为单例并随时使用。

// At the start of the App 
    var NS = { 
     sessions  : new RedisSessions({port: config.redisPort, host: config.redisHost}), 
    }; 

// Later somewhere in the app   
    NS.session.get({ 
     app : rsapp, 
     token : this.sessionToken }, 
     function(err, resp) { 
      // here some workin' 
     }) 

这是很明显的,但现在我发现它...如果它可以帮助别人,为解决我会记住这一个。