Skip to content
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

Fix memory leaks #2138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Fix memory leaks #2138

wants to merge 3 commits into from

Conversation

Arooba-git
Copy link

What kind of change does this PR introduce? (check one with "x")

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?
Significant memory leakage

What is the new behavior?
Significant reduction in memory leakage

Does this PR introduce a breaking change? (check one with "x")

  • Yes
  • No

Hello 👋
As part of our project, we are using Facebook's new Memlab tool to detect memory leaks in SPA applications and their libraries. While running the tool and analyzing the code of ngx-datatable, we saw that your project does a very good job of ensuring that all async operations are cancelled when components destroy. However, as per Memlab execution results, we found some dangling animation frame requests, timeout calls and QueryLists, that were causing the memory to leak (screenshots below).

[before]
Screen Shot 2023-02-03 at 7 49 08 AM

Screen Shot 2023-02-03 at 8 32 11 AM

Hence we added the fix by clearing the frame requests, timeout calls and querylists in destructor, and you can see the heap size and # of leaks reducing noticeably:


Screen Shot 2023-02-03 at 6 11 11 AM

Note 1. Host listener is used to ensure that ngOnDestroy is called not only when the class destroy, but also when the page refreshes, tab or browser closes or the user navigates away from the page[1]

Note 2. Static property is used to avoid losing the context of the correct ‘this’ in case any nesting changes are made to the enclosing function in future.

We executed the test suite before and after the changes, where it executed 126 of 129 (skipped 3, TOTAL: 126 SUCCESS) in both cases.

You can analyze this and other potential leak sources, if you like, by running Memlab with a scenario file covering the maximum # of use cases.

Following is a sample of the scenario file we used (it needs to be a .js file but attaching here in .txt form):
ngx-database-scenario-memlab.txt

Note that some other reported leaks (in Memlab) originated from Angular's internal objects, hence were ignored.

Reference.
[1] https://wesleygrimes.com/angular/2019/03/29/making-upgrades-to-angular-ngondestroy.html

@Arooba-git
Copy link
Author

bump @haeminn

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.

2 participants