ember.js icon indicating copy to clipboard operation
ember.js copied to clipboard

Possible memory leak when switching routes

Open Haidy777 opened this issue 10 years ago • 15 comments

I'm using Ember 2.8.3. Every time I'm switching between routes there are some strange detached DOM trees inside the heap snapshop.

I've got a small repository (https://github.com/Haidy777/leaky-ember) which has two branches (master with 2.8.3, ember-2-9-1 with 2.9.1). Even when I'm switch between this "small" routes ("page1", "page2") I've got some of these detached elements.

Since the application which I'm working on is much more complex than this simple demo, there are even more detached elements. Which causes huge trouble on iOS-Devices (since they are very memory limited)

Haidy777 avatar Nov 22 '16 10:11 Haidy777

@locks What else do you need to verify this bug?

Haidy777 avatar Nov 22 '16 11:11 Haidy777

Even when I'm switch between this "small" routes ("page1", "page2") I've got some of these detached elements.

can you share some screenshots of what you see in the heap profiler and such? That helps makes sure all available information is shared to anyone working to confirm/diagnose.

stefanpenner avatar Nov 22 '16 14:11 stefanpenner

Of course. I also added one of the snapshots into the repository mentiond above. After some switching between the two routes there are serval of this 'HTMLDivElement' and 'HTMLHeadingElement' visible. Some of them get cleared through the garbage collection, but not all. On this small scale it doesn't cost much memory, but as more content and more complex DOM gets changed, the footage grows.

To give you an overview: on one route we have a huge table with dynamically loaded entries which use the same component with some more nested components. On the other we have a medium sized grid which also gets dynamically loaded.

I will try to get the demo more clear to reproduce this problem better.

image

Haidy777 avatar Nov 22 '16 15:11 Haidy777

I just update the demo, now there are more Detached DOM trees when changing the routes: image Also there is another snapshot: https://github.com/Haidy777/leaky-ember/blob/master/Heap-20161201T143246.heapsnapshot

Haidy777 avatar Dec 01 '16 13:12 Haidy777

@locks @stefanpenner do you need a twiddle in order to verfiy this bug? if so, I could try to get this example running in a twiddle too.

herom avatar Dec 05 '16 07:12 herom

Reproduction in app form as I believe you have added should be enough. I just have not had a chance to loook.

If I don't comment by mid-week feel free to pig me so I do :)

stefanpenner avatar Dec 05 '16 14:12 stefanpenner

@stefanpenner alles klar, danke :)

herom avatar Dec 06 '16 07:12 herom

FWIW I was unable to find any memory leaks after employing the three-snapshot method. Nor could I find a large number of detached red nodes after switching between routes a bunch of times like you could:

memory-leak

However, I looked at your heap snapshot file and found that the nodes are being retained in a property called triggerElement on some object. So I grep'd node_modules for that and behold:

$ grep -r triggerElement node_modules
node_modules/ember-basic-dropdown/addon/components/basic-dropdown/content.js:    this.triggerElement = this.triggerElement || document.getElementById(this.triggerId);
node_modules/ember-basic-dropdown/addon/components/basic-dropdown/content.js:    if (this.hasMoved || this.dropdownElement.contains(e.target) || this.triggerElement && this.triggerElement.contains(e.target)) {

Is it possible the memory leak is originating there?

pittst3r avatar Dec 07 '16 11:12 pittst3r

@robbiepitts Thanks a lot, that seems possible indeed. I will file an issue over at ember-basic-dropdown and keep you guys updated.

Haidy777 avatar Dec 07 '16 11:12 Haidy777

I've investigated this in ember-basic-dropdown but I can't pinpoint how a memory leak can happen. It is true that I hold a reference to the "trigger" of the dropdown in the "content", but both components share the same parent and are destroyed at the same time, so those references should be garbage collected.

Also, i've checked and non-local events (those added to body/window) are cleaned in the willDestroyElement. I can't pinpoint what might be causing this problem.

I remember that at some point in history there was a bug in which the willDestroyElement hook was not invoked for contextual components, but I'd say that it was fixed before 2.9.1.

If someone with more experience in tracking memory leaks is available, I'd be happy to pair on this to see if we can find the leak.

cibernox avatar Dec 08 '16 09:12 cibernox

@Haidy777 Do you have specific steps to reliably reproduce this with your demo repo? I've not been able to reproduce yet.

pittst3r avatar Dec 09 '16 13:12 pittst3r

@robbiepitts Sorry for the late respone, I'm using following steps:

  1. open the demo
  2. click on Page 1
  3. click on Page 2
  4. repeat step 2 to 3 about 10 to 20 times
  5. take a heap-snapshot

Haidy777 avatar Dec 12 '16 08:12 Haidy777

@Haidy777 @stefanpenner @robbiepitts @cibernox @herom @locks is this still an issue, perhaps we should close or create a new reproduction of this, what do you think?

pixelhandler avatar Sep 14 '18 17:09 pixelhandler

@pixelhandler I've checked the problem today with Ember 2.16. (as our Application was Updated to it). It doesn't seem to appear anymore so we could close this issue.

Haidy777 avatar Sep 17 '18 07:09 Haidy777

Thank you for the updated feedback!

locks avatar Sep 17 '18 09:09 locks