javascript 查找可用内存
我们先明确一下:这不是内存泄漏的问题! 我有一个页面,允许用户输入一些数据和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
玩玩这个吧...
Have a play with this...
循环比递归使用更少的内存。
您还可以对生成的答案数量设置一些上限。
A loop will use less memory than recursion.
You could also put some upper limit on the number of answers produced.
我怀疑问题在于 0 超时延迟 - 它试图立即重新运行。尝试增加这个。
I suspect having a 0 timeout delay is the problem - it's trying to re-run instantly. Try increasing this.