24

I am debugging a javascript/html5 web app that uses a lot of memory. Occasionally I get an error message in the console window saying

"uncaught exception: out of memory".  

Is there a way for me to gracefully handle this error inside the app?

Ultimately I need to re-write parts of this to prevent this from happening in the first place.

user547794
  • 14,263
  • 36
  • 103
  • 152
  • FYI, there is a useful [AngularJS Batarang](https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk?hl=en) extension that can help with debugging performance problems. – alecxe Sep 18 '14 at 03:22
  • Can you show me your code? – Ramesh Rajendran Mar 09 '15 at 06:27

3 Answers3

5

You should calclulate size of your localStorage, window.localStorage is full

as a solution is to try to add something

var localStorageSpace = function(){
    var allStrings = '';
    for(var key in window.localStorage){
        if(window.localStorage.hasOwnProperty(key)){
            allStrings += window.localStorage[key];
        }
    }
    return allStrings ? 3 + ((allStrings.length*16)/(8*1024)) + ' KB' : 'Empty (0 KB)';
};

var storageIsFull = function () {
    var size = localStorageSpace(); // old size

    // try to add data
    var er;
    try {
         window.localStorage.setItem("test-size", "1");
    } catch(er) {}

    // check if data added
    var isFull = (size === localStorageSpace());
    window.localStorage.removeItem("test-size");

    return isFull;
}
5

I also got the same error message recently when working on a project having lots of JS and sending Json, but the solution which I found was to update input type="submit" attribute to input type="button". I know there are limitations of using input type="button"..> and the solution looks weird, but if your application has ajax with JS,Json data, you can give it a try. Thanks.

safian syed
  • 147
  • 1
  • 6
  • In case of **ajax call** I just changed input type="submit" to input type="button" , and worked for me – partho Nov 12 '16 at 05:34
1

Faced the same problem in Firefox then later I came to know I was trying to reload a HTML page even before setting up some data into local-storage inside if loop. So you need to take care of that one and also check somewhere ID is repeating or not.

But same thing was working great in Chrome. Maybe Chrome is more Intelligent.

Shasha
  • 669
  • 1
  • 8
  • 26