Source: https://wicg.github.io/page-lifecycle/spec.html
Timestamp: 2019-04-23 23:52:46+00:00

Document:
This document defines an API that supports browsers' ability to manage lifecycle of web pages.
Feedback and comments on this specification are welcome, please send them to public-web-perf@w3.org (subscribe, archives) with %5Bpage-lifecycle%5D at the start of your email’s subject.
With large numbers of web apps (and tabs) running, critical resources such as memory, CPU, battery, network, etc. easily get oversubscribed, leading to a bad end-user experience. Application lifecycle is a key way that modern OSs manage resources.
provide lifecycle-compatible APIs that allow key capabilities to work even when the app is backgrounded or stopped.
Frozen: lifecycle state for CPU suspension. This means that the §4.2.2.2 Change frozenness of document algorithm was called on the Document's browsing context. Normally HIDDEN pages will be frozen to conserve resources.
Discarded: means that the discard algorithm was called on the Document's browsing context. Normally frozen frames will be moved to discarded state to conserve resources.
The onfreeze and onresume attributes are event handler IDL attributes for the freeze and resume events, respectively.
The wasDiscarded attribute’s getter must return the value of this Document's discarded boolean.
NOTE: In addition clientId and discardedClientId will be added to Window, to support restoring view state when user revisits a discarded page, causing a reload. We expect those to be used by code that reacts to these events.
Example of restoring view state after discard: A user could have multiple tabs open for the same app & URL. If they are both in the background and are both discarded, then the app would need to distinguish between the two tabs to restore the correct state. clientId and lastClientId on the Window can be used for this purpose.
// `getPersistedState()` (if the tab has to be reloaded after a discard).
// Retrieves the state record from IndexedDB based on the passed client ID.
// client ID of the discarded tab via `self.lastClientId`.
Each task source is classified as freezable or unfreezable.
All tasks queued on freezable task source are automatically freezable.
DOM manipulation, user interaction, history traversal etc.
NOTE: networking task source is freezable with an exception: when a pending fetch keep-alive request in inflight, then networking task source must not be frozen.
TODO(domenic): classify all existing task sources in the HTML spec; find out if other specs have defined any relevant task sources.
TODO(panicker): Add note about keeping everything within a site instance that are scriptable (eg. documents that can post-message with each other) in the same lifecycle state.
When documents move into and out of bfcache (back forward cache) they will transition its frozenness state to true and false respectively.
In the unload a document algorithm, after Step #5, if the persisted attribute is true (i.e. we are moving to bfcache), run the §4.2.2.2 Change frozenness of document algorithm, passing in document and true.
In the traverse the history algorithm, before Step #4.6.4, if the persisted attribute is true (i.e. we are moving out of bfcache), run the §4.2.2.2 Change frozenness of document algorithm, passing in document and false.
Step #1 currently ignores "tasks whose associated Documents are not fully active". Modify this clause to also ignore freezable tasks whose associated Document is in the frozen state.
Rename the "discard" concept, for both browsing contexts and documents, to "destroy". This allows us to use the "discarded" terminology for the user-facing wasDiscarded attribute.
If the browsing context was previously discarded, then set the Document's discarded boolean to true.
NOTE: Per the §4.2.2.1 Change frozenness of a top-level document algorithm, when the Document of the top level browsing context changes its frozenness state then all documents of descendant browsing contexts will also change frozenness to the same value (and be consistent with the Document of the top level browsing context).
In mobile Chrome, tabs that have been in background for (at least) 5 minutes, may be frozen, to conserve battery and data.
NOTE: background tabs that are actively doing work on behalf of the user (eg. playing audio) are generally not frozen or discarded.
NOTE: For a detailed list of heuristics & exclusions used by Chrome, see this doc.
The UA will typically execute §4.2.2.1 Change frozenness of a top-level document with false when the user revisits that browsing context. In addition, the UA may choose to periodically execute §4.2.2.1 Change frozenness of a top-level document with false in the background, if plentiful resources are available.
Assert: doc’s browsing context is a top-level browsing context.
Execute §4.2.2.2 Change frozenness of document passing in doc and x.
Let descendants be the list of the descendant browsing contexts of doc.
Let d be the active document of b.
Execute §4.2.2.2 Change frozenness of document passing in d and x.
Set doc’s frozenness state to true.
Fire an event named freeze at doc.
Fire an event named resume at doc.
Set doc’s frozenness state to false.
NOTE: it is intentional that the ordering between event-firing and setting the state are different in the two cases above.
Each Document has a discarded boolean, which is initially false.
To discard a browsing context, destroy the browsing context, and make note of the fact that the reason it and any descendant browsing contents were destroyed was because of discarding.
NOTE: Discard is typically done to reclaim system memory, when memory and other resources are running low. On the other hand destroying a browser context is the normal teardown due to user leaving the page etc.
NOTE: background tabs that are actively doing work on behalf of the user (eg. playing audio) are generally not discarded.
When a top-level browsing context (tab in the browser) is discarded due to resource pressure (or unexpected events eg. process crash), and later the user revisits the tab in the browser, then the Document's discarded boolean will be true due to §4.1.5 HTML: Modifications to Initialize the document.
Special thanks to Ojan Vafai, Philip Walton, Fadi Meawad, Olli Pettay, Todd Reifsteck for their technical input and suggestions that led to improvements to this specification.

References: §4
 §4
 §4
 §4
 §4
 §4
 §4
 §4
 §4