简体   繁体   English

node.js - 可能的http服务器内存泄漏

[英]node.js - possible http server memory leak

Nodejs version: 0.8.8 Nodejs版本:0.8.8

Here is the server: 这是服务器:

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);    

Client (php script) curls away a post request to the above server. 客户端(php脚本)将发布请求卷曲到上述服务器。 POST is a string (json), about 4 megabytes in size. POST是一个字符串(json),大小约为4兆字节。

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 :) 为了调试,我删除了所有代码并回到了hello world示例,什么也没做:)
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. 当我查看节点进程的内存使用情况(在Activity Monitor,mac app中完成)时 - 它报告节点服务器内存使用量对于每个请求都是更大的。
So after 20 requests or so memory usage is doubled. 所以在20次请求之后,内存使用量翻了一番。

This is not a bug. 这不是一个错误。 It's normal, expected behaviour. 这是正常的,预期的行为。

Node.js is based on JavaScript, which is a garbage-collected language. Node.js基于JavaScript,这是一种垃圾收集语言。 In short, what happens, is that memory is not freed right away, but instead it will take some time until memory is freed (eg 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". V8(节点使用)实际上有一个非常智能的垃圾收集器 ,“确保快速的对象分配,短暂的垃圾收集暂停,没有内存碎片”。

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. 为了演示这种行为,我在Windows上使用node.js 0.8.8运行上述脚本,并使用大型HTTP POST请求轰炸服务器。

Process Explorer reveals the following memory usage graph: Process Explorer显示以下内存使用情况图:

在此输入图像描述

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. 清理后,重置使用情况并再次开始攀爬,直到下一次触发。

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

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