Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you catch an out of memory error in JavaScript

I'm building a mobile web app that will rely on caching. If my cache uses to much memory I'm seeing this message in mobile Safari...

"A problem occurred with this webpage so it was reloaded".

Without intervention the page will reload and do the same thing over again a few times until it gives up.

Are there events I can capture, heap information I can monitor or settings that I can change to build a caching system that is more resilient than forcing page reloads?

Chrome has window.performace.memory but I can't seem to find anything related to solving my issue in mobile Safari. Try/catch statements and onBeforeUnload events don't prevent the page loads or provide an opportunity to clear/reduce the cache.

like image 396
Tyler Larson Avatar asked Jun 08 '16 16:06

Tyler Larson


People also ask

How do I fix JavaScript out of memory?

Open the Start menu, search for Advanced System Settings, and select the Best match. In the Variable name field enter NODE_OPTIONS. In the Variable value field enter --max-old-space-size=4096. This value will allocate 4GB of virtual memory to Node.

Can you have memory leak in JS?

JavaScript code can experience memory leaks by keeping hidden references to objects. Hidden references can cause memory leaks in many unexpected ways.

What is JavaScript Heap out of memory?

This generally occurs on larger projects where the default amount of memory allocated by Node (1.5gb) is insufficient to complete the command successfully.

How do I fix a process out of memory exception in node JS?

This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).


1 Answers

Does this help ?

Error handling "Out of 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;

}

like image 142
Basit Anwer Avatar answered Sep 22 '22 17:09

Basit Anwer