1

I am using the Alloy framework in Appcelerator and have been struggling with memory leaks while testing my app with Apple Instruments.

I have a scrollable view, views that are the views or "pages" of that scrollable view and views like thumbnails that are the children of the "page" views. All of these views are being created dynamically and then removed and recreated when a user performs a search which reloads the contents of the scrollable view.

My issue is that even though I am removing the scrollable view and setting it to null the live bytes in Instruments continues to grow every time a search is performed and a new scrollable view is created. How should I be handling these UI elements in order for garbage collection to remove them?

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

scrollview で removeAllChildren を呼び出し、viewSliderArray を空の配列 (viewSliderArray = []) に設定する必要があります。そうしないと、参照が維持されます。

また、Instruments を使用しているときに、TiUI を検索して、特に増加しているビューを確認できます。これにより、問題を引き起こしている特定のビューを特定できます。

于 2015-05-11T14:15:51.467 に答える
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 に答える