Skip to content

Commit

Permalink
Wait a major task after triggering GC to measure memory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewiggins committed Apr 5, 2023
1 parent e97da39 commit 1345125
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion benches/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 1345125

Please sign in to comment.