node.js - 可能的http服务器内存泄漏 [英] node.js - possible http server memory leak

查看:170
本文介绍了node.js - 可能的http服务器内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Nodejs版本:0.8.8

Nodejs version: 0.8.8

这是服务器:

var http = require('http');
var port = 1338;
var ip = "127.0.0.1";    

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hi there\n');
}).listen(port, ip);    

客户端(php脚本)将发布请求转移到上述服务器。 POST是一个字符串(json),大小约为4兆字节。

Client (php script) curls away a post request to the above server. POST is a string (json), about 4 megabytes in size.

如您所见,服务器对发布的数据不做任何处理。为了调试,我删除了所有代码,然后回到hello world示例,什么都不做:)

当我看一下节点进程的内存使用情况时(在Activity Monitor中完成,mac app) - 它报告节点服务器内存使用量对于每个请求都更大。

因此,在20次请求之后,内存使用量增加了一倍。

As you can see, server does nothing with the posted data. In order to debug, I removed all my code and went back to the hello world example that does nothing :)
When I take a look at the memory usage of the node process (done in Activity Monitor, mac app) - it reports that the node server memory usage is geting larger for every request.
So after 20 requests or so memory usage is doubled.

推荐答案

这不是一个错误。这是正常的预期行为。

This is not a bug. It's normal, expected behaviour.

Node.js基于JavaScript,这是一种垃圾收集语言。简而言之,发生的事情是,内存不会立即释放,而是需要一些时间才能释放内存(例如收集垃圾)。 V8(节点使用)实际上有一个非常智能的垃圾收集器,确保快速对象分配,短垃圾收集暂停,没有内存碎片。

Node.js is based on JavaScript, which is a garbage-collected language. In short, what happens, is that memory is not freed right away, but instead it will take some time until memory is freed (e.g. garbage is collected). V8 (which node uses) actually has a very intelligent garbage collector that "ensures fast object allocation, short garbage collection pauses, and no memory fragmentation".

为了演示这种行为,我在Windows上使用node.js 0.8.8运行上面的脚本,用大型HTTP POST请求轰炸服务器。

To demonstrate this behaviour for you, I ran the above script with node.js 0.8.8 on Windows and bombarded the server with large HTTP POST requests.

Process Explorer显示以下内存使用情况图:

Process Explorer reveals the following memory usage graph:

正如你所看到的,内存使用率上升,直到某个限制为止触发垃圾收集。清理完成后,重置使用情况并再次开始爬升直到下次触发。

As you can see, the memory usage goes up, until a certain limit that triggers garbage collection. After the cleanup, the usage is reset and starts again climbing until the next triggering.

这篇关于node.js - 可能的http服务器内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