我正在使用直接Web Remoting (DWR) JavaScript库文件,只在Safari(桌面和iPad)中得到一个错误

它说

超过最大调用堆栈大小。

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

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

JS:执行超时

我认为这是相同的调用堆栈问题)


当前回答

这也会导致最大调用堆栈大小超过错误:

var items = [];
[].push.apply(items, new Array(1000000)); //Bad

我也一样:

items.push(...new Array(1000000)); //Bad

来自Mozilla文档:

But beware: in using apply this way, you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.

所以尝试:

var items = [];
var newItems = new Array(1000000);
for(var i = 0; i < newItems.length; i++){
  items.push(newItems[i]);
}

其他回答

这也会导致最大调用堆栈大小超过错误:

var items = [];
[].push.apply(items, new Array(1000000)); //Bad

我也一样:

items.push(...new Array(1000000)); //Bad

来自Mozilla文档:

But beware: in using apply this way, you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.

所以尝试:

var items = [];
var newItems = new Array(1000000);
for(var i = 0; i < newItems.length; i++){
  items.push(newItems[i]);
}

我使用React-Native 0.61.5以及(npm 6.9.0和节点10.16.1)

当我安装任何新的库在项目我得到了一个

(例如NPM install @react-navigation/native——save)

最大调用堆栈大小超过错误

为此,我尽力了

Sudo NPM缓存清理-force

(注:-以下命令通常需要1 ~ 2分钟 取决于你的NPM缓存大小)

dtTable.dataTable({
    sDom: "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>",
    "processing": true,
    "serverSide": true,
    "order": [[6, "desc"]],
    "columnDefs": [
        {className: "text-right", "targets": [2, 3, 4, 5]}
    ],
    "ajax": {
        "url": "/dt",
        "data": function (d) {
            d.loanRef = loanRef;
        }
    },
    "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
        var editButton = '';
        // The number of columns to display in the datatable
        var cols = 8;
        // Th row element's ID
        var id = aData[(cols - 1)];
    }
});

在上面的数据函数中,我使用了相同的名称d.loanRef = loanRef,但没有创建变量loanRef,因此递归地声明了自己。

解决方案:声明一个loanRef变量,或者更好的是,使用与d.loanRef不同的名称。

这个问题可能是因为递归调用没有任何终止的基本条件。

就像在我的情况下,如果你看到下面的代码,我有相同的API调用方法和方法,我用来执行操作后的API调用。

const getData = async () => {
try {
  const response = await getData(props.convID);
  console.log("response", response);
 } catch (err) {
  console.log("****Error****", err);
}
};

基本上,解决方案就是删除这个递归调用。

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

我发现Chrome的一些较新的调试工具在这方面很有用。

点击Performance选项卡,确保Javascript样本被启用,你会得到类似这样的结果。

溢出的地方很明显!如果单击extendObject,您将能够看到代码中确切的行号。

你也可以看到时间,可能有帮助,也可能没有帮助,或者转移注意力。


另一个有用的技巧是,如果你不能真正发现问题,在你认为问题所在的地方放置大量的console.log语句。上面的步骤可以帮助你做到这一点。

在Chrome中,如果你反复输出相同的数据,它会像这样显示问题更清楚。在这个例子中,堆栈在最终崩溃之前击中了7152帧: