-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Memory leak on unmounted DOM element or component #11318
Comments
EventTarget.prototype.addEventListener = function (type, listener, options) {
- console.log(`Added event listener: ${type}`, this);
originalAddEventListener.call(this, type, listener, options);
};
EventTarget.prototype.removeEventListener = function (type, listener, options) {
- console.log(`Removed event listener: ${type}`, this);
originalRemoveEventListener.call(this, type, listener, options);
}; console.log() causes memory leaks |
Thanks, that makes sense! Still, regardless of the memory leaks it does seem strange to me that the removal of the listener is logged when removing it manually but not when the div is unmounted. Or is that expected behaviour? |
That's because the elements are removed from the DOM when the component is unmounted, so they will be garbage collected and so there's no need to remove the listeners. Removing them individually would just be overhead |
Thanks a lot, that's a clear explanation and cool to hear how that works! Then I'll have to search elsewhere for what's causing my leak 😄 |
Vue version
3.4.29
Link to minimal reproduction
Reproduction
Steps to reproduce
Click the toggle button a few times.
What is expected?
There is a div with an @click event listener, and my expectation is that the event listener should be added when the div is mounted to the DOM and removed when the div is unmounted.
What is actually happening?
The console shows that the event listener gets added, but never that it gets removed. Performance recordings show that the amount of listeners keeps increasing, never decreasing.
![Screenshot 2024-07-08 163538](https://cdn.statically.io/img/private-user-images.githubusercontent.com/44325973/346605791-6ea5b531-2196-42c6-9ccb-a27a4c9e7f57.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjIyNzk2MzcsIm5iZiI6MTcyMjI3OTMzNywicGF0aCI6Ii80NDMyNTk3My8zNDY2MDU3OTEtNmVhNWI1MzEtMjE5Ni00MmM2LTljY2ItYTI3YTRjOWU3ZjU3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzI5VDE4NTUzN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRhNTZjMTJmYzY2Mjg5MjViZDM0MGM2MmNmZmFiODk2YzY5ZWNmMGZhNmEyYTgwZTc3MWFlOGVhNjYyNDRhOGImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.raWzJ20fqrLLVR8YSnJcuRyeFIrZHq0y6mt0TgPZqsk)
System Info
Any additional comments?
Using vue in the building of an online game where elements with click/mouseenter/mouseleave events often are added/removed from the DOM, and noticed significant memory leaks with ever-increasing listener amounts.
Thanks in advance for any help!
The text was updated successfully, but these errors were encountered: