Titanium SDK: 3.2.3 GA
iOS: 7.1
While trying the following sample code on iOS (I used iPad3) I noticed that the created views are never garbage collected. (the code is pretty simple, clicking the button creates 200 views and adds them to the window).
For profiling the memory I used the Instruments tool from XCode. Each time the button is pressed new bunch of views are created but the old ones are never returned.
Here is the code:
MyView.js
exports.MyView = function() { var view = Ti.UI.createView({ top : 0, left : 0, width : 1000, height : 500, backgroundColor : 'white' }); view.myFunc = function() { for (var i = 0; i < 10; i++) { Ti.API.info("Counting: " + i); } }; /** * Creates 200 views and adds them to the view object above */ function createManyViews(view) { var colors = ['blue', 'yellow', 'green', 'pink', 'orange', 'red']; var top = 0; for (var i = 0; i < 10; i++) { var left = 0; for (var j = 0; j < 20; j++) { var v = Ti.UI.createView({ top : top, left : left, width : 50, height : 50, backgroundColor : colors[(i + j) % 5], }); view.add(v); left = left + 50; } top = top + 50; } }; createManyViews(view); return view; };app.js
(function() { var MyView = require("MyDir/MyView").MyView; var myView = null; var win = Ti.UI.createWindow(); var btn = Ti.UI.createButton({ bottom : 10, width : 150, title : "Create views" }); btn.addEventListener('click', function() { if (myView) { win.remove(myView); } myView = new MyView(); win.add(myView); }); win.add(btn); win.open(); })();The problem can be resolved by one of the following:
- Remove the definition of
view.myFunc() - Move the code from
createManyViews()out of the function (remove the function)
However, I would like to know what is the actual cause for this so I can avoid similar problems.