Javascript 超出最大调用堆栈大小错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6095530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:13:17  来源:igfitidea点击:

Maximum call stack size exceeded error

javascripthtmlwebkitcallstackdwr

提问by testndtv

I am using a Direct Web Remoting (DWR) JavaScript library file and am getting an error only in Safari (desktop and iPad)

我正在使用 Direct Web Remoting (DWR) JavaScript 库文件,并且仅在 Safari(桌面和 iPad)中出现错误

It says

它说

Maximum call stack size exceeded.

超出了最大调用堆栈大小。

What exactly does this error mean and does it stop processing completely?

这个错误究竟是什么意思,它是否完全停止处理?

Also any fix for Safaribrowser (Actually on the iPad Safari, it says

还有Safari浏览器的任何修复(实际上在iPad Safari,它说

JS:execution exceeded timeout

JS:执行超过超时

which I am assuming is the same call stack issue)

我假设是相同的调用堆栈问题)

回答by alex

It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you hit the call stack limit.

这意味着在您的代码中的某个地方,您正在调用一个函数,该函数又调用另一个函数,依此类推,直到达到调用堆栈限制。

This is almost always because of a recursive function with a base case that isn't being met.

这几乎总是因为没有满足基本情况的递归函数。

Viewing the stack

查看堆栈

Consider this code...

考虑这个代码...

(function a() {
    a();
})();

Here is the stack after a handful of calls...

这是几次调用后的堆栈...

Web Inspector

网络检查员

As you can see, the call stack grows until it hits a limit: the browser hardcoded stack size or memory exhaustion.

如您所见,调用堆栈会不断增长,直到达到限制:浏览器硬编码堆栈大小或内存耗尽。

In order to fix it, ensure that your recursive function has a base case which is able to be met...

为了修复它,请确保您的递归函数具有能够满足的基本情况......

(function a(x) {
    // The following condition 
    // is the base case.
    if ( ! x) {
        return;
    }
    a(--x);
})(10);

回答by lucygenik

You can sometimes get this if you accidentally import/embed the same JavaScript file twice, worth checking in your resources tab of the inspector.

如果您不小心导入/嵌入同一个 JavaScript 文件两次,有时会得到这个,值得在检查器的资源选项卡中检查。

回答by FKasa

In my case, I was sending input elements instead of their values:

就我而言,我发送的是输入元素而不是它们的值:

$.post( '',{ registerName: $('#registerName') } )

Instead of:

代替:

$.post( '',{ registerName: $('#registerName').val() } )

This froze my Chrome tab to a point it didn't even show me the 'Wait/Kill' dialog for when the page became unresponsive...

这将我的 Chrome 标签冻结到了一个点,它甚至没有向我显示页面无响应时的“等待/终止”对话框......

回答by Aaron Digulla

There is a recursive loop somewhere in your code (i.e. a function that eventually calls itself again and again until the stack is full).

在您的代码中某处有一个递归循环(即最终一次又一次地调用自身直到堆栈已满的函数)。

Other browsers either have bigger stacks (so you get a timeout instead) or they swallow the error for some reason (maybe a badly placed try-catch).

其他浏览器要么有更大的堆栈(所以你会得到一个超时)或者他们出于某种原因吞下错误(可能是一个错误放置的 try-catch)。

Use the debugger to check the call stack when the error happens.

发生错误时使用调试器检查调用堆栈。

回答by Simon_Weaver

The problem with detecting stackoverflows is sometimes the stack trace will unwind and you won't be able to see what's actually going on.

检测 stackoverflows 的问题是有时堆栈跟踪会展开,您将无法看到实际发生了什么。

I've found some of Chrome's newer debugging tools useful for this.

我发现 Chrome 的一些较新的调试工具对此很有用。

Hit the Performance tab, make sure Javascript samplesare enabled and you'll get something like this.

点击Performance tab,确保Javascript samples已启用,你会得到这样的东西。

It's pretty obvious where the overflow is here! If you click on extendObjectyou'll be able to actually see the exact line number in the code.

溢出在哪里很明显!如果您单击,extendObject您将能够实际看到代码中的确切行号。

enter image description here

在此处输入图片说明

You can also see timings which may or may not be helpful or a red herring.

您还可以查看可能有用也可能没有帮助的时间安排或红鲱鱼。

enter image description here

在此处输入图片说明



Another useful trick if you can't actually find the problem is to put lots of console.logstatements where you think the problem is. The previous step above can help you with this.

如果您实际上无法找到问题,另一个有用的技巧是console.log在您认为问题所在的地方放置大量语句。上面的上一步可以帮助您解决这个问题。

In Chrome if you repeatedly output identical data it will display it like this showing where the problem is more clearly. In this instance the stack hit 7152 frames before it finally crashed:

在 Chrome 中,如果你重复输出相同的数据,它会像这样显示问题更清楚的地方。在这种情况下,堆栈在最终崩溃之前达到了 7152 帧:

enter image description here

在此处输入图片说明

回答by Nate Glenn

In my case, I was converting a large byte array into a string using the following:

就我而言,我使用以下命令将大字节数组转换为字符串:

String.fromCharCode.apply(null, new Uint16Array(bytes))

bytescontained several million entries, which is too big to fit on the stack.

bytes包含数百万个条目,太大而无法放入堆栈。

回答by Shahdat

In my case, click event was propagating on child element. So, I had to put the following:

就我而言,单击事件是在子元素上传播的。所以,我不得不提出以下几点:

e.stopPropagation()

e.stopPropagation()

on click event:

点击事件:

 $(document).on("click", ".remove-discount-button", function (e) {
           e.stopPropagation();
           //some code
        });
 $(document).on("click", ".current-code", function () {
     $('.remove-discount-button').trigger("click");
 });

Here is the html code:

这是html代码:

 <div class="current-code">                                      
      <input type="submit" name="removediscountcouponcode" value="
title="Remove" class="remove-discount-button">
   </div>

回答by chim

Check the error details in the Chrome dev toolbar console, this will give you the functions in the call stack, and guide you towards the recursion that's causing the error.

检查 Chrome 开发工具栏控制台中的错误详细信息,这将为您提供调用堆栈中的函数,并引导您进行导致错误的递归。

回答by Dr. Dama

If you need a infinite process/recursion running for some reason, you can use a webworker in a seperate thread. http://www.html5rocks.com/en/tutorials/workers/basics/

如果出于某种原因需要无限进程/递归运行,则可以在单独的线程中使用 webworker。 http://www.html5rocks.com/en/tutorials/workers/basics/

if you want to manipulate dom elements and redraw, use animation http://creativejs.com/resources/requestanimationframe/

如果要操作 dom 元素并重绘,请使用动画 http://creativejs.com/resources/requestanimationframe/

回答by ghayes

Nearly every answer here states that this can only be caused by an infinite loop. That's not true, you could otherwise over-run the stack through deeply nested calls (not to say that's efficient, but it's certainly in the realm of possible). If you have control of your JavaScript VM, you can adjust the stack size. For example:

这里几乎每个答案都表明这只能由无限循环引起。事实并非如此,否则您可能会通过深度嵌套的调用来溢出堆栈(并不是说这很有效,但肯定是在可能的范围内)。如果您可以控制 JavaScript VM,则可以调整堆栈大小。例如:

node --stack-size=2000

node --stack-size=2000

See also: How can I increase the maximum call stack size in Node.js

另请参阅:如何增加 Node.js 中的最大调用堆栈大小