-
Notifications
You must be signed in to change notification settings - Fork 3.4k
dialog: memory leak when injecting $mdDialog #11493
Comments
This looks like a duplicate of #11207, but the information on debugging is much appreciated! |
It's not a duplicate. #11207 involves actually creating, opening, and closing a dialog; this issues involves a case where no dialog is opened; simply injecting the $mdDialog service several times is enough to cause a leak. |
I only said that it "looks like a duplicate". I didn't say that it was a duplicate and I didn't close it as a duplicate. But thank you for your clarification. #11024 (comment) also reports a memory leak where |
I figured out what the root cause of this issue is, and a workaround. The root cause is that the So if you have enough tests that inject To avoid this, you can replace the // The $mdInteraction service causes a memory leak because it registers dom event listeners and does not
// clean them up. In a unit test environment where the service is re-initialized frequently, these
// event handlers build up and consume memory.
//
// Since, in a unit test environment, there is never any user interaction with the dom anyway,
// it's safe to replace $mdInteraction service with a different service that does not
(function() {
'use strict';
angular.module('material.core.interaction').service('$mdInteraction', function() {
});
})(); In your |
this was causing memory leaks when unit testing Fixes: angular#11493
this prevents unit tests from leaking memory Fixes: angular#11493
this prevents unit tests from leaking memory Fixes: angular#11493
this prevents unit tests from leaking memory Fixes: angular#11493
this prevents unit tests from leaking memory Fixes: angular#11493
this prevents unit tests from leaking memory Fixes angular#11493
this prevents unit tests from leaking memory Fixes: #11493 <!-- Filling out this template is required! Do not delete it when submitting a Pull Request! Without this information, your Pull Request may be auto-closed. --> ## PR Checklist Please check that your PR fulfills the following requirements: - [x] The commit message follows [our guidelines](https://github.com/angular/material/blob/master/.github/CONTRIBUTING.md#-commit-message-format) - [ ] Tests for the changes have been added or this is not a bug fix / enhancement - [ ] Docs have been added, updated, or were not required ## PR Type What kind of change does this PR introduce? <!-- Please check the one that applies to this PR using "x". --> ``` [x] Bugfix [ ] Enhancement [ ] Documentation content changes [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [ ] Build related changes [ ] CI related changes [ ] Infrastructure changes [ ] Other... Please describe: ``` ## What is the current behavior? Currently the $mdInteraction service registers when invoked. This causes a memory leak in unit tests because nothing is in place to clean up the events. <!-- Please describe the current behavior that you are modifying and link to one or more relevant issues. --> Issue Number: #11493 ## What is the new behavior? Now on the $destroy event of rootScope the events registered will be unregistered. ## Does this PR introduce a breaking change? ``` [ ] Yes [x] No ``` <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. --> <!-- Note that breaking changes are highly unlikely to get merged to master unless the validation is clear and the use case is critical. --> ## Other information
Bug:
https://codepen.io/kimtuck/pen/yRRbjb
Detailed Reproduction Steps:
What is the expected behavior?
Memory remains more or less constant, because all we're doing here is injecting
$mdDialog
, which is a service (a singleton).What is the current behavior?
Memory is dramatically consumed
What is the use-case or motivation for changing an existing behavior?
Memory leak will lead to application crashes or poor performance. In a set of unit tests that inject
$mdDialog
in many places (as our application does) chrome crashes during the unit test run, leading to a failed unit test.Which versions of AngularJS, Material, OS, and browsers are affected?
See the package.json in the codepen. Relevant lines from package.json:
"angular": "^1.7.2",
"angular-animate": "^1.7.2",
"angular-aria": "^1.7.2",
"angular-messages": "^1.7.2",
"angular-material": "^1.1.10",
Is there anything else we should know? Stack Traces, Screenshots, etc.
This is the (partial) code for MdDialogProvider. If you remove the
.setDefaults()
and the.addPresets()
then there is no memory leak. If you include the.setDefaults()
and remove the.addPresets()
then the memory leak occurs.So, the memory leak is in the
.setDefaults()
call.Note that on each test invocation, the MdDialogProvider method executes; and thus the
setDefaults()
method runs on each test invocation.The text was updated successfully, but these errors were encountered: