Skip to content

Remove old chart instance when screen resizes to prevent memory leak #5039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cocarmon
Copy link
Contributor

@cocarmon cocarmon commented May 15, 2025

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’s destroy() method to remove old chart instances, moved it into a private method (_removeOldChartInstance()), and now call that method from both update() and destroy().

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

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • My branch is up to date with any changes from the main branch

@junedchhipa
Copy link
Contributor

@cocarmon Can you create a codepen for the issue where _chartInstances is not being cleared during resize?
When I logged _chartInstances during resize, it's always being cleared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory Leak When Using responsive Option in Charts on Extreme Resize
2 participants