Does React-Bootstrap Modal leak memory during rapid toggle cycles?
0 reputation · 02 Dec 2022, 09:14 UTC
0 reputation · 02 Dec 2022, 09:14 UTC
In React-Bootstrap v2.x, memory management for portal-based components is critical for long-running single-page applications. While recent updates addressed portal reference leaks in the Dropdown component, concerns remain regarding the Modal component's behavior during high-frequency state changes.
The uncertainty centers on whether animation callbacks are consistently cleared when the show prop is toggled rapidly. If the component unmounts or changes state before an animation completes, there is a risk that pending callbacks retain references to the DOM or component state, preventing garbage collection.
To validate this, a test harness is needed to mount and unmount the Modal repeatedly while monitoring the JS heap size via Chrome DevTools. It is necessary to isolate the component to avoid false positives caused by React's internal fiber tree.
show is toggled faster than the transition duration?33275 reputation · 02 Dec 2022, 16:46 UTC
There is no widely documented, systemic memory leak in React-Bootstrap v2.x specifically caused by rapid toggling of the show prop. However, because the Modal uses React Portals and relies on asynchronous CSS transition callbacks to trigger unmounting, rapid state changes can create a race condition where multiple transition cycles overlap. While React generally handles the cleanup of the portal node, orphaned references can persist if the JavaScript heap is not cleared between rapid cycles.
It is important to distinguish between a permanent memory leak and transient heap growth:
show is toggled faster than the transition duration (typically 150–300ms), the component may enter a state of rapid re-rendering while the DOM node is still transitioning.onTransitionEnd callback). In most cases, these are collected during the next major Garbage Collection (GC) cycle and are not permanent leaks.To determine if your specific implementation is leaking memory, use the following scoped diagnostic process in Chrome DevTools:
show state 100 times rapidly (e.g., every 50ms).Detached HTMLDivElement. If the count of detached elements increases linearly with the number of toggles and persists after forced GC, a leak is present.animation={false} prop. If the leak disappears, the issue is tied to the transition callback logic.To refine this recommendation: Are you using a custom animation duration or a third-party animation library alongside React-Bootstrap? Custom durations that mismatch the internal state timing can increase the likelihood of orphaned callbacks.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.