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: unbind all retainers when instance is destroyed #135

Merged
merged 2 commits into from
Jun 14, 2023
Merged

fix: unbind all retainers when instance is destroyed #135

merged 2 commits into from
Jun 14, 2023

Conversation

fspoettel
Copy link
Collaborator

@fspoettel fspoettel commented Jun 14, 2023

Write one to two sentences summarizing this PR

This PR fixes scatterplot.destroy() to properly remove all event listeners bound during instance creation.

Description

What was changed in this pull request?

  • unbind wheelHandler bound to canvas when destroying
  • call lassoManager.destroy when destroying

Why is it necessary?

Destroyed scatterplot instances are currently retained in memory in full (including the allocated KDBush indexes and the webGL context), which leads to memory leaks in applications that create and destroy scatterplots frequently. This happens because there are some event handlers still bound to (detached) DOM elements, so the garbage collection skips the whole thing. The behavior can be observed as follows:

Adjust the example to call destroy after a timeout.

setTimeout(() => {
  scatterplot.destroy();
  // scatterplot needs to be a let binding for this to work
  scatterplot = undefined;
  canvas.parentNode.removeChild(canvas);
}, 2000);

Open the Memory tab in chrome devtools and open the example. After the canvas is removed, perform a manual garbage collection and take a heap snapshot. You will see that the instance and a canvas element is still held in memory, with the retainers pointing to certain event handlers:

Screenshot 2023-06-14 at 17 53 37

Open a new tab and repeat with the fixes applied. When looking at the snapshot now, the instance and canvas are no longer present:

Screenshot 2023-06-14 at 17 57 36

Fixes #___

Checklist

  • Provided a concise title as a semantic commit message (e.g. "fix: correctly handle undefined properties")
  • CHANGELOG.md updated
  • Tests added or updated
  • Documentation in README.md added or updated
  • Example(s) added or updated
  • Screenshot, gif, or video attached for visual changes

@flekschas flekschas self-requested a review June 14, 2023 16:53
@flekschas flekschas added the bug Something isn't working label Jun 14, 2023
@flekschas
Copy link
Owner

That's a lot for putting together the PR! I wonder how I missed this haha.

Copy link
Owner

@flekschas flekschas left a comment

Choose a reason for hiding this comment

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

Looks great!

@flekschas flekschas merged commit cbb4611 into flekschas:master Jun 14, 2023
@fspoettel fspoettel deleted the fix/destroy-memory-leaks branch June 14, 2023 16:58
@flekschas
Copy link
Owner

New version (v1.6.10) with the fix is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants