【发布时间】:2014-06-15 02:08:07
【问题描述】:
我一直在苦苦思考如何处理我的 Titanium 应用程序的内存分配(专为 android [9.4MB]、ios[2.8MB] 和 MobileWeb[5.4MB] 构建。
我创建了一个小部件,用于从菜单选择中打开视图。
<Alloy>
<Window id="mainWindow">
<View id="menuView">
<View id="vTop">
<!-- Header goes here -->
<TableView id="menuTable" />
</View>
<!-- footer message goes here -->
</View>
<View id="contentView">
<View id="nav">
<Label id="winTitle"/>
<View id='leftButtonView'>
<Label id="icoLeftButton" />
</View>
<View id='backButtonTitleView'>
<Label id="icoBack" />
<Label id="backButtonTitle" />
</View>
<View id='rightButtonView'>
<Label id="icoRightButton" />
</View>
</View>
<View id="mainView" layout="composite" />
</View>
</Window>
</Alloy>
此小部件的示例用法:
通过关注this solution,我能够减少视图的内存分配。每次我从菜单选择中打开另一个视图时,我都会应用它。
(我的小部件的控制器)
var memPool = Ti.UI.createWindow();
memPool.open();
memPool.hide();
memPool.close();
// Open view from menu selection
function openView(e) {
var cbAdd = function() {
var ctrl = Alloy.createController(e["url"]);
if (ctrl != null) {
setWindowTitle(e["wTitle"]);
setRightIco(ctrl.getRightIco()); //setting right label icon (IcoMoon font)
$.setRightNavClick(ctrl.getRightNavCb());
//Have to passed navGroup to be able to open other views
ctrl.winMain.navGroup = $;
//Close splash screen after layout
ctrl.winMain.addEventListener("postlayout", postLayout);
$.mainView.add(ctrl.winMain);
ctrl.init();
ctrl = null;
}
};
var cbRemove = function() {
//Remove all children from mainView; fn [commonjs]
fn.removeChildren($.mainView, cbAdd);
};
if ($.mainView.children.length > 0) {
cleanUp($.mainView.children[0]);
cbRemove();
} else
cbAdd();
}
function cleanUp(obj, cb) {
memPool.open();
memPool.hide();
memPool.setZIndex(-1);
memPool.add(obj);
memPool.close();
obj = null;
if (cb != null) cb();
}
在 XCode Instruments 上检查结果:
每次我从菜单打开其他视图时,TiUIView 总是会减少,但 TiUIViewProxy 不会。
要打开的示例视图:(由小部件组成)
<Alloy>
<View id="winMain">
<ScrollView id="form" layout="vertical">
<Widget id="fperiod" src="ph.com.test.controls.period" top="10" />
<Widget id="ftermid" src="ph.com.test.controls.key" label="Terminal No." top="20" />
<Widget id="fofficeid" src="ph.com.test.controls.combobox" label="Branch" />
<View id="btnReset" width="100%" height="50" layout="horizontal" top="20" bottom="20" backgroundColor="#B40026">
<Label class="resetFilter" />
<Label class="lblReset" />
</View>
</ScrollView>
</View>
</Alloy>
以下是很有帮助的参考资料:
- http://developer.appcelerator.com/question/116867/this-is-a-solution-to-your-memory-woes#answer-203729
- http://www.tidev.io/2014/03/27/memory-management/
- http://developer.appcelerator.com/question/152656/tips-and-tricks-for-memory-management
- http://developer.appcelerator.com/question/140852/quick-memory-question--tiuiview-vs-tiuiviewproxy
- http://developer.appcelerator.com/question/151989/should-i-null-objects-to-release-memory-in-alloy
如何减少TiUIViewProxy的内存分配?我是否还必须从其控制器中清理小部件的视图?
我试图从其控制器中清理我的小部件的视图。然后,TiUIViewProxy 根据在 XCode Instruments 上的检查有所减少,但我的问题是我的应用程序突然崩溃了。我不知道为什么。或者我不只是做对了。我的小部件中的示例清理功能:
$.cview.remove($.formItemLabel);
$.cview.remove($.lblValue);
$.mView.remove($.cview);
//nulling controller variables goes here
mData = null;
animateLeft = null;
...
【问题讨论】:
标签: android ios memory-management titanium titanium-mobile appcelerator-mobile titanium-alloy