简体   繁体   English

使用NodeJS时内存泄漏

[英]Memory Leak While Using NodeJS

I think I am just missing something basic. 我想我只是缺少一些基本的东西。 I dont understand the how memory is being allocated. 我不明白如何分配内存。 If I just hit this function over and over again, it leaks like crazy (just watching it grow in top). 如果我一次又一次地点击这个功能,它会像疯了一样泄漏(只是看着它在顶部增长)。 I am new to nodejs and javascript in general. 我是nodejs和javascript的新手。 I can't seem to find any explanation why this is wrong, but it is wrong.. Is it how I am calling handleMessage by using the require statement inline? 我似乎无法找到任何解释为什么这是错误的,但它是错误的..我是如何通过使用require语句内联调用handleMessage? I also tried setting a variable equal to the require statement and just accessed that variable but it still kept eating memory. 我也试过设置一个等于require语句的变量,只是访问了那个变量,但它仍然在吃内存。 Anything will be a big help! 什么都会有很大的帮助!

main.js: main.js:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  if (req.url == "/getRandom")
  {
    require('./getRandom.js').handleMessage(req,res)
  }
  else
  {
    res.end(req.url+'\n');
  }
}).listen(443);
console.log('Server running at http://127.0.0.1:443/');

getRandom.js: getRandom.js:

var qs = require('querystring');
function getRandom()
{
    var numbers = new Array()
    for (i = 0; i < 100; i++)
    {
            numbers[i] = Math.floor(Math.random()*100);
    }
    return numbers;
}
function handleMessage(req,res)
{
    var body = '';
    req.on('data', function (data) {
        body += data;
    });
    req.on('end', function () {
            var t = JSON.parse(body);
            var c = t.name;
            var response = new Object();
            response.numbers = getRandom();
            res.end (JSON.stringify(response));
            response.numbers = null;
            response = null;
            body=null;
            t=null;
    });
}
module.exports.handleMessage = handleMessage;

Memory is allocated for the numbers Array, but I don't see any memory leak here. 内存是为数字 Array分配的,但我没有看到任何内存泄漏。 Garbage collector does not have to run as soon as there is something to claim back. 垃圾收集器不必在有需要回复的情况下立即运行。

To illustrate my point, if you will increase length of the Array to help GC start collecting memory faster (also some minor tweaks) 为了说明我的观点,如果你将增加数组的长度,以帮助GC开始更快地收集内存(也是一些小的调整)

function getRandom()
{
  var numbers = new Array()
  for (var i = 0; i < 100000; i++)
  {
    numbers[i] = Math.floor(Math.random()*100);
  }
  return numbers;
}
function handleMessage(req,res)
{
  var body = '';
  req.on('data', function (data) {
    body += data;
  });
  req.on('end', function () {
    var response = new Object();
    response.numbers = getRandom();
    res.end (JSON.stringify(response));
    response.numbers = null;
    response = null;
  });
}
module.exports.handleMessage = handleMessage; 

memory will be freed faster, so it's actually will be noticeable. 内存将被更快地释放,所以它实际上将是显而易见的。

Here is screenshot from Instruments on OSX, when running the code mentioned above, executing 60 requests per second to /getRandom endpoint 这是来自OSX上的Instruments的屏幕截图,当运行上述代码时,每秒执行60个请求到/ getRandom端点

在此输入图像描述

In top output, it's noticable that RSIZ - (Resident memory size) goes up and down (when memory is allocated, and then - freed) 顶部输出中,RSIZ - (驻留内存大小)上下移动(当分配内存然后 - 释放时)是显而易见的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM