originUnknown
originUnknown

Reputation: 123

How can I find memory leaks in a single-page javascript app?

The app is a single page, so it's not being refreshed, and our hope is to leave it running on an android device for extended periods of time.

We're currently seeing a crash (and a logcat report indicating an OOM error when running on android) after about 18 hours. When I use remote debugging and capture a timeline using chrome's dev tools it is quite apparent that we have a memory leak because we see a steadily growing js heap. The heap seems to continue growing until I force a garbage collection.

Another thing that seems peculiar to me is that on timeline I can also see that our event listeners are growing and appear unaffected by the forced GC. Could this be the problem all on its own? (We're talking 10's of thousands of listeners according to the Timeline)

Does anyone have any advice beyond the standard "3 snapshot heap analyzing technique"? It's not very helpful here because the heap is growing even when it is not being interacted with, which I believe to be due to some of the periodic updating we do behind the scenes to read and display data. Otherwise I'm entirely open to any other memory analyzing tools altogether!

I have very little experience in the area so any advice at narrowing down the cause of these nasty leaks would be terrific!

Unfortunately, due to the nature and size of the program, it is very difficult to provide helpful code snippets. I apologize for the long question.

EDIT: One of my strongest suspicions is that when I look in the dev tools timeline and see that the listeners keep increasing, and never get garbage collected... Could that eventually be causing a crash?

Upvotes: 4

Views: 266

Answers (1)

Renato Oliveira
Renato Oliveira

Reputation: 66

mate. I once made a single-page app too and also kept something running forever there and had the same problem you had. I had a infinite 'while(true)' loop doing something. And even though I was nullifying the vars and objects at the end of the loop, javascript's GC wasn't collecting garbage.

Solution? I change from a while(true) loop to a timed event. Every 1sec do something, using javascripts timeout method.

From this I've learned that javascript's does not run GC on while(true) methods. Do you have something like that in your code?

rugs

Upvotes: 2

Related Questions