Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...