0
votes

I'm developing an app in Titanium that needs to work on android and IOS, but I'm getting some memory problems.

In my app.js file I have this:

var win = Ti.UI.createWindow({
    backgroundColor : 'white',
    url : 'Home.js'
});

win.open();

Ti.App.View = [];

The Ti.App.View array is tu keep a reference to all my container views along my project so I can close them or check if they are already visible.

Then in my Home.js file I have some buttons to open some views. Ex:

var view = Ti.UI.createView({
    height : deviceHeight,
    width : deviceWidth,
    backgroundColor : 'white'
});
Ti.UI.currentWindow.add(view);

var viewMenu = Ti.UI.createView({
    layout : 'vertical',
    width : deviceWidth * 0.20,
    backgroundColor : 'transparent',
});

view.add(viewMenu);

viewMenu.addEventListener('click', function() {
    var Favorites = require("Eventos");
    Events.AddLayout();
});

This is the way how I add a new layout to the same window.

Then on my Events.js file I have a function like this and a global event :

exports.AddLayout = function(e) {
    //adding all my layout.....
}

Ti.App.addEventListener('Update', function() {
   // due something in hear
});

My question is how can I remove all Ti.UI objects created in the AddLayout function from memory when I press back button? And how can I remove the global event created by the Events.js file?

I have tried to reference te container view to null but it has not solved my problem.

1
don't put them in the Ti.UI namespace, put them in Alloy.Globals (if you use Alloy) or even better, make a lib to handle it.Rene Pot

1 Answers

0
votes

Have you considered developing this with Alloy? It uses a commonJS approach and would remove the need to use Global events like this and structure the app in a way that's easier to represent views, open and destroy views etc.

If not, try to avoid using Global Event listeners -- typically there's no need to use these and they'll end up causing all kinds of memory leaks.

This is a great video on the subject:

Your Apps Are Leaking

Hope this helps!