Performance: measureUserAgentSpecificMemory() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Note: This feature is available inWeb Workers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
ThemeasureUserAgentSpecificMemory() method is used to estimate the memory usage of a web application including all its iframes and workers.
In this article
Syntax
measureUserAgentSpecificMemory()Parameters
None.
Return value
APromise that resolves to an object containing the following properties:
bytesA number representing the total memory usage.
breakdownAn
Arrayof objects partitioning the totalbytesand providing attribution and type information. The object contains the following properties:bytesThe size of the memory that this entry describes.
attributionAn
Arrayof container elements of the JavaScript realms that use the memory. This object has the following properties:urlIf this attribution corresponds to a same-origin JavaScript realm, then this property contains the realm's URL. Otherwise it is the string "cross-origin-url".
containerAn object describing the DOM element that contains this JavaScript realm. This object has the following properties:
scopeA string describing the type of the same-origin JavaScript realm. Either
"Window","DedicatedWorkerGlobalScope","SharedWorkerGlobalScope","ServiceWorkerGlobalScope"or"cross-origin-aggregated"for the cross-origin case.
typesAn array of implementation-defined memory types associated with the memory.
An example return value looks like this:
{ "bytes": 1500000, "breakdown": [ { "bytes": 1000000, "attribution": [ { "url": "https://example.com", "scope": "Window" } ], "types": ["DOM", "JS"] }, { "bytes": 0, "attribution": [], "types": [] }, { "bytes": 500000, "attribution": [ { "url": "https://example.com/iframe.html", "container": { "id": "example-id", "src": "redirect.html?target=iframe.html" }, "scope": "Window" } ], "types": ["JS", "DOM"] } ]}Exceptions
SecurityErrorDOMExceptionThrown if thesecurity requirements for preventing cross-origin information leaks aren't fulfilled.
Description
The browser automatically allocates memory when objects are created and frees it when they are not reachable anymore (garbage collection). This garbage collection (GC) is an approximation since the general problem of determining whether or not a specific piece of memory is still needed is impossible (see alsoJavaScript Memory Management). Developers need to make sure that objects are garbage collected, memory isn't leaked, and memory usage doesn't grow unnecessarily over time leading to slow and unresponsive web applications. Memory leaks are typically introduced by forgetting to unregister an event listener, not closing a worker, accumulating objects in arrays, and more.
ThemeasureUserAgentSpecificMemory() API aggregates memory usage data to help you find memory leaks. It can be used for memory regression detection or for A/B testing features to evaluate their memory impact. Rather than make single calls to this method, it's better to make periodic calls to track how memory usage changes over the duration of a session.
Thebyte values this API returns aren't comparable across browsers or between different versions of the same browser as these are highly implementation dependent. Also, howbreakdown andattribution arrays are provided is up to the browser as well. It is best to not hardcode any assumptions about this data. This API is rather meant to be called periodically (with a randomized interval) to aggregate data and analyze the difference between the samples.
Security requirements
To use this method your document must be in asecure context andcross-origin isolated.
You can use theWindow.crossOriginIsolated andWorkerGlobalScope.crossOriginIsolated properties to check if the document is cross-origin isolated:
if (crossOriginIsolated) { // Use measureUserAgentSpecificMemory}Examples
>Monitoring memory usage
The following code shows how to call themeasureUserAgentSpecificMemory() method once every five minutes at a random interval usingExponential distribution.
function runMemoryMeasurements() { const interval = -Math.log(Math.random()) * 5 * 60 * 1000; console.log(`Next measurement in ${Math.round(interval / 1000)} seconds.`); setTimeout(measureMemory, interval);}async function measureMemory() { const memorySample = await performance.measureUserAgentSpecificMemory(); console.log(memorySample); runMemoryMeasurements();}if (crossOriginIsolated) { runMemoryMeasurements();}Specifications
| Specification |
|---|
| Measure Memory API> # ref-for-dom-performance-measureuseragentspecificmemory⑤> |