3 ms·
you’re right, it is a bad UI, but the problem isn’t a matter of “just save the page state”. That already happens when you go back and forward - there’s literall
by olliej 7y ago
you’re right, it is a bad UI, but the problem isn’t a matter of “just save the page state”. That already happens when you go back and forward - there’s literally a concept in browsers called the “back/forward cache” that is responsible for you not losing stuff that you entered on pages. Interestingly this also maintains a greater semblance of just working because a lot of network session info is kept around as well - things are ostensibly “per page load”, like session cookies through to session keys.
The problem is how do I save page state such that in critically low memory situations I can recover that state?
On a full system you can always page to disk - that’s how they keep the appearance of everything running. The downside is that swapping is bad for Flash storage, and apparently more so for the low power stuff you get in phones.
Now your statement “saving all memory is surely more expensive than saving some” isn’t actually correct.
First you have to define what “some” is: the entire dom, the js heap, network resources, and additional worker threads. Those are the vast bulk of the memory already being used - there’s not much else that isn’t already either torn down before a tab is killed or shared across processes (like the text segment, etc).
So we don’t end up meaningfully reducing how much needs to be stored.
Taking us to the next step: how do we selectively store just this “reduced” memory?
The reality is you end up having to add a bunch of additional metadata to the browser’s data structures. Which increases the frequency you hit the event you’re trying to avoid. It also means you have to maintain that metadata, providing a new source of bugs (I would consider highly likely), and using more cpu time (though I suspect not by much).
Finally, let’s say you have all of the logic to allow you to serialise everything needed to bring the page back up, when do you do it?
Tabs (or rather the process rendering the tab) are killed when the system is under memory pressure. Serialising requires a bunch of memory - it needs the space for the serialised page (let’s be generous and say 50% of the existing memory usage), it also needs space for the serialisation state, which is likely to be a bunch of O(Number of heap objects) maps.
That means the first thing a web process does when given a memory pressure warning is allocate a huge amount of memory, which would presumably fail (its a memory pressure situation), and then not be able to continue, and then get killed.
Even if you could serialise straight to disk (bad because aforementioned swapping vs SSD) you still get everything killed because you can’t store the serialisation state itself to disk, it has to be resident. Then what happens is you encounter memory pressure and every browser process simultaneously starts trying to serialise their current page, so all allocate a pile of memory for their serialisation state, and again you’re in the position of having a bunch of memory allocation requests when you by definition have little memory left.