简体   繁体   中英

Detect memory exhaustion in the browser before it crashes

When a JavaScript client application uses too much memory, the browser will either crash or throw an exception that can't be recovered from or swap like it's the 80s.

Do browsers signal that they almost reached the available memory limit for a tab?

Ideally, I'd love to be able to catch an event that I can intercept in JavaScript when the browser is running low on memory in order to automatically fall back to a light version of the application or tell my users to go buy a new computer / phone.

I know Chrome Performance Tools allow imprecise querying of the used memory, which is a first step, but probably not enough to detect memory limitations.

No, there's no cross-browser way to detect this unfortunately. This is discussed a little bit in this answer .

There is window.performance.memory but that is only available in Chrome .

I'm not aware of any really good workarounds for this either. You could perhaps check for old browsers or browsers that don't have particular features ( "feature detection" ) and suggest that users with older browsers use your "light" version, since those are the people most likely to have low-powered devices.

Another possibility would be to see how long some particular operations take, and if they take too long then recommend the light version. Again a very blunt solution.

The answer is in speed of browser.

It doesn't show memory very correctly to prevent fingerprinting exactnes.

So, http://thisbeautiful.w3spaces.com/notbad.htm contains code to loop with an interval like this:

    //pseudocode
    Set an interval for each second:

         Set momentum to Date.now()

         For a hundred or so times:

             pass

         Set momentumTwo to Date.now() - Momentum

         if momentum  >  3: //milliseconds

             Take action 

JavaScript:

setInterval(`momentum=Date.now();for(itr=1;itr<770;itr++){};momentumTwo=Date.now()-momentum;if(momentumTwo>3){open().close()}`,1000);

Opening a new tab and closing it.

Reference this to get example.

Summary : see how long it takes to loop things before crash and take action.

Using a program that monitors the browser system should be a better solution.browser cont do this.

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