nodejs内存泄漏有几种情况

nodejs memory leak in several cases

本文关键字:几种 情况 内存 泄漏 nodejs      更新时间:2023-09-26
  1. 原始的本地tcp服务器我使用了一个客户端测试代码来并发长连接服务器,什么也不做连接5周后,我关闭了client.js,但服务器端将有大约100M的内存

服务器代码:

var net = require('net');
var server = net.createServer(function(client) {
  console.log('server connected');
  client.on('data',function(){});
  client.on('end',function(){console.log('end');});
});
server.listen(8124, function() {
  console.log('server bound');
});

客户端代码:

var net = require('net');
var host = '192.168.0.110'
// var host = "localhost"
  , port = 8124

for(var i=0; i < 50000; i++){
  var client = net.connect({host: host, port: port},
      function(i){
       return function() { //'connect' listener
           var num = i;
           console.log('client connected ' + num);
      }}(i)
  );
  client.on('end',function(){
        console.log('end');
        client.end()
  })
}

客户端在另一台机器上

2、长回路

 var a = b = c = d = [];
 console.log((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2), 'Mb');
 for(i=0;i<50000;i++){
     a.push(new Date());
     b.push(new Date());
     c.push(new Date());
     d.push(new Date());
 }
 console.log((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2), 'Mb');
   a = null;
   b = null;
   c = null;
   d = null;
 console.log('null');
 console.log((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2), 'Mb');
 console.log((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2), 'Mb');
 setInterval(function(){
   console.log((process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2), 'Mb');
 },5000);

我将变量设置为null,但内存不会释放。有人告诉我使用process.nextTick来防止长循环但它仍然不起作用。

取消分配引用并将其设置为null后,内存不会立即释放。如果您想强制GC在给定时刻执行:

  1. 使用--expose-gc启动节点

node --expose-gc test.js

  1. 在代码中添加gc调用:

global.gc();

您不应该在生产中这样做,因为V8本身处理GC调用,并且在这方面做得很好。

您的客户端js:出现问题

client.on('end', function() {
    console.log('end');
    client.end()
})

此事件将在连接结束后发生,您可以通过调用.end()。。。因此在结束事件中调用CCD_ 4没有任何意义。此外,您根本没有关闭连接:

var client = net.connect({
    host: host,
    port: port
}, function(i) {
    return function() { //'connect' listener
        var num = i;
        console.log('client connected ' + num);
        this.end() //Close the connection
    }
}(i));

在修复后,我可以毫无例外地运行你的代码,它没有为我泄漏。

服务器代码:

function getUsed() {
    return process.memoryUsage().heapUsed;
}
var startedWith;
var net = require('net');
var server = net.createServer(function(client) {
    client.on('data', function() {});
    client.on('end', function() {
        console.log("end");
    });
});
server.listen(8124, function() {
    startedWith = getUsed();
});
setInterval(function() {
    gc();
}, 13);
setInterval(function() {
    gc();
    console.log(getUsed() - startedWith);
}, 250)

使用节点0.10 $ node --expose-gc server.js 运行

客户代码:

var net = require('net');
var host = 'localhost',
    port = 8124
for (var i = 0; i < 1500; i++) {
    var client = net.connect({
        host: host,
        port: port
    },
    function(i) {
        return function() { //'connect' listener
            var num = i;
            console.log('client connected ' + num);
            this.end();
        }
    }(i));
    client.on('end', function() {
        console.log('end');
    })
}

在服务器运行后使用节点0.10 $ node client.js运行。