Tracking objects’ heap allocation usually involves starting a recording, performing a sequence of actions, then stop the recording for analysis.
So DevTools provides a powerful set of tools for understanding the memory characteristics of web apps. It’s invaluable for diagnosing leaks and detecting regressions.
Using Performance APIs
For programmatic memory measurement, the browser API
performance.measureUserAgentSpecificMemory()
allows measuring total memory usage of a page.
function scheduleMeasurement() {
// Check measurement API is available.
if (!window.crossOriginIsolated) {
console.log('performance.measureUserAgentSpecificMemory() is only available in cross-origin-isolated pages');
console.log('See https://web.dev/coop-coep/ to learn more')
return;
}
if (!performance.measureUserAgentSpecificMemory) {
console.log('performance.measureUserAgentSpecificMemory() is not available in this browser');
return;
}
const interval = measurementInterval();
console.log(`Running next memory measurement in ${Math.round(interval / 1000)} seconds`);
setTimeout(performMeasurement, interval);
}
This method returns the aggregated RAM usage of the current page plus all related contexts like iframes and workers. The result includes breakdowns attributing memory usage to specific execution contexts.
This API can be used to collect real user data on memory consumption. Long-term trends may reveal gradual leaks or regressions after site changes.
However, measureUserAgentSpecificMemory() reflects each browser’s internal memory representation. Results vary significantly across browsers. Relative comparisons over time are most meaningful.
Common Causes of Memory Leaks
Many memory leaks in web apps stem from retaining stale references to objects, preventing their cleanup by garbage collection.
Some common sources include:
- Forgetting to remove event listeners on DOM detachment
- Unintentionally capturing references to DOM elements in closures
- Growing data structures like maps or arrays without clearing
- Failing to close Web Workers properly
- Lingering references between parent and iframe contexts
- Closures and unexpected references are a frequent cause. For example, adding a DOM element inside a closure keeps it referenced even if removed from the page later.
Memory leaks tend to be introduced inadvertently and often grow gradually over time. But fixing even small leaks can improve performance.
Profiling memory usage during typical user flows helps identify gradual leak sources. DevTools,
fuite
, and other tools can pinpoint growing objects. Fixing leaks may require rearchitecting some logic.
Aim for relatively flat memory usage over time, rather than sawtooth patterns showing gradual accumulation between resets. Leaks under 1MB can still be worth fixing if they add up over long sessions.
Conclusion
Chrome’s new hover cards provide useful insight into per-tab memory usage for users. For developers, this feature when combined with DevTools, performance APIs, and careful optimization can help you deliver smoother, memory-efficient web apps that users enjoy.
Further reading