Titanium Community Questions & Answer Archive

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

Memory Management

I was just wondering what strategies are used to manage memory within the Titanium SDK.

Are there functions that would be beneficial to call within an App?

Such as setting an object that is no longer used to null or reusing objects?

— asked March 30th 2010 by John Welch
  • optimization
0 Comments

5 Answers

  • @Regis: Define a namespace for each controller, its properties and methods using Javascript object literal notation, aka. "singleton". This makes it much easier to organize your codebase.

    For example, here's a quick example on how to build a controller for managing XHR requests:

    // Primary Controller
    var Network = {
        action : '',
        view : '',
        xhr : Titanium.Network.createHTTPClient()
    };
    Network.xhr.setTimeout([15000]);
    Network.xhr.onerror = function(e) {};
    Network.xhr.onreadystatechange = function() {};
    

    Then define each network controller like so…

    Network.login = {
        name : 'Login',
        run : function() {
            Ti.API.log('Running '+this.name);
            // do something
        }
    };
    

    The controller directory then looks something like this:

    controllers/Network.js
    controllers/Network.Login.js
    controllers/Network.Register.js
    ...
    

    To call a controller just use:

    Network.login.run();
    // Outputs "Running Login"
    

    You can also look into Javascript Module Patterns. To understand more, I highly recommend reading more about closures.

    — answered March 30th 2010 by TZ Martin
    permalink
    0 Comments
  • In most cases you don't have to worry about it - see this thread.

    — answered March 30th 2010 by James K
    permalink
    0 Comments
  • In other cases you want to be conscience of how many views and UI elements you have rendered and added to a given window. You could dynamically add() / remove() views based on user actions, in addition to anonymous functions / closures. This can help optimize performance across devices.

    For iPhone, I typically optimize for iPod Touch (1st gen), 3G, 3GS, and use a MVC approach to development with JS "classes", etc.

    — answered March 30th 2010 by TZ Martin
    permalink
    0 Comments
  • Hi Terry - I am trying to implement an MVC approach as well. I am a little confused about how to implement it in Javascript… especially the Controller. I have done some with PHP for web apps, but I am not sure how to do it here. Any pointers?
    Thanks!

    — answered March 30th 2010 by Regis Zaleman
    permalink
    0 Comments
  • @Terry - Thank you so much. I will learn more about this.

    — answered April 2nd 2010 by Regis Zaleman
    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.