节点中分配变量的内存使用情况(使用 restify)
Memory usage of assigned variables in node (with restify)
setInterval(() => {
var mem = process.memoryUsage();
console.info("Memory used: ", mem.heapUsed.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "));
}, 1000);
server.get("/", function(req, res, next) {
var test = new Array(1e7);
test = null;
res.send(200);
next();
});
间隔计时器不断报告或多或少稳定的内存使用情况,但在我向 restify-endpoint 发出请求后,它增加了 80 MB(自然,因为我分配了一个 1000 万个元素的数组),但是然后它留在那里。这是否意味着即使我将 null 分配给变量后大数组仍在内存中?
Memory used: 82 645 408
Memory used: 82 647 240
Memory used: 82 649 072
Memory used: 82 650 904
Memory used: 82 652 736
Memory used: 163 126 464
Memory used: 163 136 128
Memory used: 163 137 968
Memory used: 163 139 808
Memory used: 163 141 648
Memory used: 163 143 488
Memory used: 163 145 328
这是一个非常简化的示例,最终目标是避免 api 端点中使用的对象在不再使用时保留在内存中。
如果您在 V8 garbage collection 上查看这篇文章,您会看到:
Large-object-space: This space contains objects which are larger than
the size limits of other spaces. Each object gets its own mmap'd
region of memory. Large objects are never moved by the garbage
collector.
我不知道 "large object," 的具体构成,但 1000 万个数组似乎符合描述。问题是,如果大对象永远不会被垃圾收集器移动,那么它们是如何清理的?我还没有找到答案。
但是,如果您在终端中启动节点然后重复运行此命令
var myarr = new Array(1e7)
你会看到内存确实增加了,但只是增加到一定程度。我发现一旦内存使用超过 500MB,就会发生某种垃圾收集,内存使用回落到 251MB。因此,虽然看起来您的大型阵列永远不会消失,但实际上 V8 会在某个时候对此做些事情。
setInterval(() => {
var mem = process.memoryUsage();
console.info("Memory used: ", mem.heapUsed.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " "));
}, 1000);
server.get("/", function(req, res, next) {
var test = new Array(1e7);
test = null;
res.send(200);
next();
});
间隔计时器不断报告或多或少稳定的内存使用情况,但在我向 restify-endpoint 发出请求后,它增加了 80 MB(自然,因为我分配了一个 1000 万个元素的数组),但是然后它留在那里。这是否意味着即使我将 null 分配给变量后大数组仍在内存中?
Memory used: 82 645 408
Memory used: 82 647 240
Memory used: 82 649 072
Memory used: 82 650 904
Memory used: 82 652 736
Memory used: 163 126 464
Memory used: 163 136 128
Memory used: 163 137 968
Memory used: 163 139 808
Memory used: 163 141 648
Memory used: 163 143 488
Memory used: 163 145 328
这是一个非常简化的示例,最终目标是避免 api 端点中使用的对象在不再使用时保留在内存中。
如果您在 V8 garbage collection 上查看这篇文章,您会看到:
Large-object-space: This space contains objects which are larger than the size limits of other spaces. Each object gets its own mmap'd region of memory. Large objects are never moved by the garbage collector.
我不知道 "large object," 的具体构成,但 1000 万个数组似乎符合描述。问题是,如果大对象永远不会被垃圾收集器移动,那么它们是如何清理的?我还没有找到答案。
但是,如果您在终端中启动节点然后重复运行此命令
var myarr = new Array(1e7)
你会看到内存确实增加了,但只是增加到一定程度。我发现一旦内存使用超过 500MB,就会发生某种垃圾收集,内存使用回落到 251MB。因此,虽然看起来您的大型阵列永远不会消失,但实际上 V8 会在某个时候对此做些事情。