react2angular
react2angular copied to clipboard
Memory leak
I don't know if this is at all an issue with react2angular, but I am experiencing some nasty leaks with a React component wrapped with react2angular. I have a workaround listed at the end, but it's ugly and I'd like to find a better solution.
My component looks like this:
export class EncounterView extends React.Component {
memLeak = new Int8Array(100000000);
componentWillUnmount() {
debugger;
}
render() {
return <h1>no-op</h1>;
}
}
// Register component for AngularJS integration
module('talixCodingInsight')
.component(
'encounterView',
react2angular(
EncounterView
)
);
Then this component is embedded into my AngularJS app like this, hooked up with $stateProvider.state
:
<encounter-view></encounter-view>`,
The problem here is that when I go to this route and leave it, componentWillUnmount()
is called but the buffer is not released.
Here are the retainers:
memLeak in EncounterView@711833 | 11 | 1360 % | 100 001 32852 %
stateNode in FiberNode@708915 | 10 | 2880 % | 100 001 61652 %
lastEffect in FiberNode@707329 | 9 | 2880 % | 100 007 13652 %
currentFiber in system / Context@90437 | 8 | 11 6080 % | 100 539 41652 %
context in overrideHookState()@68727 | 7 | 640 % | 1680 %
However, I did find a workaround. Which is nasty. The fix was to use ng-if
to remove the component when changing the route. If I do that, then the buffer is released.
I don't like this solution though. Any ideas as to what's happening?
Hi @scottschafer! Im facing the same issue, can u please provide an example of ur hacky ng-if
fix? I tried do that ng-if
hack somewhere on the top level of routing, but memory was still leaked (but less than without that). Just wanted to double check where in the app u apply that trick so it helps to avoid leaks
@scottschafer how did you do to get the leaks that way?
memLeak in EncounterView@711833 | 11 | 1360 % | 100 001 32852 % stateNode in FiberNode@708915 | 10 | 2880 % | 100 001 61652 % lastEffect in FiberNode@707329 | 9 | 2880 % | 100 007 13652 % currentFiber in system / Context@90437 | 8 | 11 6080 % | 100 539 41652 % context in overrideHookState()@68727 | 7 | 640 % | 1680 %
To @liquidprogrammer , the fix looked something like this in the template:
<encounter-view
id="encounter-view-ng"
ng-if="$ctrl.includeEncounterView"
Where EncounterView is the react2angular component.
Then when changing states away from this route, I set includeEncounterView to false and wait until the view is torn down before proceeding to the route.
$scope.$on('$stateChangeStart', (event, toState, toParams, fromState, fromParams) => {
if (toState.name !== 'home.patients.details.coding.encounter') {
this.includeEncounterView = false;
if ($('#encounter-view-ng').length) {
event.preventDefault();
const checkTornDownInterval = $interval(() => {
if (!$('#encounter-view-ng').length) {
$interval.cancel(checkTornDownInterval);
$state.go(toState, toParams);
}
}, 0);
}
}
@brianunlam, It's been a while since I looked at this. SInce I reported this bug, I discovered that React seems to hang onto references and free them periodically. This means that you might need to take a memory snapshot, perform an operation that shouldn't leak and then WAIT before taking another snapshot - if you do this too quickly, you might see leaks that don't really exist.
But in this case, it did seem like the memory was steadily increasing. I don't know, this leak/non-leak was driving me a little bit crazy. Now I have code that I'm afraid to touch. ;)
I asked about React hanging onto references here BTW: https://stackoverflow.com/questions/58984601/reactdom-seems-to-be-caching-and-releasing-elements-so-is-this-a-leak
Thanks for a great and useful library. I don't think it's the culprit here.
@scottschafer hi, thanks! I'll try that out. I didn't try to ng-if
the component itself, i only tried to add ng-if
at the top level root component, but that helped only a bit.