Remove old chart instance when screen resizes to prevent memory leak #5039
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.
New Pull Request
This PR fixes the issue of the memory leak described in #5028 .
Each time the screen is resized, a new chart is created and added to the global
Apex._chartInstances
array. However, old chart instances were not being cleaned up on window resize, which led to a significant increase in memory usage over multiple resizes.I extracted the logic used in the
ApexCharts
class’sdestroy()
method to remove old chart instances, moved it into a private method (_removeOldChartInstance()
), and now call that method from bothupdate()
anddestroy()
.Fixes #5028
Below is a comparison of the memory after resizing the window 7 times. After multiple tests the average is a ~97% reduction in memory usage.
Before:

After:

Type of change
Checklist: