javascript 查找可用内存

发布于 2024-08-16 21:25:23 字数 631 浏览 4 评论 0原文

我们先明确一下:这不是内存泄漏的问题! 我有一个页面,允许用户输入一些数据和 JavaScript 来处理这些数据并生成结果。 JavaScript 在 DIV 上产生增量输出,如下所示:

(function()
{
   var newdiv = document.createElement("div");
   newdiv.innerHTML = produceAnswer();
   result.appendChild(newdiv);
   if (done) {
      return;
   } else {
      setTimeout(arguments.callee, 0);
   }
})();

在某些情况下,计算将产生如此多的数据,以至于 IE8 将失败 与此消息:

处理过多数据时存储空间不足

问题是: 有什么方法可以计算出多少数据算数据过多吗?

正如我所说,没有任何错误需要解决。这是真正的内存不足,因为计算 需要创建太多的 html 元素。

我的想法是在执行计算之前运行一个函数,以提前计算出浏览器是否会成功。但要做到这一点,以通用的方式,我认为我需要找到浏览器可用的内存。

欢迎任何建议。

Let's make it immediately clear: this is not a question about memory leak!
I have a page which allows the user to enter some data and a JavaScript to handle this data and produce a result.
The JavaScript produces incremental outputs on a DIV, something like this:

(function()
{
   var newdiv = document.createElement("div");
   newdiv.innerHTML = produceAnswer();
   result.appendChild(newdiv);
   if (done) {
      return;
   } else {
      setTimeout(arguments.callee, 0);
   }
})();

Under certain circumstances the computation will produce so much data that IE8 will fail
with this message:

not enough storage when dealing with too much data

The question is:
is there way I can work out how much data is too much data?

as I said there is no bug to solve. It's a genuine out of memory because the computation
requires to create too many html elements.

My idea would be to run a function before executing the computation to work out ahead if the browser will succeed. But to do so, in a generic way, I think I need to find the memory available to my browser.

Any suggestion is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一人独醉 2024-08-23 21:25:24

JavaScript(在浏览器中)在沙箱中运行,这意味着它被隔离,无法访问可能导致安全问题的内容,例如本地文件、系统资源等 - 所以不,您无法检测内存使用情况。

正如其他答案所述,您可以通过在实现之间暂停或使用资源密集程度较低的代码来使浏览器的任务变得更容易,但每个浏览器都有其限制。

Javascript (in the browser) is run in a sandbox, which means that it is fenced-off from accessing things that could cause security issues such as local files, system resources etc - so no, you can't detect memory usage.

As the other answers state, you can make the task easier for the browser by pausing between implementations or using less resource-intensive code, but every browser has its limits.

冧九 2024-08-23 21:25:24

玩玩这个吧...

document.write(performance.memory.jsHeapSizeLimit+'<br><br>');
document.write(performance.memory.usedJSHeapSize+'<br><br>');
document.write(performance.memory.totalJSHeapSize);

Have a play with this...

document.write(performance.memory.jsHeapSizeLimit+'<br><br>');
document.write(performance.memory.usedJSHeapSize+'<br><br>');
document.write(performance.memory.totalJSHeapSize);
梦罢 2024-08-23 21:25:24

循环比递归使用更少的内存。

   do
   {
     var newdiv = document.createElement("div");
     newdiv.innerHTML = produceAnswer();
     result.appendChild(newdiv);
   } while (!done);

您还可以对生成的答案数量设置一些上限。

   var answerCount = 0;
   do
   {
     var newdiv = document.createElement("div");
     newdiv.innerHTML = produceAnswer();
     result.appendChild(newdiv);
   } while (!done && answerCount++ < 1000);

A loop will use less memory than recursion.

   do
   {
     var newdiv = document.createElement("div");
     newdiv.innerHTML = produceAnswer();
     result.appendChild(newdiv);
   } while (!done);

You could also put some upper limit on the number of answers produced.

   var answerCount = 0;
   do
   {
     var newdiv = document.createElement("div");
     newdiv.innerHTML = produceAnswer();
     result.appendChild(newdiv);
   } while (!done && answerCount++ < 1000);
毁梦 2024-08-23 21:25:24

我怀疑问题在于 0 超时延迟 - 它试图立即重新运行。尝试增加这个。

I suspect having a 0 timeout delay is the problem - it's trying to re-run instantly. Try increasing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文