Can you catch a memory error in JavaScript?

I am creating a mobile web application that will rely on caching. If my cache uses a lot of memory, I see this message in mobile Safari ...

"A problem has occurred with this webpage so that it reloads."

Without intervention, the page will reload and do the same again several times until it refuses.

Are there any events that I can capture, heap information that I can control, or parameters that I can change to create a more reliable caching system than a forced page reload?

Chrome has window.performace.memory, but I can’t find anything related to solving my problem in mobile Safari. The Try / catch and onBeforeUnload statements do not prevent the page from loading or provide the ability to clear / reduce the cache.

+4
source share
1 answer

Does it help?

Error handling and memory exception in browser?

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;

}

+1
source

All Articles