如何监控Node.js的内存使用情况?

185

我该如何监控Node.js的内存使用情况?

5个回答

233

内置的process模块有一个方法memoryUsage,可以提供当前 Node.js 进程的内存使用情况。以下是在 Node v0.12.2 上 64 位系统中的示例:

$ node --expose-gc
> process.memoryUsage();  // Initial usage
{ rss: 19853312, heapTotal: 9751808, heapUsed: 4535648 }
> gc();                   // Force a GC for the baseline.
undefined
> process.memoryUsage();  // Baseline memory usage.
{ rss: 22269952, heapTotal: 11803648, heapUsed: 4530208 }
> var a = new Array(1e7); // Allocate memory for 10m items in an array
undefined
> process.memoryUsage();  // Memory after allocating so many items
{ rss: 102535168, heapTotal: 91823104, heapUsed: 85246576 }
> a = null;               // Allow the array to be garbage-collected
null
> gc();                   // Force GC (requires node --expose-gc)
undefined
> process.memoryUsage();  // Memory usage after GC
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4528072 }
> process.memoryUsage();  // Memory usage after idling
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4753376 }

在这个简单的例子中,你可以看到分配一个包含1000万个元素的数组大约会消耗80MB的内存(查看heapUsed)。
如果你查看V8的源代码(Array::New, Heap::AllocateRawFixedArray, FixedArray::SizeFor),那么你会看到一个数组使用的内存是一个固定值,再加上长度乘以指针的大小。后者在64位系统上为8个字节,这说明观察到的内存差异为8 x 10 = 80MB是有道理的。


1
@MestreSan,哪个版本的Node不需要使用--expose-gc来调用gc函数? - Rob W
4
我从未说过你需要使用--expose-gc来执行process.memoryUsage()。在回答中,使用了gc()(需要--expose-gc)来有规律地触发垃圾收集以便更容易地查看process.memoryUsage报告的内容。请注意不改变原文的意思,并尽可能使翻译通俗易懂。 - Rob W
这是一个很棒的答案,可以正确地衡量JS-Stuff。感谢您提供的答案。 - suther
你的工作真是大有可为。我刚才意识到,通过调用进程所公开的所有方法,可以帮助我创建一个更高效的应用程序。谢谢。 - Andrew
我相信这应该是被接受的答案。 - ΔO 'delta zero'

60

此外,如果您想了解全局内存而不是节点进程:

var os = require('os');

os.freemem();
os.totalmem();

查看文档


4
然而,freemem()并不等同于服务器上的可用内存。有没有其他方法可以找到可用内存而不是空闲内存? - Alex
这是另一个问题的答案。 - humkins
似乎 os.freemem() 函数使用的是可用内存,具体情况可以参考这个问题:https://github.com/nodejs/node/issues/23892 - richytong
@Alex Math.round(OS.totalmem() / 1024 / 1024 * 100) / 100-Math.round(OS.freemem() / 1024 / 1024 * 100) / 100 - undefined

55

您可以使用 node.js process.memoryUsage() 方法:

const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`;

const memoryData = process.memoryUsage();

const memoryUsage = {
  rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
  heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
  heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
  external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
};

console.log(memoryUsage);

/*
{
  "rss": "177.54 MB -> Resident Set Size - total memory allocated for the process execution",
  "heapTotal": "102.3 MB -> total size of the allocated heap",
  "heapUsed": "94.3 MB -> actual memory used during the execution",
  "external": "3.03 MB -> V8 external memory"
}
*/

30

如果你使用的是 express.js 框架,则可以使用 express-status-monitor。它非常易于集成,并以图形格式提供 CPU 使用率、内存使用率、响应时间等信息。

enter image description here


这很有用!可以在高层次上获得基本的见解。 - undefined

5
在 Linux/Unix(注意:Mac OS 是一个 Unix 系统)上使用 top 命令,并按 M 键(Shift+M)按照内存使用量排序进程。
在 Windows 上使用任务管理器。

@majidarif 前往 应用程序 > 实用工具,您会找到一个名为 活动监视器 的应用程序。那个相当于任务管理器。OS X 也有 top 命令。 - Ingwie Phoenix
4
在Linux上使用htop代替top,效果更佳。 - Ryan Shillington

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接