Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap Memory Management

For few months now I have been developing an Android app using PhoneGap 2.8 and on the javascript side I have used Backbone and jQuery as my main frameworks. As my application has grown to a reasonable size, I have started to notice a considerable memory consumption. Having read different articles that explain why PhoneGap requires considerable amount of memory even to run, I still believe that I can do some optimization to how i use memory.

In BackBone we have a Router object that maps URI-s to specific functions, which render me something called a View object. Not only I implemented my router functions to create a view and render it, but I also store globally reference to currently being displayed view. So before a new view is created, I tell the old view to make some clean up (That is done recursively since views can contain more "sub" views). Under clean up I currently tell view to undelegate his events (I trust Backbone removes the event listeners). Not much more is done currently. Once new view is rendered, global variable will reference the new view. I trust that javascript GC will release the memory, used by the old view. Alas, I dont things this is happening- the more I navigate around my app, the more memory is being used up. I am aware that there is some memory leaking going on, but I can't figure out what is it, that takes memory. One thing I suspect is that old objects are not being garbage collected properly for some reason. I suspect that once I render new html (DOM) over some container, perhaps old DOM is causing memory leaks, perhaps some event handlers are being unnecessarily stored somewhere.

What I would like to know, if there is any tools or commands or tips on how can I debug/ trace/ measure where memory is being allocated. Is there a way to access all event listeners and measure them somehow (same for DOM). Any article to smart memory efficient techniques would also be appreciated. Currently only thing that I can thing of to do, is to start recursively deleting all attributes of the objects (in the end objects as well) I am willing to destroy.

Any suggestion is very welcome! Thank you in advance.

like image 634
Erich Jagomägis Avatar asked Oct 21 '22 00:10

Erich Jagomägis


1 Answers

I faced similar issues with my first phonegap app. Few techniques we managed to apply were

*old view - view getting navigated away

  • Unbind all events associated with old view
  • Remove all nodes attached to the view from dom, to make sure event are also removed
  • Remove old view object, model/collection, so that there are no instances remaining on the DOM
  • Moreover try to use prototyping as much as possible, as functions created via prototype occupy space in RAM only once. So if the view is created/initiated again, its associated/child functions aren't going to be pushed into RAM again
  • Most imp, make sure 'this' pointer isn't leaking anywhere between files. One of my workplace used to get stuck after 1.5 hrs of play and after a week debugging, we came to find out that there was a leakage of this pointed between 2 files/objects/views, which created a circular referencing and make the DOM to explode.

Also you can try to use Google Chrome's profiling tool

Few useful links

  • http://blog.socialcast.com/javascript-memory-management/
  • Backbone.js Memory Management, Rising DOM Node Count
like image 169
Siddhartha Gupta Avatar answered Oct 27 '22 10:10

Siddhartha Gupta