javascript to find memory available
Asked Answered
H

4

11

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.

Heer answered 8/1, 2010 at 12:55 Comment(6)
with so much data, can't the user even cope?Gisele
Does it work to surround the code with try/catch?Baulk
memory.performance.usedJSHeapSize works just fine in Chrome now.Tabescent
I think you mean: window.performance.memory.usedJSHeapSizePostgraduate
Also note that it's Chrome-only. See: docs.webplatform.org/wiki/apis/timing/properties/memoryPostgraduate
... and this thread suggests that it may no longer work properly: groups.google.com/forum/#!topic/google-chrome-developer-tools/…Postgraduate
S
8

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.

Sylphid answered 8/1, 2010 at 13:25 Comment(1)
thanks for your answer. I suspected that I could not detect memory usage.Heer
S
2

Have a play with this...

document.write(performance.memory.jsHeapSizeLimit+'<br><br>');
document.write(performance.memory.usedJSHeapSize+'<br><br>');
document.write(performance.memory.totalJSHeapSize);
Stricklin answered 31/3, 2015 at 9:16 Comment(1)
Unfortunately, performance.memory is a proprietary Chrome stuff.Blandish
H
1

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);
Helsell answered 8/1, 2010 at 13:20 Comment(2)
Passing arguments.callee to setTimeout invokes the function again, but you are correct, it is not strictly recursion because the setTimeout allows the calling function to return (at least it should, but perhaps the delay=0 is optimized to call immediately, in which case it would be recursion). Using a loop with a counter will still help and the code will be easier to read.Helsell
even with delay=0 you don't have recursion.Heer
L
0

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

Lynnalynne answered 8/1, 2010 at 12:57 Comment(1)
no it's not. the 0 timeout delay is a well known technique. google around if you don't believe me.Heer

© 2022 - 2024 — McMap. All rights reserved.