Skip to content

Latest commit

 

History

History

memory-leak

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Prevent subscribing to event without de-registration (memory-leak)

When subscribing to events, you must remember to add an associated deregistration somewhere in your component's lifecycle. Otherwise, continual event subscriptions will build up and cause memory usage issues. So, when the following happens:

const removeEventListener = x.on('some-event', () => console.log('even happened'));

Any of these must also be found in the same file:

removeEventListener();
x.off('eventName');
x.off();
x.destroy();
x.removeListener();

In the case of AngularJS's $scope subscriptions, add it to the destroy event:

$scope.$on('$destroy', () => {
  removeEventListener();
});

Here is an example of where an unsubscribe happens in React class components:

componentWillUnmount() {
  removeEventListener();
}