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

时间:2022-04-12 21:25:33

How can I monitor the memory usage of Node.js?

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

5 个解决方案

#1


49  

node-memwatch : detect and find memory leaks in Node.JS code. Check this tutorial Tracking Down Memory Leaks in Node.js

节点内存监视:检测和发现节点中的内存泄漏。JS代码。检查本教程跟踪Node.js中的内存泄漏

#2


103  

The built-in process module has a method memoryUsage that offers insight in the memory usage of the current Node.js process. Here is an example from in Node v0.12.2 on a 64-bit system:

内置的过程模块有一个方法memoryUsage,它提供了对当前节点的内存使用情况的洞察。js的过程。下面是64位系统的Node v0.12.2中的一个例子:

$ 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 }

In this simple example, you can see that allocating an array of 10M elements consumers approximately 80MB (take a look at heapUsed).
If you look at V8's source code (Array::New, Heap::AllocateRawFixedArray, FixedArray::SizeFor), then you'll see that the memory used by an array is a fixed value plus the length multiplied by the size of a pointer. The latter is 8 bytes on a 64-bit system, which confirms that observed memory difference of 8 x 10 = 80MB makes sense.

在这个简单的示例中,您可以看到分配一个包含10M元素的数组,占用大约80MB(请看heapUsed)。如果查看V8的源代码(数组:New, Heap:::AllocateRawFixedArray, FixedArray::SizeFor),那么您将看到数组使用的内存是一个固定值,加上长度乘以指针的大小。后者是在64位系统上的8字节,这证实了观察到的内存差异8 x 10 = 80MB是有意义的。

#3


29  

Also, if you'd like to know global memory rather than node process':

另外,如果你想知道全局内存而不是节点进程':

var os = require('os');

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

See documentation

看文档

#4


24  

The original memwatch is essentially dead. Try memwatch-next instead, which seems to be working well on modern versions of Node.

原始的memwatch基本上已经死了。试试memwatch-next,它似乎在现代版本的Node上运行得很好。

#5


1  

On Linux/Unix (note: Mac OS X is a Unix) use top and press M (shift+M) to sort processes by memory usage.

在Linux/Unix(注意:Mac OS X是Unix)上,使用top并按M (shift+M)按内存使用对进程进行排序。

On Windows use the Task Manager.

在Windows上使用任务管理器。

#1


49  

node-memwatch : detect and find memory leaks in Node.JS code. Check this tutorial Tracking Down Memory Leaks in Node.js

节点内存监视:检测和发现节点中的内存泄漏。JS代码。检查本教程跟踪Node.js中的内存泄漏

#2


103  

The built-in process module has a method memoryUsage that offers insight in the memory usage of the current Node.js process. Here is an example from in Node v0.12.2 on a 64-bit system:

内置的过程模块有一个方法memoryUsage,它提供了对当前节点的内存使用情况的洞察。js的过程。下面是64位系统的Node v0.12.2中的一个例子:

$ 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 }

In this simple example, you can see that allocating an array of 10M elements consumers approximately 80MB (take a look at heapUsed).
If you look at V8's source code (Array::New, Heap::AllocateRawFixedArray, FixedArray::SizeFor), then you'll see that the memory used by an array is a fixed value plus the length multiplied by the size of a pointer. The latter is 8 bytes on a 64-bit system, which confirms that observed memory difference of 8 x 10 = 80MB makes sense.

在这个简单的示例中,您可以看到分配一个包含10M元素的数组,占用大约80MB(请看heapUsed)。如果查看V8的源代码(数组:New, Heap:::AllocateRawFixedArray, FixedArray::SizeFor),那么您将看到数组使用的内存是一个固定值,加上长度乘以指针的大小。后者是在64位系统上的8字节,这证实了观察到的内存差异8 x 10 = 80MB是有意义的。

#3


29  

Also, if you'd like to know global memory rather than node process':

另外,如果你想知道全局内存而不是节点进程':

var os = require('os');

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

See documentation

看文档

#4


24  

The original memwatch is essentially dead. Try memwatch-next instead, which seems to be working well on modern versions of Node.

原始的memwatch基本上已经死了。试试memwatch-next,它似乎在现代版本的Node上运行得很好。

#5


1  

On Linux/Unix (note: Mac OS X is a Unix) use top and press M (shift+M) to sort processes by memory usage.

在Linux/Unix(注意:Mac OS X是Unix)上,使用top并按M (shift+M)按内存使用对进程进行排序。

On Windows use the Task Manager.

在Windows上使用任务管理器。