Search code examples
javascriptandroidioswebkit

Mobile webkit memory consumption


We are working on HTML5 application for mobile devices(Android + iOS). But the great problem is memory consumption - used memory amount is rising very fast and app become laggy.

What are the best practices, hints, tools, solutions, etc. for fighting with memory leaks in HTML(JavaScript) applications ?

P.S. We are targeting only on Webkit browsers


Solution

  • There have been some really great articles on this subject recently. There are some really surprising sources of object creation that don't really catch your attention unless you're tuned for it. Typically, the problem isn't the memory use, it's actually the garbage collection cycles required to collect the memory the app is slowly leaking.

    This article is the best I've read on the topic recently: https://www.construct.net/en/blogs/construct-official-blog-1/write-low-garbage-real-time-761

    As far as tools to combat/diagnose the issue, Google Chrome's Speedtracer comes to mind. Of course, tuning for Chrome doesn't guarantee tuning for all browsers, but most of the things that result in object creation in Chrome are common to the JS spec as it's implemented by all the browsers.

    One important thing to consider is that RAM use and video RAM use are not the same. One best practice is to determine which portions of your UI are being hardware accelerated and to make sure they're small (i.e. fit on screen all at once). If you have huge scrolling portions of the screen hardware accelerated, you will get GPU tearing/tiling and laggy scrolling. You can detect this in part using the iOS simulator. This article covers the idea briefly: https://web.archive.org/web/20131224004217/http://devinsheaven.com/turn-your-iphone-wacky-and-make-your-iphone-application-better/

    Lastly, there are a bunch of really common memory leaky patterns in JavaScript that every engineer runs into from time to time. IBM has a good list of them. I can't post more than two links because I'm a n00b, but you can google for "Common JavaScript Memory Leaks" and it's probably the first result.