【问题标题】:Remove var object from memory in titanium从钛的内存中删除 var 对象
【发布时间】:2016-06-20 23:32:09
【问题描述】:

我有一个带有窗口的 .js 文件,然后我将所有布局添加到此窗口。

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);

当我到期时,视图和标签占用的内存被释放了还是需要做其他事情来释放手机内存?就像将变量设置为 null 这样就不再关联 Ti 对象并且可以被垃圾收集器清理?

在我的项目中将变量设为 null 的问题在于,某些变量是在函数内部创建的,然后在该函数外部不可用。

【问题讨论】:

    标签: javascript android ios garbage-collection titanium


    【解决方案1】:

    如果您不想访问任何视图或标签,请不要为其创建变量。例如。如果您没有在其他任何地方访问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/

    【讨论】:

    • 我怎样才能释放我在函数的其他地方创建并需要访问的变量的内存?
    • @Manuel_Rodrigues 为此,您还可以在删除该视图之前使用该视图的removeAllChildren() 方法。 docs.appcelerator.com/platform/latest/#!/api/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多