2016-06-20 68 views
0

我有一个窗口的.js文件,然后我所有的布局添加到这个窗口。从钛内存中删除VAR对象

first.js

var Win = Ti.UI.CreateWindow({ 
    backgroundColor : 'white' 
}); 
Win.open(); 

secun.js

var View = Ti.UI.createView({ 
    height : Ti.UI.SIZE, 
    width : deviceWidth, 
    backgroundColor : 'white' 
}); 

Ti.UI.CurrentWindow.add(View); 

var label = Ti.UI.createLabel({ 
    text : "Test", 
    color : 'white', 
    height : deviceHeight * 0.090, 
    width : deviceWidth, 
    backgroundColor : 'transparent', 
    textAlign : 'center', 
    font : { 
     fontSize : deviceHeight * 0.0285, 
     fontWeight : 'normal' 
    } 
}); 

View.add(label); 

要删除查看我做出如下:

Ti.UI.CurrentWindow.remove(View); 

当我由于这一点,由View和标签占用的内存是发布还是需要做其他事情来释放手机内存?像设置变量为空,所以不再是关联的Ti对象,并可以通过垃圾收集器清理?

在我的项目中,将变量沸腾到null的问题在于,一些变量是在函数内部创建的,然后在函数外部不可用。

回答

2

如果您不想访问任何视图或标签,则不要为其创建变量。例如。如果您没有访问label其他地方(只是增加它View),那么建议您直接添加label为:

View.add(Ti.UI.createLabel({ 
    text : "Test", 
    color : 'white', 
    height : deviceHeight * 0.090, 
    width : deviceWidth, 
    backgroundColor : 'transparent', 
    textAlign : 'center', 
    font : { 
     fontSize : deviceHeight * 0.0285, 
     fontWeight : 'normal' 
    } 
})); 

更多请看看到http://docs.appcelerator.com/platform/latest/#!/guide/Managing_Memory_and_Finding_Leaks http://www.tidev.io/2014/03/27/memory-management/

+0

又如何我是否可以释放已创建并需要在我的函数的其他位置访问的变量的内存? –

+0

@Manuel_Rodrigues,您可以在删除该视图之前使用视图的'removeAllChildren()'方法。 http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.View-method-removeAllChildren – Swanand