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(gatsby-link) Clean up IntersectionObservers to fix a memory leak #17056

Merged
merged 2 commits into from
Aug 28, 2019

Conversation

phacks
Copy link
Contributor

@phacks phacks commented Aug 24, 2019

Description

This PR fixes a memory leak caused by IntersectionObservers not being cleaned up in case the intersection is never reached.

Many thanks to @atomiks who did a thorough investigation on the matter and came up with this solution 👏

I tested it on https://reactjs.org with the following procedure:

Here’s a comparison of before and after the fix (tested locally):

🙁Before 🙂After
Memory Leak before PR No memory leak after PR

Related Issues

Fixes #12198.

@KyleAMathews
Copy link
Contributor

Nice! Thanks!

Does this need done in gatsby-image as well?

@phacks
Copy link
Contributor Author

phacks commented Aug 25, 2019

Don’t know yet, will have a look tomorrow!

@phacks
Copy link
Contributor Author

phacks commented Aug 25, 2019

I did a bit of testing and could not reproduce the memory leak for gatsby-image. As it turns out, the leak has already been fixed by @alexandernanberg in #10278!

componentWillUnmount() {
if (this.cleanUpListeners) {
this.cleanUpListeners()
}
}

Copy link
Contributor

@wardpeet wardpeet 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 great! 🎉 I feel like intersection observer should only be created once but that's not really a concern for this PR.

I added a small nit about adding a guard for this.io to make sure it exists.

packages/gatsby-link/src/index.js Show resolved Hide resolved
@wardpeet wardpeet added the status: awaiting author response Additional information has been requested from the author label Aug 26, 2019
@phacks phacks removed the status: awaiting author response Additional information has been requested from the author label Aug 27, 2019
@phacks phacks assigned phacks and unassigned phacks Aug 27, 2019
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

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

Works like a charm! ❤️ thanks for fixing our bad code 😂

@wardpeet wardpeet merged commit 762506f into master Aug 28, 2019
@wardpeet wardpeet deleted the fix/clean-intersection-observers-to-fix-memory-leak branch August 28, 2019 14:12
waltercruz pushed a commit to waltercruz/gatsby that referenced this pull request Sep 8, 2019
…#17056)

* fix(gatsby-link) Clean up IntersectionObserver on componentWillUnmount to prevent memory leaks

Associated issue: gatsbyjs#12198

* Check whether IntersectionObserver exists before unobserving
@sidharthachatterjee
Copy link
Contributor

Thank you once again for this! Featuring it on this month's Gazette in #17548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential memory leak when navigating between pages
4 participants