JavaScript infinite loops and Heap memory limit – fatal error: reached heap limit allocation failed – javascript heap out of memory

huangapple go评论67阅读模式
英文:

JavaScript infinite loops and Heap memory limit - fatal error: reached heap limit allocation failed - javascript heap out of memory

问题

I am trying to run some scripts that require a loop to run indefinitely (until a certain condition is met but this could take forever). The operations within the loop are very simple and do not create new variables, push elements to arrays or make new calls. Basically, no new resources are used on every loop, at least as far as I see it. However, I end up getting the infamous error:

fatal error: reached heap limit allocation failed - javascript heap out of memory

我试图运行一些需要无限循环的脚本(直到满足某个条件,但这可能永远不会发生)。循环内的操作非常简单,不会创建新变量,不会将元素推送到数组中,也不会进行新的调用。至少在我看来,每次循环都不会使用新资源。然而,我却遇到了臭名昭著的错误:

致命错误:达到堆限制,分配失败 - JavaScript 堆内存耗尽

I have been investigating memory usage in loops and for the sake of simplicity of my question, mine can be considered analogue to this:

我一直在研究循环中的内存使用情况,为了简化我的问题,我的情况可以类比于这个:

async function main(){
    let n = 5;
    while(1){
        console.log("number: " + n + "; squared: " + n**2);
        console.log(process.memoryUsage());
    }    
}

I found that the heap usage keeps growing on every loop despite "nothing" being made. I tried this same turning the loop into a recursive function with similar results. I am also familiar with changing the heap memory size allocation but that is not a solution for this case since I need this to run indefinitely.

我发现尽管“什么都没做”,堆使用量在每次循环中都在增加。我尝试将这个循环转换成递归函数,结果类似。我也了解如何更改堆内存大小分配,但这对这个情况不是解决方案,因为我需要它无限运行。

What coding formulation should I use to run a loop like this? What should I change to make this possible without the heap growing? I know a bit about the garbage collector and I can figure it is this feature what is causing the limitation but cannot really understand why. This thing I want to do seems to me a pretty straightforward need that many other people will have come across with and I cannot believe Java has just disabled it like this.

我应该使用什么编码方式来运行这样的循环?我应该做哪些改变,以使这个循环在不增加堆的情况下运行?我对垃圾收集器有一些了解,我可以想象是这个特性导致了限制,但我真的不明白为什么。我想做的事情对我来说似乎是一个非常简单的需求,许多其他人可能也会遇到,我无法相信 Java 就这样禁用了它。

英文:

I am trying to run some scripts that require a loop to run indefinitely (until a certain condition is met but this could take forever). The operations within the loop are very simple and do not create new variables, push elements to arrays or make new calls. Basically, no new resources are used on every loop, at least as far as I see it. However, I end up getting the infamous error:

fatal error: reached heap limit allocation failed - javascript heap out of memory

I have been investigating memory usage in loops and for the sake of simplicity of my question, mine can be considered analogue to this:

async function main(){
    let n = 5;
    while(1){
        console.log("number: " + n + "; squared: " + n**2);
        console.log(process.memoryUsage());
    }    
}

I found that the heap usage keeps growing on every loop despite "nothing" being made. I tried this same turning the loop into a recursive function with similar results. I am also familiar with changing the heap memory size allocation but that is not a solution for this case since I need this to run indefinitely.

What coding formulation should I use to run a loop like this? What should I change to make this possible without the heap growing? I know a bit about the garbage collector and I can figure it is this feature what is causing the limitation but cannot really understand why. This thing I want to do seems to me a pretty straightforward need that many other people will have come across with and I cannot believe Java has just disabled it like this.

Thanks for your answers

答案1

得分: 2

I would strongly suggest to use setInterval or requestAnimationFrame. JavaScript is single-threaded, which may cause infinite loops (even in async functions) to potentially block the execution of other code.

我强烈建议使用setInterval或requestAnimationFrame。JavaScript是单线程的,这可能导致无限循环(甚至在异步函数中)潜在地阻塞其他代码的执行。

I hope this will fix your issue.

我希望这能解决你的问题。

let n = 5;

function someFunction() {
console.log("number: " + n + "; squared: " + n ** 2);
console.log(process.memoryUsage());
}

someFunction();
setInterval(performTask, 1000);

英文:

I would strongly suggest to use setInterval or requestAnimationFrame. JavaScript is single-threaded, which may cause infinite loops (even in async functions) to potentially block the execution of other code.

I hope this will fix your issue.

let n = 5;

function someFunction() {
  console.log("number: " + n + "; squared: " + n ** 2);
  console.log(process.memoryUsage());
}

someFunction();
setInterval(performTask, 1000);

huangapple
  • 本文由 发表于 2023年5月23日 01:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定