Node.js内存消耗OS X与Linux [英] Node.js memory consumption OS X vs Linux

查看:158
本文介绍了Node.js内存消耗OS X与Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Node.js中编写了一个超简单的TCP/IP套接字服务器:

I have written a super-simple TCP/IP socket server in Node.js:

'use strict';

var net = require('net');

net.createServer(function (socket) {
  socket.resume();
  socket.once('end', function () {
    socket.removeAllListeners();
  });
}).listen(3000);

once('end', ...甚至没有必要,但是我想确定.然后,我从终端运行以下命令以将500 MBytes的数据发送到服务器:

The once('end', ... should not even be necessary, but I wanted to be sure. Then I run the following command from the terminal to send 500 MBytes of data to the server:

$ cat 500mb.txt | nc localhost 3000

现在变得有趣了.我正在使用top观看node进程,并且在OS X和Linux上都执行此操作.

Now it gets interesting. I am watching the node process using top, and I do this on OS X and on Linux.

当我启动Node.js时...

When I start Node.js…

  • OS X报告MEM 5152K(看起来不错)
  • Linux报告RES 15180(我将其解释为 15 MB ,但是我不确定).
  • OS X reports MEM 5152K (which seems fine)
  • Linux reports RES 15180 (which I interpret as 15 MB, but I'm not sure with this).

第一个问题:两个值是否可比,还是我在这里遗漏了一些东西?

First question: Are both values comparable, or am I missing something here?

然后,当我运行cat ... | nc ...时,内存使用率上升.一个电话后...

Then, as soon as I run cat ... | nc ..., memory usage raises. After one call…

  • OS X报告MEM 20M
  • Linux报告RES 92320(这意味着使用92(!)MB内存)
  • OS X reports MEM 20M
  • Linux reports RES 92320 (which would means 92 (!) MB memory usage)

这是怎么回事?为什么Node.js在Linux上比OS X上使用更多的内存?我的设置错了吗?我想念什么吗? ...?

What is going on here? Why is Node.js using so much more memory on Linux than on OS X? Is my setup wrong? Am I missing something? ...?

推荐答案

Linux和Mac OS X的内存管理器是复杂的野兽,弄清进程的内存使用情况是一门妖术.

The memory managers of Linux and Mac OS X are complex beasts, and working out the memory usage of a process is a black art.

在Linux上,RSS大小是 resident 进程的大小.它包括已映射的二进制文件和库以及匿名映射的页面(malloc数据)之类的内容,但不包括未映射的数据,例如当前未映射的二进制文件的某些部分,已映射出的匿名页面或延迟分配的匿名页面.即使您的应用程序工作不多,当有足够的内存时,它也会增长;如果由于不同的过程而导致内存压力,它将缩小.从程序员的角度来看,这几乎是无用的(但从系统管理员的角度来看非常有用).

On Linux, the RSS size is the resident process size. It includes things like mapped binaries and libraries and anonymously mapped pages (malloc data), but doesn't include unmapped data, such as parts of the binary that are not currently mapped, anonymous pages that have been mapped out, or lazily allocated anonymous pages. It will grow when there's plenty of memory even if your application is not doing much, and will shrink if there's memory pressure due to a different process. It's pretty much useless from a programmer's point of view (but very useful from the system administrators point of view).

在Mac OS X上,MEM列记录为内部存储器大小".如果不检查top和内核的来源,就不可能知道它的确切含义.

On Mac OS X, the MEM column is documented as "Internal memory size". Without checking the sources of both top and the kernel, it is impossible to know what it reflects exactly.

总而言之,只要您的应用程序运行良好,所显示的不同值就不必担心.查看操作系统提供的值可能有助于大致了解是否有问题,但查看V8堆分析器提供的值可能会更好.

In summary, the different values being displayed are probably nothing to worry about as long as your application is running fine. While looking at the OS-provided values can be useful as a rough idea of whether anything is wrong, you'll probably be way better served by looking at the values provided by V8's heap profiler.

这篇关于Node.js内存消耗OS X与Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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