javascript to find memory available

天涯浪子 提交于 2019-11-30 19:44:34
Adam Hopkinson

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...

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

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);

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!