admin管理员组

文章数量:1191728

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.

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.

Share Improve this question asked Sep 18, 2014 at 3:18 user547794user547794 14.5k37 gold badges108 silver badges152 bronze badges 2
  • FYI, there is a useful AngularJS Batarang extension that can help with debugging performance problems. – alecxe Commented Sep 18, 2014 at 3:22
  • Can you show me your code? – Ramesh Rajendran Commented Mar 9, 2015 at 6:27
Add a comment  | 

3 Answers 3

Reset to default 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;
}

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.

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.

本文标签: javascriptError handling quotOut of Memory Exceptionquot in browserStack Overflow