简体   繁体   中英

Error handling "Out of Memory Exception" in browser?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM