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

Possible memory leak? #137

Closed
francescozaniol opened this issue Oct 16, 2019 · 15 comments · Fixed by #141
Closed

Possible memory leak? #137

francescozaniol opened this issue Oct 16, 2019 · 15 comments · Fixed by #141
Assignees

Comments

@francescozaniol
Copy link
Collaborator

Hi, unless I'm doing something wrong, this extension seems to be causing a memory leak in my SPA vue app. To be more precise it seems that when an element with "v-click-outside" gets destroyed (by a route change) not all references to the doms are properly removed and they stay as "detached" elements in the memory. See the two imgs attached:
Screenshot from 2019-10-15 17-56-01
In this memory snapshot I've got 4645 detached elements, and all of them have v-click-outside in common. This causes memory to keep going up on every route change.
In the following screenshot I've removed the v-click-outside directive and I end up with just one detached elements, which seems to be keeping memory stable:
Screenshot from 2019-10-15 17-56-07
Am I missing something?
Thanks!

@ndelvalle
Copy link
Owner

@francescozaniol thanks for reporting the issue, I will take a look 👍

@theprobugmaker
Copy link

Is this affecting only SPA?

@ndelvalle ndelvalle self-assigned this Nov 2, 2019
@ndelvalle
Copy link
Owner

ndelvalle commented Nov 4, 2019

@zefexdeveloper yes this issue affects SPA mostly if you refresh the page the problem goes away.
@francescozaniol I am investigating the issue, the event listeners are properly removed but still, there are some Detached HTMLDiv Element left. I am updating the directive implementation in this branch, I also added an example project to be able to reproduce the problem if you want to play around.
I tested some other event listeners related directives and they have the same problem, even tho the listener is properly removed.

@francescozaniol
Copy link
Collaborator Author

Thanks @ndelvalle , I'll have a look as soon as I can

@francescozaniol
Copy link
Collaborator Author

Hi @ndelvalle , I had a look at your example branch; actually in that example the detached nodes remain in memory because you're using console.log of "el" (file example/src/views/Home.vue); if you remove the console.logs (or you can just keep the strings) the memory snapshot remains clear from detached nodes. Can you confirm it too?
If this is the case then you successfully fixed the issue, or, it might be an issue related to my app only.
Thanks again!

@francescozaniol
Copy link
Collaborator Author

Hi again @ndelvalle , good news: I made a build from the branch you created and placed it directly inside node_modules of my app, no more detached nodes and memory is drastically lower. I can confirm the issue is gone. Thanks, well done

@ndelvalle
Copy link
Owner

Wow, thanks @francescozaniol for helping me debug the problem 🙏🏻! I will update the tests according to the changes I made and push a new version tonight.

@ndelvalle
Copy link
Owner

v-click-outside 2.1.5 published 🎉

@ndelvalle
Copy link
Owner

I should also do not expose the el to middlewares and the handler function anymore to totally avoid this issue 🤔

@francescozaniol
Copy link
Collaborator Author

Hey @ndelvalle , sorry to bother you again with this but I'm still experiencing the issue. I've updated at 2.1.5 and I've had a look at a diff and I noticed you added a setTimeout, which seems to be the culprit. Was there a particular reason for that? removing it fixes the memory leak in my app. Let me know.

@ndelvalle
Copy link
Owner

Strange, @francescozaniol this was introduced at #42

I wonder if this still happens so we can remove it, or maybe we should tackle differently. I am also preparing a new version removing the el argument in the callbacks to avoid having unexpected Detached HTMLDiv Element as you suggested.

@ndelvalle
Copy link
Owner

@francescozaniol are you sure you are not saving the el reference somewhere? I am having the same results with and without the set timeout. I like having the timeout, to avoid executing the event listener handler when navigating to the route that is using the directive without clicking anything. There are some other ways we can achieve this, but I am not sure if the time out is the problem here.

@francescozaniol
Copy link
Collaborator Author

@ndelvalle I'll do some more testing asap and I'll let you know

@francescozaniol
Copy link
Collaborator Author

@ndelvalle after quite some testing, I've found out that the issue was indeed linked with the timeout, but only with elements populated by async data. In this scenario there were cases when the "unbind" function was run before the timeout function (for reasons I cannot still quite understand ..), therefore the event was added to a detached node, causing the memory leak. Check my PR, that commit fixes the issue (at least in my app)

@francescozaniol
Copy link
Collaborator Author

Installed v3.0.0 in my SPA, confirmed memory leak is gone. Thanks again @ndelvalle

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 a pull request may close this issue.

3 participants