JavaScript 内存限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2936782/
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 02:34:32  来源:igfitidea点击:

JavaScript memory limit

javascriptmemorybrowser

提问by D?nu

Is there a maximum amount of data a JavaScript application can store?

JavaScript 应用程序可以存储的数据量是否有上限?

I guess this is handled by the browser and each one has its limitation?

我猜这是由浏览器处理的,每个都有其局限性?

If there isn't a limit, will a page file be created? If so, wouldn't that be insecure?

如果没有限制,是否会创建一个页面文件?如果是这样,那岂不是不安全?

采纳答案by Pascal Thivent

AFAIK, there is no upper limit, your script can basically use memory until the system runs out of memory (including swap). No upper limit doesn't mean you have to eat it all, users may not like it.

AFAIK,没有上限,您的脚本基本上可以使用内存,直到系统耗尽内存(包括交换)。没有上限并不意味着你必须吃完它,用户可能不喜欢它。

回答by John Zabroski

In Chrome and Chromium OS, the memory limit is defined by the browser, and you can inspect the limit with the following command in the Developer Tools command-line by hitting F12:

在 Chrome 和 Chromium OS 中,内存限制由浏览器定义,您可以在开发人员工具命令行中按 F12 使用以下命令检查限制:

> window.performance.memory.jsHeapSizeLimit
1090519040

On my Windows 10 OS, it is about 1 GB.

在我的 Windows 10 操作系统上,它大约是 1 GB。

On Chrom(e/ium), you can get around the heap size limit by allocating native arrays:

在 Chrom(e/ium) 上,您可以通过分配本机数组来绕过堆大小限制:

var target = []
while (true) {
    target.push(new Uint8Array(1024 * 1024)); // 1Meg native arrays
}

This crashes the tab at around 2GB, which happens very rapidly. After that Chrom(e/ium) goes haywire, and repeating the test is not possible without restarting the browser.

这会使选项卡在 2GB 左右崩溃,这发生得非常快。在那之后 Chrom(e/ium) 失控了,如果不重新启动浏览器就不可能重复测试。

I also recommend reading TrackJS's blogpost about Monitoring JavaScript Memorybefore you get deep into the weeds trying to diagnose or measure anything memory related in the browser.

我还建议您在深入研究尝试诊断或测量浏览器中任何内存相关的杂草之前阅读 TrackJS 的关于监控 JavaScript 内存的博文。

You can also search comp.lang.javascript for javascript memory limit.

您还可以在 comp.lang.javascript 中搜索javascript memory limit

See also these Stack Overflow posts:

另请参阅这些 Stack Overflow 帖子:

  1. Maximum size of an Array in Javascript, which suggests you can store up to 232-1 = 4,294,967,295 = 4.29 billion elements.

  2. Maximum number of arguments a JavaScript function can accept

  1. Javascript 中数组的最大大小,这表明您最多可以存储 2 32-1 = 4,294,967,295 = 42.9 亿个元素。

  2. JavaScript 函数可以接受的最大参数数

There is additional knowledge on the JS9 astronomical image display library website: Dealing with Memory Limitations.

JS9 天文图像显示库网站上还有其他知识:处理内存限制

(I was trying to find a good answer, and the "there is no upper limit" answer provided here was just silly to me. I cannot run into a production issue for a multi-million dollar project and say to management, "Well, I assumed there is no upper limit and everything would be okay." Try to do a proof-of-concept, e.g. loading lots of combobox controls in your JavaScript UI framework of choice, etc. You may discover your framework has some performance degradation.)

(我试图找到一个好的答案,这里提供的“没有上限”的答案对我来说只是愚蠢的。我不能遇到数百万美元项目的生产问题并向管理层说,“好吧,我假设没有上限,一切都会好起来的。”尝试进行概念验证,例如在您选择的 JavaScript UI 框架中加载大量组合框控件等。您可能会发现您的框架性能有所下降。 )

Here are some components that I've found scale very well both in CPU performance and memory performance:

以下是我发现在 CPU 性能和内存性能方面都具有很好扩展性的一些组件:

  1. Microsoft Monaco editor
    • This is used by several commercial projects:
      1. Postman, as of v7.1.1-canary08
      2. VS Code
  1. 微软摩纳哥编辑
    • 这被几个商业项目使用:
      1. 邮递员,从 v7.1.1-canary08 开始
      2. VS代码

Here are some examples of frameworks with well-known performance degradation:

