From a9d9c649db18f40606b27cdb2e37e8d3fbb638cc Mon Sep 17 00:00:00 2001 From: Andre Wiggins <459878+andrewiggins@users.noreply.github.com> Date: Wed, 5 Apr 2023 23:46:24 -0700 Subject: [PATCH] Wait a major task after triggering GC to measure memory (#3962) Locally, i'm getting very different memory results than what is reported in this PR so attempting this benchmarking fix to hopefully normalize and reduce noise seen in differences across different machines and environments. --- benches/src/util.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/benches/src/util.js b/benches/src/util.js index 9711bcd157..5eda4f5027 100644 --- a/benches/src/util.js +++ b/benches/src/util.js @@ -5,6 +5,12 @@ export { afterFrame }; export const measureName = 'duration'; +const majorTask = () => + new Promise(resolve => { + window.addEventListener('message', resolve, { once: true }); + window.postMessage('major task delay', '*'); + }); + let promise = null; export function afterFrameAsync() { if (promise === null) { @@ -19,10 +25,20 @@ export function afterFrameAsync() { return promise; } -export function measureMemory() { +export async function measureMemory() { if ('gc' in window && 'memory' in performance) { // Report results in MBs + performance.mark('gc-start'); window.gc(); + performance.measure('gc', 'gc-start'); + + // window.gc synchronously triggers one Major GC. However that MajorGC + // asynchronously triggers additional MajorGCs until the + // usedJSHeapSizeBefore and usedJSHeapSizeAfter are the same. Here, we'll + // wait a moment for some (hopefully all) additional GCs to finish before + // measuring the memory. + await majorTask(); + performance.mark('measure-memory'); window.usedJSHeapSize = performance.memory.usedJSHeapSize / 1e6; } else { window.usedJSHeapSize = 0;