Measuring memory in the field
Measure memory on real visits with measureUserAgentSpecificMemory(): cross-origin isolation, random sampling, beacons, percentiles, and a memory budget.
- 01Ask Chrome how much memory a page usesCall measureUserAgentSpecificMemory(), read its bytes and breakdown, and expect the answer to take seconds.
- 02Make a page able to askIsolate the page, check before calling, and know why the older performance.memory misleads.
- 03Measure real visits and set a budgetAsk at random times, send each answer home, read the percentiles by how far into the visit, and judge them.
How much memory do real visitors' tabs use?
Ravi has fixed the Leaf and Kettle's leak. His leak test from lesson 29.2 now passes. Twenty round trips to a tea page and back leave nothing behind.
But Ravi still has a question. The test runs on his laptop, for twenty round trips. Ben shops on a cheap phone, and some evenings he stays a long time. How much memory does the shop hold in real visitors' tabs after 100 round trips? Does it keep growing? Ravi can't sit next to every visitor with DevTools open.
So the page has to measure itself. Chrome has a function for it: performance.measureUserAgentSpecificMemory(). The page calls it now and then, at random times. Each answer goes to Ravi's server as a beacon, a small message the page sends home, as in lesson 12.3. Then Ravi groups the answers by how far into the visit they came.
Measuring real visits like this is measuring in the field. A leak test is a measurement in the lab: one computer, one script, the same steps every time. Lesson 3.3 compared the two.
The course sent robots in place of visitors: scripts that click through the shop like a visitor. Each robot made 150 round trips, much faster than a person would. Of the robots, 8 visited the fixed shop, and 8 the old leaky shop, for comparison. Here is what Ravi's dashboard showed:
The answers are in three groups: early, midway and late in a visit. Each bar is a group's p50, its middle answer. Half the answers in the group were smaller, and half were bigger. Here are the same numbers in code:
// The middle answer (p50) from each shop's visits, in MB: after 0-49, 50-99 and 100-150 round tripsconst fixed = [0.91, 0.91, 0.91];const leaky = [1.54, 1.55, 1.55]; const grew = (p50) => (p50[2] - p50[0]).toFixed(2);console.log(`Fixed shop: grew ${grew(fixed)} MB from early to late in a visit`);console.log(`Leaky shop: grew ${grew(leaky)} MB, and held ${(leaky[2] - fixed[2]).toFixed(2)} MB more`);- Line 1 is a comment. It says what the numbers are.
- Lines 2 and 3 hold each shop's p50 in MB, early, midway and late in a visit.
- Line 5 takes the late number and subtracts the early one.
- Lines 6 and 7 print
Fixed shop: grew 0.00 MB from early to late in a visitandLeaky shop: grew 0.01 MB, and held 0.64 MB more.
That answers Ravi's question. The fixed shop's middle answer was 0.91 MB early in a visit. After 100 round trips, it was still 0.91 MB. Why it matters: the fix holds in long visits, not only in a short test.
The leaky shop held about 0.6 MB more the whole time. But it didn't grow either. A later section shows why.
A head chef can't taste every bowl that leaves her kitchen. So all evening, a cook tastes a spoonful at random moments and writes down how salty it is. At closing time, the chef sorts the notes into three piles: early, middle and late. If the late soup is saltier, the pot is boiling down, and something needs fixing.
- In real life: A spoonful, tasted at a random moment
- In JavaScript: One call to measureUserAgentSpecificMemory()
- In real life: The note the cook writes down
- In JavaScript: A beacon sent to Ravi's server
- In real life: Three piles: early, middle and late
- In JavaScript: Answers grouped by round trips so far
- In real life: Late soup saltier than early soup
- In JavaScript: Memory that grows as a visit goes on
A page can ask Chrome how much memory it uses. One answer is slow and noisy. So ask at random times, in many visits, and send every answer home. Then compare the answers early and late in a visit, against a budget. The sections below take each step: asking, the isolated page it needs, an older number to avoid, sampling, reading the answers, and the budget.
How the course recorded it
- The pages: lesson 29.2's shop, fixed and leaky, served over HTTPS from this computer, with the two headers from lesson 27.4.
- The robots: 16 visits in Chrome 157, 8 per shop, four of each at a time. A round trip was a mouse click on a tea, then one on "← All teas". Chrome ran at full speed.
- The measuring: the page's own code, shown later in this lesson, asked at random times. It sent 139 beacons to a server on this computer.
- Two more pages: the same shop without the headers, and a blank tab, for the older
performance.memory.
Asking Chrome how much memory a page uses
The JavaScript course met this function in Allocation profiling. Here it is again, as small as it gets:
if (crossOriginIsolated) { const result = await performance.measureUserAgentSpecificMemory(); console.log(result.bytes); for (const entry of result.breakdown) console.log(entry.bytes, entry.types);}- Line 1 checks that this page may call it. The next section explains why it might not.
- Line 2 calls it. It returns a promise: an object that hands you the answer later.
awaitwaits for it. - Line 3 prints
bytes: Chrome's estimate of the page's memory. - Line 4 prints each entry of
breakdown: its bytes, and what kind of memory it is.
The course loaded the fixed shop and let it sit idle for two seconds, with nobody clicking. Then it called the function. Here is Chrome's answer, entry by entry:
// Chrome's first answer on the isolated shop, from the recordingconst result = { bytes: 568815, breakdown: [ { bytes: 254223, types: ["JavaScript"] }, { bytes: 0, types: [] }, { bytes: 40552, types: ["DOM"] }, { bytes: 274040, types: ["Shared"] },] };const mb = (bytes) => (bytes / 1e6).toFixed(2) + " MB";for (const entry of result.breakdown) { console.log(`${entry.types.join(", ") || "(empty)"}: ${mb(entry.bytes)}`);}console.log(`In all: ${mb(result.bytes)}`);- Line 2 holds the total. Lines 3 to 6 hold the four entries, in the order Chrome gave them.
- Line 8 turns bytes into MB. Lines 9 to 11 print one line per entry. Line 12 prints the total.
- It prints
JavaScript: 0.25 MB,(empty): 0.00 MB,DOM: 0.04 MBandShared: 0.27 MB. ThenIn all: 0.57 MB.
Each entry's types says what kind of memory it holds. Chrome's source gives these names:
- JavaScript: the page's own JavaScript objects. Its
attributionnames the page that owns them. - DOM: the page's elements and styles, which Chrome keeps in a heap of their own.
- Shared: memory the engine couldn't tie to one page.
- The empty entry: no bytes and no types. The spec asks for it, so that code reads the list as a list, without counting on fixed entries.
The order changes too. The spec asks browsers to shuffle the entries. On the second call, the empty entry came first. Why it matters: find an entry by its types, never by its place in the list. And the names are Chrome's own. The explainer warns that they can differ between browsers, and even between versions of one browser.
The answer takes seconds
Chrome answered after 19.0 seconds. That isn't a slow computer. Chrome measures memory during a garbage collection: the clean-up that frees objects no code can reach, as lesson 29.1 showed. A clean-up walks through every live object anyway. So Chrome counts them as it goes.
Your family wants to know how many clothes they own. Nobody empties every wardrobe for that. You count on washing day, when every shirt passes through your hands anyway. If someone asks on Monday, you say: I'll tell you after the next wash. And if no wash comes soon, you do one specially.
- In real life: Washing day
- In JavaScript: A garbage collection
- In real life: Counting each shirt as you fold it
- In JavaScript: Measuring while the clean-up walks the objects
- In real life: "I'll tell you after the next wash"
- In JavaScript: The promise, which waits for an answer
- In real life: A special wash, if none comes soon
- In JavaScript: The clean-up V8 starts after 10 to 19 seconds
What if no clean-up comes? Then V8, Chrome's JavaScript engine, starts one itself. Its source picks the wait at random: 10 to 19 seconds. Here is how long the course waited:
// How long each promise took to settle, in ms, from the recordingconst idleShop = [19019, 11014, 17014];const robots = { count: 139, shortest: 42, longest: 19016 }; const s = (ms) => (ms / 1000).toFixed(1) + " s";console.log(`Idle shop: ${idleShop.map(s).join(", ")}`);console.log(`Robots: ${robots.count} answers, from ${robots.shortest} ms to ${s(robots.longest)}`);- Line 2 holds the three calls on the idle shop, in ms. Line 3 holds the robots' answers: how many, the shortest and the longest.
- It prints
Idle shop: 19.0 s, 11.0 s, 17.0 sandRobots: 139 answers, from 42 ms to 19.0 s.
On the idle shop, no clean-up came on its own. So each call waited for V8's timer: just over 19, 11 and 17 seconds. The timer picks whole seconds. The robots were busy clicking. When a clean-up came sooner on its own, the answer came sooner. Still, 70 of their 139 answers took 10 seconds or more.
Why it matters: don't use it to time one click. The explainer says it isn't meant for measuring before and after a single action. It's meant for many answers, put together.
Only an isolated page may ask
Ravi tries the call on the shop as it is today. It fails: the function isn't there. Chrome only gives it to a page that is cross-origin isolated.
Why so strict? Because the answer is a size, and a size can give a secret away. Say a page shows a picture from another site, one that only signed-in people see. If the page could weigh everything it holds, it could learn how big that private picture is. The explainer says the API shows only one thing across origins: "the size information provided in the bytes fields". So it asks for a page where every file from another site has agreed to be there.
You pack a box for your aunt. Your cousin slips her own gift in, wrapped. If you weigh the box, you learn how heavy her gift is, and that might spoil the surprise. So the rule is: weigh the box only when the lid is shut, and only when every gift inside has a note saying "you may weigh mine".
- In real life: The box and everything in it
- In JavaScript: The page, with its frames and workers
- In real life: Your cousin's wrapped gift
- In JavaScript: A file from another site
- In real life: Her note: "you may weigh mine"
- In JavaScript: A CORP or CORS header, which COEP asks for
- In real life: Closing the box to anyone else
- In JavaScript: COOP: other sites' windows can't reach in
- In real life: The scale
- In JavaScript: measureUserAgentSpecificMemory()
Lesson 27.4 isolated a page for shared memory. It's the same recipe:
- HTTPS: the page is a secure context, sent over HTTPS or from your own computer.
Cross-Origin-Opener-Policy: same-origin, or COOP: windows from other sites can't hold on to the page.Cross-Origin-Embedder-Policy: require-corp, or COEP: every file from another site must agree to be loaded, with a CORP or CORS header.
The course sent the same shop twice, over HTTPS both times. Only the second had the two headers:
// What each page saw, from the recordingconst plain = { crossOriginIsolated: false, measure: "undefined", memory: "object" };const isolated = { crossOriginIsolated: true, measure: "function", memory: "object" }; for (const [name, page] of Object.entries({ plain, isolated })) { console.log(`${name}: isolated ${page.crossOriginIsolated}, measureUserAgentSpecificMemory is ${page.measure}`);}- Lines 2 and 3 hold what each page saw.
- Lines 5 to 7 print
plain: isolated false, measureUserAgentSpecificMemory is undefinedandisolated: isolated true, measureUserAgentSpecificMemory is function.
Without the headers, the function wasn't there at all. So check crossOriginIsolated before you call it, as line 1 of the first example did. Why it matters: a monitor that skips the check breaks on every page that isn't isolated.
Isolation has a price. Every image, script and frame from another site must send CORP or CORS, or it won't load. web.dev suggests a dry run first. The header Cross-Origin-Embedder-Policy-Report-Only reports what would break, without blocking anything. Check what this page can do:
| What was read | This page | Shop, no headers | Shop, with headers |
|---|---|---|---|
| crossOriginIsolated | not checked | false | true |
| measureUserAgentSpecificMemory | not checked | undefined | function |
| performance.memory | not checked | object | object |
| navigator.deviceMemory | not checked | 16 | 16 |
The lab reads four things on this page: whether it's isolated, whether the two memory functions exist, and the device's memory.
performance.memory, the older number
Chrome has an older number too: performance.memory. Every page in Chrome can read it, isolated or not. Its usedJSHeapSize is the size of the JavaScript heap, the memory that holds JavaScript objects. It's tempting. But it isn't a standard, MDN marks it as deprecated, and it can mislead in three ways.
It can be rounded
Chrome runs each page in a process: a separate running program, with its own memory. It gives the real number only when the tab's process belongs to one site. Its source says so. On computers, Chrome gives each site a process of its own. On Android, it does that for only some sites, to keep the cost down. In a process shared with other sites, Chrome rounds the number up, to one of 100 steps. The first step is 10 MB. Then it gives the same answer for 20 minutes.
The course saw it in a new, blank tab, which belonged to no site yet:
// usedJSHeapSize in a blank tab: before, just after making 200,000 small objects, and a second laterconst reads = [10000000, 10000000, 10000000];const realHeap = { before: 660884, after: 12007512 }; // read through the DevTools protocol console.log(`performance.memory: ${reads.join(", ")}`);console.log(`The real heap grew by ${((realHeap.after - realHeap.before) / 1e6).toFixed(1)} MB`);- Line 2 holds three reads of
usedJSHeapSize: before the tab made 200,000 small objects, just after, and a second later. - Line 3 holds the real heap, before and after, read through the DevTools protocol: the commands DevTools itself uses.
- It prints
performance.memory: 10000000, 10000000, 10000000andThe real heap grew by 11.3 MB.
The heap grew by more than 11 MB, and the number didn't move. On the shop, in a process of its own, the same property gave real numbers, like 781,617. Why it matters: in a shared process, any heap up to 10 MB reads 10,000,000, leak or no leak.
In power-saving mode, Ben's step counter rounds up to whole thousands. Walk 300 steps or 900, and it shows 1,000. It also checks only every 20 minutes. Walk 5,000 more steps in the next ten minutes, and it still shows 1,000.
- In real life: Power-saving mode
- In JavaScript: A process shared with other sites
- In real life: Rounded up to whole thousands
- In JavaScript: Rounded up to Chrome's steps, the first one 10 MB
- In real life: A new number every 20 minutes
- In JavaScript: The same answer for 20 minutes
- In real life: Normal mode, every step counted
- In JavaScript: A process of its own: the real number
Move the slider to see Chrome's steps:
| A process of its own | 12,000,000 |
|---|---|
| A process shared with other sites | 12,700,000 |
Chrome rounds up to the first of its 100 steps at or above the real size. The step after this one is 13,400,000 bytes. Then it gives the same answer for 20 minutes. In a process of its own, the page gets the real number.
It counts something else
performance.memory counts the whole JavaScript heap, with what every empty page starts with. It counts no DOM. The new function leaves out the empty page's share, as its spec asks, and adds the DOM. The robots' beacons carried both numbers, so you can compare them:
// p50 late in a visit (100 to 150 round trips), in bytes, from the same beaconsconst measured = { leaky: 1552415, fixed: 906129 }; // measureUserAgentSpecificMemory()const legacy = { leaky: 2363823, fixed: 1920337 }; // performance.memory const leak = (bytes) => ((bytes.leaky - bytes.fixed) / 1e6).toFixed(2) + " MB";console.log(`measureUserAgentSpecificMemory: the leak costs ${leak(measured)}`);console.log(`performance.memory: the leak costs ${leak(legacy)}`);- Lines 2 and 3 hold the p50 late in a visit, for each shop, from the same beacons.
- Line 5 subtracts the fixed shop from the leaky one.
- It prints
measureUserAgentSpecificMemory: the leak costs 0.65 MBandperformance.memory: the leak costs 0.44 MB.
performance.memory missed about a third of the leak. Most of the missing part is DOM: the kept tea pages' elements, about 0.23 MB.
It may count other pages
If other pages share the tab's process, their objects count too. And workers and frames from other sites, with heaps of their own, don't count at all. MDN calls the number unreliable for both reasons. Read the table across: each row asks one question about the two ways to ask.
| Question | measureUserAgentSpecificMemory() | performance.memory |
|---|---|---|
| What does it count? | The page's JavaScript and DOM, and memory the engine couldn't tie to the page. Not what every empty page starts with. | The whole JavaScript heap of the tab's process, with what every page starts with. No DOM. |
| When does it answer? | After the next clean-up: up to about 20 seconds later. | At once. |
| Is it rounded? | No. | Yes, in a process shared with other sites: up to steps that start at 10 MB, and the same for 20 minutes. |
| Which pages can use it? | Cross-origin isolated pages, in Chromium browsers like Chrome. | Any page, in Chromium browsers like Chrome. |
| Is it a standard? | A draft, from the WICG. | No. MDN marks it as deprecated. |
Why it matters: build your field data on the new function. Where it can't run, treat the old number as a rough guide.
Measuring real visits at random times
Now Ravi's page can ask. But when? One answer means little. It depends on what the visitor just did, and on when Chrome last cleaned up. The explainer suggests many answers, at random times. First, the gap between two questions:
const MEAN_GAP_MS = 10_000; // the course's robots; real visitors: 5 * 60_000function nextGap() { return -Math.log(Math.random()) * MEAN_GAP_MS;}- Line 1 sets the average gap. The robots used 10 seconds. For real visitors, the explainer's example uses 5 minutes.
- Lines 2 to 4 pick a random gap.
Math.random()gives a number between 0 and 1. Its-Math.log(), times the average, gives gaps that are often short and sometimes long, with the average you asked for.
Why random? A fixed gap, say every 5 minutes on the dot, can miss the same moments every time. web.dev warns that memory peaks may come between the samples. Random gaps make every moment of a visit equally likely to be measured. This kind of sampling is called a Poisson process.
Next, the loop that asks, waits, and sends:
async function measureOnce() { const asked = Math.round(performance.now()); const result = await performance.measureUserAgentSpecificMemory(); const answered = Math.round(performance.now()); sendMemoryBeacon(result, asked, answered); setTimeout(measureOnce, nextGap());}if (crossOriginIsolated) setTimeout(measureOnce, nextGap());- Line 2 notes when the page asked, in ms since it loaded. Line 3 waits for the answer. Line 4 notes when it came.
- Line 5 sends the answer and both times.
- Line 6 plans the next question, after a new random gap.
- Line 8 starts it all, but only on an isolated page.
And the beacon itself:
let routeChanges = 0;document.addEventListener("click", (event) => { if (event.target.closest("a[data-route]")) routeChanges++;});function sendMemoryBeacon(result, asked, answered) { navigator.sendBeacon("/memory", JSON.stringify({ bytes: result.bytes, breakdown: result.breakdown, roundTrips: Math.floor(routeChanges / 2), asked, answered, legacy: performance.memory?.usedJSHeapSize, deviceMemory: navigator.deviceMemory, }));}- Lines 1 to 4 count route changes: each click on a link the shop's router handles. A round trip is two of them.
- Line 5 starts
sendMemoryBeacon, which the loop above calls. - Line 6 sends the beacon with
navigator.sendBeacon(). Lesson 12.3 explains why a beacon still arrives when the visitor leaves. - Lines 7 and 8 send the total and the breakdown. Line 9 sends how far into the visit it was, in round trips.
- Lines 10 and 11 send the two times.
- Line 12 adds
performance.memory, for comparison. Line 13 adds the device's memory.
Here is one robot visit to the leaky shop, as the server saw it:
The page asked 10 times. Some gaps were under 2 seconds, and one was almost 20. No one picked those moments, so every part of the visit had the same chance of being measured.
Which device was it?
Lesson 29.1 met navigator.deviceMemory: the device's memory in GB, rounded. Line 13 sends it with each answer. The Device Memory spec suggests just that, so you can split your data by device.
A server can get the same number without JavaScript, over HTTPS. It asks once, with a response header. Then Chrome sends the number with the page's next requests:
HTTP/1.1 200 OKAccept-CH: Sec-CH-Device-Memory POST /memory HTTP/1.1Sec-CH-Device-Memory: 16- Line 1 starts the server's answer to the page.
- Line 2 is the server's question:
Accept-CH: Sec-CH-Device-Memory. The number Chrome sends back is a client hint. - Lines 4 and 5 are a beacon from the page, with the answer:
Sec-CH-Device-Memory: 16.
The course's first load carried no hint: Chrome hadn't been asked yet. The reload carried it, and so did all 139 beacons. Every robot said 16: this computer's memory, in GB. Why it matters: with real visitors, Ravi can read the answers from small phones on their own.
Reading the answers by how far into the visit
The robots sent 139 answers: 64 from the leaky shop and 75 from the fixed one. The server sorted them into three groups, by the round trips made when Chrome answered: 0 to 49, 50 to 99, 100 to 150. For each group, it worked out percentiles by nearest rank, as in lesson 12.4. The p75 is the answer that three in four were at or below. The p95 is the one that 19 in 20 were at or below. Pick one:
| Round trips | Leaky shop | Fixed shop |
|---|---|---|
| 0 to 49 | 1.66 (19 answers) | 1.28 (20 answers) |
| 50 to 99 | 1.56 (25 answers) | 1.32 (25 answers) |
| 100 to 150 | 1.56 (20 answers) | 0.95 (30 answers) |
The p75 is three answers in four were at or below it. Each group holds the answers that came after that many round trips, from all of a shop's visits.
Three things stand out.
The p50s are flat. A shop that kept growing would climb from group to group. Neither did.
The p75 and p95 jump around. The fixed shop's p75 was 1.32 MB midway, then 0.95 MB late. Single answers jumped too, because of the Shared part. It moved between about 0.4 and 1.1 MB from one answer to the next, in both shops. Why it matters: compare groups of answers, never two single ones.
The leaky shop never climbed. Why not? In lesson 29.2, every round trip opened a different tea, and kept its page. The robots went round the same 20 teas again and again. The leaky shop's Map keeps one page per tea. So once a visitor has opened every tea, there's nothing new to keep. The DOM part shows it:
// The leaky shop's DOM part, in MB, after this many round trips, from the beaconsconst dom = { 13: 0.19, 15: 0.22, 19: 0.27, 20: 0.28, 147: 0.27 }; for (const [trips, mb] of Object.entries(dom)) { console.log(`After ${trips} round trips: ${mb} MB`);}- Line 2 holds the DOM part from 5 of the leaky shop's answers: the earliest ones, and the last.
- Lines 4 to 6 print one line each, from
After 13 round trips: 0.19 MBtoAfter 147 round trips: 0.27 MB.
It rose until about the 20th round trip, then stayed level. Why it matters: the field shows a leak's real shape. This one had a ceiling of 20 pages, so it cost every visitor about the same, early and late. A leak with no ceiling would climb from group to group.
A memory budget for the shop
Numbers need a line to be judged against. A memory budget is a limit you agree on before you look, like the JavaScript budget of lesson 22.1. Ravi writes his with two rules:
- A ceiling: in every group, the p75 stays under 1.5 MB.
- A growth limit: the late p75 is at most 20% above the early one.
He took the ceiling from the fixed shop. Its highest p75 was 1.32 MB, and he added some room. The growth limit is for leaks that keep climbing.
A school sets a rule: three bags in four must weigh under 5 kg. The teacher weighs the class's bags at the start, the middle and the end of the school year. Bags mustn't get heavier as the year goes on, either. A bag that grows every month means books never go home. A bag that's too heavy from the first day breaks the rule too.
- In real life: No more than 5 kg, for three bags in four
- In JavaScript: The p75 under the ceiling
- In real life: Weighed at the start, middle and end of the year
- In JavaScript: Checked early, midway and late in a visit
- In real life: No heavier at the end than at the start
- In JavaScript: The growth limit
- In real life: A bag that's too heavy all year
- In JavaScript: A leak that stops: it breaks the ceiling
Here is the check, on the robots' p75s:
const budget = { p75: 1500000, growth: 1.2 }; // bytes, and at most 20% growthconst shops = { leaky: [1664286, 1555553, 1558177], fixed: [1284690, 1316672, 946571] }; // p75 in each part of a visit for (const [name, p75] of Object.entries(shops)) { const over = p75.some((bytes) => bytes > budget.p75); const grows = p75[p75.length - 1] > p75[0] * budget.growth; console.log(`${name}: ${over ? "over the ceiling" : grows ? "grows too much" : "within budget"}`);}- Line 1 is the budget: the ceiling in bytes, and the growth limit as a factor.
- Line 2 holds each shop's p75, early, midway and late.
- Line 5 checks the ceiling. Line 6 checks the growth.
- Line 7 prints
leaky: over the ceilingandfixed: within budget.
The leaky shop broke the ceiling in every group. It passed the growth limit, because its leak stopped at 20 pages. The fixed shop passed both. Why it matters: one rule alone can miss a leak. A ceiling catches a leak that stops, and a growth limit catches one that doesn't. Move the ceiling to see how the verdicts change:
| Leaky shop: over the ceiling | 1.66, 1.56, 1.56 |
|---|---|
| Fixed shop: within budget | 1.28, 1.32, 0.95 |
Each shop's p75 must stay under the ceiling early, midway and late in a visit. It also mustn't grow by more than 20% from the first group to the last.
A budget helps only if something checks it. Run the check on each day's beacons and on each new release. When it fails, send an alert, as in lesson 12.4. And check the small phones' group on its own.
Which tool answers it?
Each question below needs a different tool. Sort it by the one that answers it:
- Does each round trip leave a tea page behind, before the release ships?
- Which object keeps the old tea page in memory?
- How much memory do tabs hold after 100 round trips?
- Does the new release use more memory than the old one, for real visitors?
- Does memory keep growing the longer people stay?
- Are the biggest numbers from phones with 2 GB of memory?
- Should the server send a lighter page to a phone with little memory?
Sort each question by the tool that answers it.
Common mix-ups
- "One answer tells me how much memory my page uses." No. The Shared part alone moved by more than half a megabyte between answers. Read groups of answers.
- "The promise answers right away." No. On the idle shop it took 11.0 to 19.0 seconds, waiting for a clean-up.
- "Any HTTPS page can call it." No. On the shop without COOP and COEP, the function was undefined.
- "performance.memory is the same number, only older." No. It counts no DOM, includes what every page starts with, and is rounded in shared processes.
- "A leak always shows as a line that keeps climbing." No. The leaky shop stopped at 20 pages. Its leak showed as a higher line, which the ceiling caught.
- "I can compare bytes between browsers, or between Chrome versions." No. The explainer says the results are specific to each browser and can change between versions.
Practice exercises
Answer first, then use the hints.
Another shop's p75s go up a little in each group. What does Ravi's check print?
const budget = { p75: 1500000, growth: 1.2 };
const p75 = [1100000, 1250000, 1400000]; // early, midway, late in a visit
const over = p75.some((bytes) => bytes > budget.p75);
const grows = p75[2] > p75[0] * budget.growth;
console.log(over ? "over the ceiling" : grows ? "grows too much" : "within budget");grows too much. Every p75 is under the ceiling. But 1,100,000 times 1.2 is 1,320,000, and the late p75 is 1,400,000. Memory that climbs through a visit is what a leak with no ceiling looks like.
Here, three draws of Math.random() are fixed. What gaps, in ms, does the monitor's formula give?
const MEAN_GAP_MS = 10_000;
const draws = [0.5, 0.25, 0.9];
const gaps = draws.map((random) => Math.round(-Math.log(random) * MEAN_GAP_MS));
console.log(gaps.join(", "));6931, 13863, 1054. A draw near 1 gives a short gap, and a draw near 0 a long one. Over many draws, the gaps average 10 seconds.
In the playground, what was the fixed shop's p95 after 100 to 150 round trips, in MB?
1.39 MB. One answer in 20 was bigger than that. The p95 sits well above the p50, because of the Shared part's jumps.
Ravi's colleague puts this monitor on a page without COOP and COEP. Which line throws?
function measureOnce() {
performance.measureUserAgentSpecificMemory().then((result) => {
navigator.sendBeacon("/memory", JSON.stringify({ bytes: result.bytes }));
});
setTimeout(measureOnce, nextGap());
}
setTimeout(measureOnce, nextGap());if (crossOriginIsolated) setTimeout(measureOnce, nextGap());Line 2. Without isolation, performance.measureUserAgentSpecificMemory is undefined, so calling it throws a TypeError. Start the loop only when crossOriginIsolated is true.
Open the course's copy of the leaky shop in Chrome. In DevTools' Application panel, what does the Measure memory row say for the top frame?
unavailable. The course's copy doesn't send COOP and COEP, so it isn't cross-origin isolated. DevTools reads that from the frame and shows the function as unavailable.
The explainer says one kind of context can't call the function. Which kind of worker is it?
A dedicated worker. The page that owns it already measures it, so the explainer calls a call from inside it likely a misuse. Shared and service workers can call it.
Check your understanding
For each question, ask which step it's about: asking, isolating, sampling, reading, or the budget.
Question 1 of 7What does this check of Chrome's first answer print?
Read the code, then predictconst result = { bytes: 568815, breakdown: [ { bytes: 254223, types: ["JavaScript"] }, { bytes: 0, types: [] }, { bytes: 40552, types: ["DOM"] }, { bytes: 274040, types: ["Shared"] }, ] }; const sum = result.breakdown.reduce((total, entry) => total + entry.bytes, 0); console.log(sum === result.bytes, result.breakdown.length);Choose an answer to see the explanation.
Question 2 of 7Why did each promise on the idle shop take many seconds to settle?
Choose an answer to see the explanation.
Question 3 of 7Why does measureUserAgentSpecificMemory() need a cross-origin isolated page?
Choose an answer to see the explanation.
Question 4 of 7Why does the monitor wait a random time between measurements, not a fixed one?
Choose an answer to see the explanation.
Question 5 of 7Chrome's answer had an entry with no bytes, no attribution and no types. What is it for?
Choose an answer to see the explanation.
Question 6 of 7In the robots' visits, why did the leaky shop stop growing early in each visit?
Choose an answer to see the explanation.
Question 7 of 7Why send navigator.deviceMemory with every memory beacon?
Choose an answer to see the explanation.
Key takeaways
performance.measureUserAgentSpecificMemory()answers after a clean-up, often 10 seconds or more later. Read its parts by their types.- Only a cross-origin isolated page gets it: HTTPS, COOP and COEP. Check before you call.
performance.memorycounts no DOM, includes what every page starts with, and is rounded in shared processes.- Ask at random times, send each answer as a beacon with the device's memory, and group answers by how far into the visit.
- Judge the percentiles against a budget with a ceiling and a growth limit.
Remember the one-liner.
Ask at random times, send every answer home, and judge the percentiles against a budget.
Sources
The facts on this page come from these specs, docs and source files, and from the course's recordings:
- Measure Memory API, WICG draft specification.
- performance.measureUserAgentSpecificMemory API: the explainer, WICG.
- Monitor your web page's total memory usage with measureUserAgentSpecificMemory(), web.dev.
- measure_memory_controller.cc: how Chrome builds the answer, Chromium source.
- memory-measurement.cc: when V8 cleans up to answer, V8 source, as Chrome 157 ships it.
- Making your website cross-origin isolated using COOP and COEP, web.dev.
- Performance: measureUserAgentSpecificMemory() method, MDN.
- Performance: memory property, MDN.
- memory_info.cc: how performance.memory rounds and waits, Chromium source.
- window_performance.cc: when performance.memory is precise, Chromium source.
- Site Isolation, The Chromium Projects.
- Device Memory, W3C.
- Navigator: deviceMemory property, MDN.
- Sec-CH-Device-Memory header, MDN.
- FrameDetailsView.ts: the Application panel's frame details, Chrome DevTools source, as Chrome 157 ships it.
Coming next: Storage performance. Where a page keeps data between visits, and which kinds of storage block the main thread.