gpt4 book ai didi

javascript - 简单的 nodejs 应用程序中的内存泄漏

转载 作者:行者123 更新时间:2023-11-30 15:47:27 25 4
gpt4 key购买 nike

为了好玩和尝试 nodejs,我编写了一个非常非常简单的程序来测试 Collat​​z 猜想的荒谬数量。从理论上讲,这应该没问题。我遇到的问题是这个 super 简单的代码有内存泄漏,我无法确定原因。

var step;
var numberOfSteps;
for (var i = 0; i < 100000000000000; i++) {
step = i;
numberOfSteps = 0;
while (step !== 1) {
if (step%2 === 0)
step /= 2;
else
step = 3 * step + 1;
numberOfSteps++;
}
console.log("" + i + ": " + numberOfSteps + " steps.");
}

我已经尝试了循环内外的变量。我试过在循环结束时将它们归零。没有任何改变内存泄漏。

最佳答案

调查一下我的核心转储:

<--- Last few GCs --->

131690 ms: Scavenge 1398.1 (1458.1) -> 1398.1 (1458.1) MB, 1.3 / 0 ms (+ 2.8 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
132935 ms: Mark-sweep 1398.1 (1458.1) -> 1398.1 (1458.1) MB, 1245.0 / 0 ms (+ 3.7 ms in 2 steps since start of marking, biggest step 2.8 ms) [last resort gc].
134169 ms: Mark-sweep 1398.1 (1458.1) -> 1398.1 (1458.1) MB, 1234.5 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x33083d8e3ac1 <JS Object>
1: /* anonymous */ [/user/projects/test.js:~1] [pc=0x557d307b271] (this=0x2a4a669d8341 <an Object with map 0xf8593408359>,exports=0x33083d804189 <undefined>,require=0x33083d804189 <undefined>,module=0x33083d804189 <undefined>,__filename=0x33083d804189 <undefined>,__dirname=0x33083d804189 <undefined>)
3: _compile [module.js:413] [pc=0x557d304d03c] (this=0x2a4a669d8431...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Aborted (core dumped)

这似乎是关于 console.log 的一个已知问题,根据 github 上的这个问题 https://github.com/nodejs/node/issues/3171

This is a known "issue" since writing to stdout in the case of a tty/console is async. So logging a lot of data very fast could very well cause a lot of writes to be buffered in memory if the tty/console cannot keep up.

关于javascript - 简单的 nodejs 应用程序中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com