fix: prevent memory leaks from uncleaned uPlot event listeners #9320
+77
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes critical memory leaks in uPlot chart rendering that were causing memory spikes during chart re-renders and updates.
Problem
Memory profiling revealed significant memory leaks in
getUplotChartOptions.ts
. The function was adding multiple event listeners that were never cleaned up when charts were destroyed or re-rendered:mouseenter
andmouseleave
events on each legend itemmousemove
event listener ondocument
that accumulated on every chart renderdocument.body
that were never removedEach time a chart was re-rendered or updated, new event listeners were added while old ones remained attached, causing memory to grow continuously.
Solution
Implemented comprehensive event listener cleanup using uPlot's lifecycle hooks:
Changes Made
_legendCleanups
array inExtendedUPlot
interface to track all event listenersdestroy
hook to uPlot options that removes all tracked listenersUplot.tsx
destroy callback to manually trigger all cleanup functions.legend-tooltip
elements from DOMFiles Modified
frontend/src/lib/uPlotLib/getUplotChartOptions.ts
- Main memory leak fixesfrontend/src/components/Uplot/Uplot.tsx
- Enhanced cleanup in React componentImpact
5 Heap Snapshot before Fix: (10 Sec interaction between each snapshot)
6 Heap Snapshot after Fix: (10 Sec interaction between each snapshot)