Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Need to release memory? (On mobile app)

I am trying to create my app using javascript classes. When I create a class instance, do I need to release it's memory allocation when I am done with it?
for example I could have the following code

Titanium.include('mysuperclass.js');
var myinstance = new mysuperclass(parameter);
//do stuff with my instance
// ...
// and then when I am finished:
myinstance = null;

Is the last line necessary? or even correct? does it make any sense or is Titanium taking care of freeing memory?
Thanks a lot,
Regis

— asked March 23rd 2010 by Regis Zaleman
  • class
  • instance
  • memory
  • release
0 Comments

3 Answers

  • Accepted Answer

    We'll take care of freeing memory. However, generally, in the app.js (which is consider the global context), most of the memory is retained during your app. If you want to release memory (such as you do something only on startup), you could wrap in an anonymous function (see below). Windows that use JS urls are also nice since when you close them, you automatically free all associated memory.

    (function()
    {
       var do_something_here;
    })();
    
    — answered March 23rd 2010 by Jeff Haynie
    permalink
    4 Comments
    • I have made a custom TabGroup from scratch (a designer’s choice, can’t live with the Android upper tabBar…).

      I use it in a app that has a main window and a secondary window that is that TabGroup. When this TabGroup is opened, I load a inner window (the content) via windows.url (and create a corresponding controller/mediator object). When I change the tab, I close the previous inner window (and release the controller object) and open an other one, etc.

      When I test this on my iPod touch, it looks like when I open the tabGroup and go back to the main screen, and go to the tabGroup again, and go back, etc. a lot of times, it takes significantly more time to transition to the tabGroup. Eventually, even the transition effect becomes very, very laggy.

      Is it true that when I close a window that use JS urls is freeing all the memory or maybe I should look at a way to make sure that I free the memory of my controller objects when I release them?

      — commented June 30th 2011 by Alain Duchesneau
    • Jeff, you said that windows closed will free all associated memory, but that doesn't seem to work for us. It works on iPhone but we have big leak issues on Android when we try to close a window. It just leaks. Can you give us some information about this? Its very important for us as we can't release the app if it crashes after opening only 10-15 windows. We have implemented numerous solutions.. Any information?

      — commented October 7th 2011 by Jaron Rubenstein
    • I have the same problem as Alain. We built a video player that has detail screens that are added to the main window.It has a back button that (supposedly) removes the views, and restores the original window, however, after going back and forth multiple times the tabgroup is leaking…

      — commented November 22nd 2011 by Frédéric Joiris
    • Using DDMS i found that Memory allocated to application is get increases as i am navigating screen by screen and after some time it crash application with force close message/Fail to load resources. Even i have remove all objects from window and after that assign null to it. i.e
      window.remove(viewname);
      viewname=null;
      window.close();
      window=null.
      Please help Me OUT.

      — commented February 16th 2012 by Fernan Delgado
  • Using DDMS i found that Memory allocated to application is get increases as i am navigating screen by screen and after some time it crash application with force close message/Fail to load resources. Even i have remove all objects from window and after that assign null to it. i.e
    window.remove(viewname);
    viewname=null;
    window.close();
    window=null.
    Please help Me OUT.

    — answered February 21st 2012 by Fernan Delgado
    permalink
    0 Comments
  • Hi Fernan,

    I am also having the same memory issues on Android. See this post for some information/details.

    http://developer.appcelerator.com/question/132357/memory-leaks-on-android-but-not-on-ios—very-simple-test-case#comment-115055

    I am waiting to see if anyone has a working solution for Android.

    Thanks

    — answered February 21st 2012 by sean sean
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.