react: Bug: Memory leak in react while focusing input elements

I 've noticed that having an input element focused and unmounting its owner component doesn’t actually free up this component from the js heap , I 've created this simple blnkr to demonstarate this which add a dummy X to a ref inside a component that has an input element

React version: 17.0.2

Steps To Reproduce

  1. Go to this plnkr
  2. Click Tab Two
  3. Click the Grow button a few times
  4. Take a heap snapshot
  5. Click the Grow button a few more times
  6. IMPORTANT Focus the input element by clicking into it
  7. Click Tab One ( This unmount the component that own the ref to the list )
  8. Take another heap snapshot ( after running the GC)
  9. Compare between the two snapshots
  10. The added string X will be found within the `concat check the screenshot Screenshot 2023-01-28 at 1 13 12 AM

Link to code example: can be found above

The current behavior

The current component that own the input element is still being there in the memory and doesn’t free up its own resources afer being unmounted.

The expected behavior

The current component shloud free up its resources whenever being unmounted

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

哈哈,都是Chinese,我们用中文交流吧,你去抖音搜索一下 “游离节点造成的内存泄露”。就明白解决办法了

@YagamiNewLight I think This issue has already been resolved within react 18 , however I 'm not sure if there’s a workaround for this in previous versions