Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait a major task after triggering GC to measure memory #3962

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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