question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak - Detached HTML elements

See original GitHub issue

Expected behavior

I’m using interactjs in a SPA where I need to destroy/recreate several times an interact element. When I need to destroy an interactable element I do the following:

        interact(element).unset();
        interact.removeDocument(document);

This call should be enough for the GC to collect and garbage all the retained memory by interactjs.

Actual behavior

In the Heap Snapshots of Chrome there is a Detached HTMLElement stored and never released by interactjs every time I unset. The HTMLElement is referenced in the object Interactable inside the this._latestPointer variable this._latestPointer.eventTarget.

Solving proposition

For the moment, here is what happens inside the unset method:

https://github.com/taye/interact.js/blob/587eb948ba6adbfdda89c54c932fc3715d5651ab/packages/core/scope.ts#L93

I think we should call a new destroy method from interaction

          if (interaction.interactable === this) {
            interaction.stop()
          }
            interaction.destroy()

Inside the destroy method of interaction, it needs to clear up the memory like so :


    this._latestPointer = {
      pointer: null,
      event: null,
      eventTarget: null
    }

I’ve tried this solution and its working, the GC can collect and garbage.

I’m not proposing a Pull Request because maybe unset is not the right place for cleaning the memory, and maybe a new destroy or detach method should be provided by scope.

I let you guys decide.

System configuration

interact.js version: 1.4.0-RC.13 Browser name and version: Chrome Operating System: MacOS

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jf-mcommented, Apr 26, 2019

Found another one here downPointer in auto-scroll

image

2reactions
jf-mcommented, Apr 26, 2019

I’ve created a pull request #715 that solves this issue (at least in my test-case).

Hope it helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can Detached DOM elements cause memory leak in ...
Detached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript.
Read more >
Detached window memory leaks - web.dev
The two most common cases where detached windows cause memory leaks are when the parent document retains references to a closed popup or...
Read more >
Get detached DOM elements to investigate memory leaks
Get detached DOM elements to investigate memory leaks · Open the command menu and type "detached" to find and open the Detached Elements...
Read more >
Locating detached DOM tree memory leak - Stack Overflow
I'm having trouble diagnosing a detached DOM tree memory leak in a very large single-page web app built primarily with Knockout.
Read more >
Debug memory leaks with the Microsoft Edge Detached ...
Debug memory leaks with the Microsoft Edge Detached Elements tool ... Memory leaks occur when the JavaScript code of an application retains more ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found