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 module registry memory leak. #8282

Merged
merged 2 commits into from
Apr 7, 2019

Conversation

scotthovestadt
Copy link
Contributor

@scotthovestadt scotthovestadt commented Apr 7, 2019

Summary

This leak is caused by this line:
https://github.com/facebook/jest/blob/01044dabf2253287a5a84e6a87b8505b559e9257/packages/jest-runtime/src/index.ts#L693

The moduleRegistry is preserved in the closure and never GC'd, along with all modules within the registry, making it a pretty nasty memory leak. I'm fairly certain it's also a bug fix: when you reset the registry, this reference to the registry continued referencing the old version. That doesn't make sense after a reset.

This code is now a map and easy to clear but until very recently it was a normal Object which explains why it was 'cleared' in this way.

Memory Impact

command: yarn jest --logHeapUsage --w=1 packages/

--expose-gc enabled to trigger gc manually after each test for accurate readings on the leak

without circus:
295 MB -> 262 MB (11% savings)

with circus:
848 MB -> 762 MB (10% savings)

Test plan

  • All tests pass.
  • Reason for leak identified.
  • Memory impact quantified.
  • Hoping CI for Circus doesn't OOM now. :)

@scotthovestadt
Copy link
Contributor Author

Screen Shot 2019-04-07 at 1 50 27 AM

🎉🎉🎉

Circus CI no longer fails with an OOM error, which means this has definitely made an impact.

Just need a sanity check on the behavior change and if it makes sense. In the case where we want to preserve the old behavior (I still think it's a bug), it can be done in a way that doesn't create a large memory leak.

@cpojer take a look if you have a moment, maybe you can confirm it's a bug for me?

@codecov-io
Copy link

codecov-io commented Apr 7, 2019

Codecov Report

Merging #8282 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #8282   +/-   ##
=======================================
  Coverage   62.13%   62.13%           
=======================================
  Files         266      266           
  Lines       10664    10664           
  Branches     2590     2590           
=======================================
  Hits         6626     6626           
  Misses       3448     3448           
  Partials      590      590
Impacted Files Coverage Δ
packages/jest-runtime/src/index.ts 69.39% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 01044da...bebca61. Read the comment docs.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

This is so awesome!

Changelog? 😀

@SimenB
Copy link
Member

SimenB commented Apr 7, 2019

I'm fairly certain it's also a bug fix: when you reset the registry, this reference to the registry continued referencing the old version. That doesn't make sense after a reset.

I agree. Hopefully nobody (unwittingly) relied on getting old versions of modules after a reset

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

nice!

Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

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

Great work identifying this ❤️

@jeysal
Copy link
Contributor

jeysal commented Apr 7, 2019

Also cc @dubzzz fast-check is not to blame after all ;)

@cpojer
Copy link
Member

cpojer commented Apr 7, 2019

This is great! Memory leaks are the worst.

@peterdemartini
Copy link

@scotthovestadt can we get a release with this PR? Our tests are broken in CI due to memory leaks and it would be nice to be able to test this.

@scotthovestadt
Copy link
Contributor Author

@peterdemartini Yes, soon! There will be a release within a week.

Please report back and let me know if this has solved it for you after the release.

@johannessjoberg
Copy link
Contributor

Any updates concerning the release @scotthovestadt ? 🙏

@ulrichb
Copy link
Contributor

ulrichb commented Jul 29, 2019

Please release 🙏

My Jest integration tests eat 10 GB ... :/

@ulrichb
Copy link
Contributor

ulrichb commented Jul 29, 2019

Oh. This has been released with 24.8 and I have 24.8. Oh my dear. 10 GB !

@SimenB
Copy link
Member

SimenB commented Jul 29, 2019

This was released as part of jest 24.8.0

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants