'React memory leaks: node count keeps increasing when using keys

I observed that when (un-)mounting components that have children with keys the node count keeps increasing and nodes do not get garbage collected. To demonstrate this issue I created a small codesandbox project with a simple button that toggles on/off a component. Below I included the corresponding performance plot:

Node count

Codesandbox: https://codesandbox.io/embed/goofy-liskov-lxrms?fontsize=14&hidenavigation=1&theme=dark

Is this expected behaviour? Can I do anything about it and destroy the nodes?

Thanks for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source