Javascript堆内存不足错误(Javascript heap out of memory error)

编程入门 行业动态 更新时间:2024-10-27 02:21:39
Javascript堆内存不足错误(Javascript heap out of memory error)

我试图运行以下while循环,但由于某种原因我得到一个JS堆内存错误:

while( typeof(apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p]) == 'undefined') { //create time delay of one second setTimeout(function(){ //put code to run after delay here if( typeof(apiResults[0].league.season.eventType[0].events[0].pbp[i+1].pbpDetails[0]) != 'undefined') { //this will run if the next inning has come through i = i+1; p = 0; } //this will also pass the while loop and go down to the actual code }, 3000); }

我知道逻辑是有缺陷的,但是现在我只在if语句为真的情况下运行它。

我收到以下错误:

<---最后几个GC --->

11450 ms:标记扫描1388.5(1433.0) - > 1388.5(1445.0)MB,834.3 / 0.0 ms(自标记开始以来,63步中+ 0.0 ms,最大步长0.0 ms)[分配失败] [清除可能不成功]。 12374 ms:标记扫描1400.2(1445.0) - > 1400.3(1445.0)MB,910.6 / 0.0 ms(自标记开始以来的188步中+ 0.1 ms,最大步长0.0 ms)[分配失败] [清除可能不成功]。

<--- JS stacktrace ---> Cannot get stack trace in GC. FATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory 1: node::Abort() [/usr/local/bin/node] 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]

I am attempting to run the following while loop, but for some reason am getting a JS heap out of memory error:

while( typeof(apiResults[0].league.season.eventType[0].events[0].pbp[i].pbpDetails[p]) == 'undefined') { //create time delay of one second setTimeout(function(){ //put code to run after delay here if( typeof(apiResults[0].league.season.eventType[0].events[0].pbp[i+1].pbpDetails[0]) != 'undefined') { //this will run if the next inning has come through i = i+1; p = 0; } //this will also pass the while loop and go down to the actual code }, 3000); }

I know the logic is flawed, but right now I am only running it in instances where the if statement will be true.

I receive the following error:

<--- Last few GCs --->

11450 ms: Mark-sweep 1388.5 (1433.0) -> 1388.5 (1445.0) MB, 834.3 / 0.0 ms (+ 0.0 ms in 63 steps since start of marking, biggest step 0.0 ms) [allocation failure] [scavenge might not succeed]. 12374 ms: Mark-sweep 1400.2 (1445.0) -> 1400.3 (1445.0) MB, 910.6 / 0.0 ms (+ 0.1 ms in 188 steps since start of marking, biggest step 0.0 ms) [allocation failure] [scavenge might not succeed].

<--- JS stacktrace ---> Cannot get stack trace in GC. FATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory 1: node::Abort() [/usr/local/bin/node] 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [/usr/local/bin/node]

最满意答案

我认为这不符合你的期望。 setTimeout接受你传递它的函数,在延迟之后,将函数添加到堆栈之后。 由于while循环阻塞,最终会出现一种奇怪的情况,即在while循环结束之前不会调用延迟函数。

var x = 0;
while (x<10) {
  x++;
  console.log('omg');
  setTimeout(function(){console.log('from timeout');},0);
} 
  
 

您将在上面看到的是循环将继续,直到在任何setTimeouts执行之前完成。 如果你的循环是无限的,那么它会创建一大堆需要在将来发生的调用,但不能因为它们被循环阻塞。

如果我不得不猜测,似乎你正在实施某种长期的民意调查。 如果那个东西是同步的(似乎不是),它可能更容易轮询它。 如果不是,那么你将不得不考虑异步处理它。 有回调,事件或承诺。

在等待事情发生的情况下,事件往往是你的朋友。 您可以创建自定义事件或使用框架来帮助传播。

I don't think this does what you would expect. setTimeout takes the function that you pass it and after the delay, adds the function to the stack after everything else thats there. Since the while loop blocks, you end up with a weird situation where the delayed function won't get called until the while loop finishes.

var x = 0;
while (x<10) {
  x++;
  console.log('omg');
  setTimeout(function(){console.log('from timeout');},0);
} 
  
 

What you will see above is that the loop will continue until it's done before any of the setTimeouts execute. If your loop is infinite then it creates a huge stack of calls that need to happen in the future but can't because they are blocked by the loop.

If I had to guess, it seems like you are implementing some sort of long polling thing. if that thing is synchronous (seems like it isn't) it could be easier to poll it. If it's not, then you will have to think about handling this asynchronously. Either with callbacks, events or promises.

In a situation where you are waiting for something to happen events are often your friend. You can create custom events or use frameworks to help propagate for you.

更多推荐

本文发布于:2023-07-24 00:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   内存不足   Javascript   error   memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!