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 with dragDropManager as retainer object

See original GitHub issue

Describe the bug

Possible memory leak with react-dnd.

In a app with react-router-dom when I navigate away of a screen with react-dnd I still see a lot of my models instances, unrelated components (not child neither sibilings of my component containing the DnDProvider) and detached dom elements with references to dragDropManager.

At the current state the memory usage after I leave the screen with the DnD is around 49MB, if I comment out the DND components (which is now as simple as possible: provider + 1 drop zone) it goes to 28MB.

I was able to recreate a small sample which only shows the leak of detached dom elements with the references to dragDropManager.

I couldn’t yet replicate the scenario where multiple components are kept in memory with dragDropManager as retainer object. But here is a picture of the retainer in one of my components: Screenshot 2019-09-11 at 10 37 34

Reproduction

Video showing the possible memory leak: https://monosnap.com/file/1aKyq523ZkKY1gO39QS05yjBmzEBB0

Steps to reproduce the behavior:

  1. Clone https://github.com/canastro/dnd-sample
  2. Run sample in production mode with npm run prod
  3. Open http://localhost:5000
  4. Go to the DND page and navigate back and sometimes between the other two pages
  5. Take heap snapshot, check for dettached div elements, and check a retainer related with react-dnd

Expected behavior All references to react-dnd should be cleaned up properly.

Desktop (please complete the following information):

  • OS: macOS Movaje
  • Browser: chrome
  • Version: 76.0.3809.132

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
lukemeadscommented, Sep 13, 2019

I can confirm I’m having a similar issue, but with the combination of mobx and react-dnd when remounting drag source’s.

Using the legacy HOC API, I was seeing lastConnectedDragSource in SourceConnector.ts holding onto these detached <div> elements. This change https://github.com/lukemeads/react-dnd/commit/eb2662e70fc706f9ac727be63e2c8a844da88fdc seemed to stop the most significant leak for me. Here’s a repro just changing state to trigger rerenders; https://codesandbox.io/s/wrse8 <Box> seems to be OK, but <LegacyBox> definitely leaks memory.

Using react-dnd hooks, I’m seeing a similar behaviour to OP’s issue. But I couldn’t seem to repro it in a smaller app consistently. It’s unclear where the leak in the hooks API is but I’m seeing dragDropManager as the retainer as well:

image

0reactions
canastrocommented, Sep 23, 2019

I can confirm that this issue has been fixed with version 9.4.0:

Before: heap snapshots from version prior to 9.4.0 show that no memory from previous route was released

After: heap snapshots from version 9.4.0 show that all memory was released

@darthtrevino this is good to close by me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Memory tooling improvements in DevTools for Retainers
Any object with no retainers can be discarded by the browser's garbage collector, which reduces memory usage. However, the Retainers pane is ...
Read more >
Detached window memory leaks - web.dev
In JavaScript, memory leaks happen when objects are no longer needed ... This is often caused by the parent page retaining references to...
Read more >
Troubleshooting a JavaScript memory leak - Blog
The retainer size of a heap object is the size of all objects that it dominates. In the first example, the memBuffer object...
Read more >
Debug DOM memory leaks with the Detached Elements tool
A memory leak can occur in your application when an element is no longer attached to the Document Object Model (DOM) tree, ...
Read more >
MemLab: An open source framework for finding JavaScript ...
Finding and addressing the root cause of memory leaks is ... MemLab traverses the heap and generates retainer traces for each leaked object....
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