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

fix: make leak-detector more aggressive when running GC #14526

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))

### Performance

### Chore & Maintenance
Expand Down
13 changes: 12 additions & 1 deletion packages/jest-leak-detector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// <reference lib="es2021.WeakRef" />

import {promisify} from 'util';
import {setFlagsFromString} from 'v8';
import {getHeapSnapshot, setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import {isPrimitive} from 'jest-get-type';
import {format as prettyFormat} from 'pretty-format';
Expand Down Expand Up @@ -48,6 +48,17 @@ export default class LeakDetector {
await tick();
}

if (this._isReferenceBeingHeld) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds a full 300ms to the runtime of the simplest test I can manage (on a beefy macbook pro). But doing it here means we only have the hit if we believe we're still leaking after running GC, so I think it's worthwhile

Copy link
Member Author

@SimenB SimenB Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this added a lot of time to the unit tests - promising 😅 (so I ended up mocking out the call)

// triggering a heap snapshot is more aggressive than just `global.gc()`,
// but it's also quite slow, so only do it if we still think we're leaking.
// https://github.com/nodejs/node/pull/48510#issuecomment-1719289759
getHeapSnapshot();

for (let i = 0; i < 10; i++) {
await tick();
}
}

return this._isReferenceBeingHeld;
}

Expand Down