1

我在 Appcelerator 中使用 Alloy 框架,并且在使用 Apple Instruments 测试我的应用程序时一直在努力解决内存泄漏问题。

我有一个可滚动视图,视图是该可滚动视图的视图或“页面”,而缩略图之类的视图是“页面”视图的子视图。所有这些视图都是动态创建的,然后在用户执行重新加载可滚动视图内容的搜索时删除并重新创建。

我的问题是,即使我删除了可滚动视图并将其设置为 null,每次执行搜索并创建新的可滚动视图时,Instruments 中的实时字节都会继续增长。我应该如何处理这些 UI 元素以便垃圾收集将它们删除?

var videoSlider;

function loadData(searchTerms,channel,sortBy,limit) {
if (videoSlider) {
    $.scrollableViewHolder.remove(videoSlider);
    videoSlider = null;
}

videoSlider = Alloy.createController('videoSlider', {}).getView();
$.scrollableViewHolder.add(videoSlider);

var viewSliderArray = [];

feeds.GetFeeds({
    success: function(data) {
        Ti.API.info("Number of videos returned from Brightcove " + videosObject.items.length);
        var j = 0;
        for(var i=0; i<videosObject.items.length; i++) {
            if(i % 8 == 0) {
                Ti.API.info(i);
                if(i > 0) {
                    viewSliderArray.push(viewSlider);
                }
                viewSlider = Alloy.createController('viewSlider', {}).getView();
                j = 0;
            }

            tempTime = videosObject.items[i].length/1000;
            minutes = Math.round(tempTime/60);
            seconds = Math.round(tempTime%60);
            seconds = "0"+seconds;
            seconds = seconds.substr(seconds.length-2);

            videoLength = minutes+":"+seconds;

            videoBox = Alloy.createController('videoBox', {
                videoBoxTop: videoBoxTop[j],
                videoBoxLeft: videoBoxLeft[j],
                videoStill : videosObject.items[i].videoStillURL,
                videoTitle: videosObject.items[i].name,
                videoLength: videoLength
            }).getView();

            viewSlider.add(videoBox);
            j++;
        }
        viewSliderArray.push(viewSlider);

        Ti.API.info(viewSliderArray);

        videoSlider.views = viewSliderArray;
    }
},searchTerms,channel,sortBy,limit);

}
4

3 回答 3

0

从滚动视图中删除所有子项并将它们设置为空。这是一个可能对您有用的示例代码:

exports.removeChildren = function(thisView){
if (thisView.children) 
{
    var removeData = [];
    for (var i = thisView.children.length; i > 0; i--)
    {
       removeData.push(thisView.children[i-1]);  
    };
    // Remove childrens
    for (var i = 0; i < removeData.length; i++)
    {
       thisView.remove(removeData[i]);
    }
    removeData = null;
}
}
于 2015-05-07T12:04:51.323 回答
0

试试这个来清理你的视图的内存。

 clean(Your_View);
 Your_View = null;

function do_clean(e, c) {
    try{
        clean(c);
    e.remove(c);
    c = null;
    }catch(ex){
        Ti.API.info('Exception in removing child: '+ex +'children :'+c);
    }
    return;
}

function clean(e) {
    if (e != null) {
        if (e.children) {
            for (var i = 0; i < e.children.length; i++) {
                if(e.children[0] != undefined && e.children[0] != null)
                    do_clean(e, e.children[0]);
            }
        } else {
            return;
        }
    }
}

还尝试删除所有不需要的 eventListeners。

于 2015-05-07T12:14:57.380 回答
0

您需要在滚动视图上调用 removeAllChildren 并将 viewSliderArray 设置为空数组,即 viewSliderArray = [],否则将保留引用。

此外,在使用 Instruments 时,您可以搜索 TiUI 并查看具体增加的视图,这样您就可以确定给您带来麻烦的特定视图。

于 2015-05-11T14:15:51.467 回答