以下是一些众所周知的性能下降的框架示例:

  1. Angular: Poor change detection approach.
    • For each async event, compare each of the bindings (Model-Dom binding) to its old value to decide if to re-render.
      1. NG1: >2500 watchers, performance grinds to a halt
      2. NG2: the same problem remains but you have a long tiring workaround: Switch to immutables and spread ChangeDetectionStrategy.onPush all over your app to turn off the default problematic strategy
  2. React
    • Again, Immutable collections of JS objects only scale so far.
      1. create-react-app internally uses Immutable.JS, and Immutable.JS can only create about 500k immutable collections before it dies.
  1. Angular:糟糕的变化检测方法。
    • 对于每个异步事件,将每个绑定(Model-Dom 绑定)与其旧值进行比较,以决定是否重新渲染。
      1. NG1:> 2500 名观察者,性能停滞不前
      2. NG2:同样的问题仍然存在,但你有一个很长累人的解决方法:切换到不可变并在你的应用程序中传播 ChangeDetectionStrategy.onPush 以关闭默认的有问题的策略
  2. 反应
    • 同样,JS 对象的不可变集合目前只能扩展。
      1. create-react-app 内部使用的是 Immutable.JS,而 Immutable.JS 在它死之前只能创建大约 500k 个不可变集合。

Here are some other things to think about:

这里有一些其他的事情需要考虑:

  1. Use array.slice for manipulating arraysto minimize additional array allocations; array.slice will modify the array in place, which will reduce garbage collection and overall heap size.
  1. 使用array.slice 操作数组以最小化额外的数组分配;array.slice 将就地修改数组,这将减少垃圾收集和整体堆大小。

回答by Tel

Firefox supports the option "javascript.options.mem.max" and if you search on that you can find discussions about sensible values that people have found workable.

Firefox 支持选项“ javascript.options.mem.max”,如果您搜索该选项,您可以找到有关人们认为可行的合理值的讨论。

Not sure how many people can be bothered going in there and setting it, but speaking for myself I set it to 128000 (which is 128M).

不知道有多少人会费心去那里设置它,但就我自己而言,我将它设置为 128000(即 128M)。

回答by MonicaEgg

I think the memory limitation is from the browser. We can use the DevTools to figure that out. Like in chrome, press F12 and enter window.performance.memory you can see the memory info.

我认为内存限制来自浏览器。我们可以使用 DevTools 来解决这个问题。就像在 chrome 中一样,按 F12 并输入 window.performance.memory 您可以看到内存信息。

 window.performance.memory

enter image description here

在此处输入图片说明

回答by Secko

There are no memory limitations for a Javascript program. Your script can hog all the RAM on your machine. However, it is not recommended to use up all the memory on users machine. If you are dealing with a lot of data I would suggest that you check out caching.

Javascript 程序没有内存限制。您的脚本可以占用您机器上的所有 RAM。但是,不建议用完用户机器上的所有内存。如果您正在处理大量数据,我建议您检查缓存。

回答by yolisa myataza

This may vary between different web browsers, from observation, Iv'e picked up with Chrome(ver. 79.0.3945.130) the maximum is 2Gb per task. The Chrome debugger will pause the web page as the "Paused before potential out-of-memory crash" message comes up.

这可能因不同的 Web 浏览器而异,根据观察,我使用 Chrome(版本 79.0.3945.130)获得的最大容量为每个任务 2Gb。当出现“在潜在的内存不足崩溃之前暂停”消息时,Chrome 调试器将暂停网页。

However, the debugger does allow one to resume web page operation(usually resulting in web page crash) but if it doesn't crash, the memory used can climb well above 2Gb, the page will generally perform slower beyond this point. It seems as if the 2Gb is set on a per task basis.

然而,调试器确实允许恢复网页操作(通常会导致网页崩溃),但如果它没有崩溃,则使用的内存可以远远超过 2Gb,超过这一点后页面通常会执行得更慢。似乎 2Gb 是基于每个任务设置的。

ie: I have developed a real time web based Log viewer that displays the Log info, read from some local log files. One can request what percentage of the log file to view. Some of the Log files are large and can contain 100 000 plus lines. When requesting to view all those lines Chrome will crash, but if I request to view those lines 10 000 lines at a time, no problem at all. this brings me to the "2Gb max per task" conclusion.

即:我开发了一个基于 Web 的实时日志查看器,它显示日志信息,从一些本地日志文件中读取。可以请求查看日志文件的百分比。一些日志文件很大,可以包含 100 000 多行。当请求查看所有这些行时,Chrome 会崩溃,但如果我请求一次查看这些行 10 000 行,则完全没有问题。这让我得出“每个任务最大 2Gb”的结论。

This is only a theory, i stand corrected. Refer to the images attached

这只是一个理论,我站纠正。请参阅所附图片

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明