Rob*_*b W 131
内置流程模块有一个方法memoryUsage可以深入了解当前Node.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 }
Run Code Online (Sandbox Code Playgroud)
在这个简单的例子中,您可以看到分配一个10M元素的数组消费者大约80MB(看看heapUsed).
如果看一下V8的源代码(Array::New,Heap::AllocateRawFixedArray,FixedArray::SizeFor),那么你将看到由阵列中使用的存储器是一个固定值加上长度乘以一个指针的大小.后者在64位系统上是8个字节,这证实观察到的8 x 10 = 80MB的内存差异是有意义的.
Dam*_*ran 61
node-memwatch:检测并查找Node.JS代码中的内存泄漏.查看本教程在Node.js中跟踪内存泄漏
Voy*_*Voy 37
另外,如果您想了解全局内存而不是节点进程':
var os = require('os');
os.freemem();
os.totalmem();
Run Code Online (Sandbox Code Playgroud)
Shi*_*ule 31
如果您使用express.js框架,那么您可以使用express-status-monitor。它非常容易集成,并以图形格式提供 CPU 使用情况、内存使用情况、响应时间等。
Joe*_*oel 27
最初的记事本基本上已经死了.尝试使用memwatch-next,这似乎在现代版本的Node上运行良好.
l2y*_*sho 10
您可以使用 node.js 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"
}
*/
Run Code Online (Sandbox Code Playgroud)
在 Linux/Unix(注意:Mac OS 是 Unix)上,使用top并按 M ( Shift+ M) 按内存使用情况对进程进行排序。
在 Windows 上使用任务管理器。
| 归档时间: |
|
| 查看次数: |
92257 次 |
| 最近记录: |