【问题标题】:How would you resolve the following memory leak?您将如何解决以下内存泄漏?
【发布时间】:2017-09-27 22:15:59
【问题描述】:

您将如何解决以下内存泄漏问题?

我在 iOS 应用 Ti SDK 6.2 中遇到内存泄漏问题,但没有取得多大成功。

当打开和关闭窗口时,xcode Instruments Allocations 工具会显示内存中保留有许多 TiUIxxxxProxy 对象。

为了测试/调试问题,我创建了一个超级简单的经典 appcelerator 应用程序(代码如下所示)。使用下面的代码,我从 app.js 打开 window1,然后从 window1 上的按钮打开 window2。

您可以在附加的 xcode Instruments Allocations 图像中看到,在 window2 关闭后,代理对象仍然存在(窗口、表格等)。更糟糕的是,多次打开和关闭 window2 会不断添加额外的使用内存的代理对象。

App.js

require('Window1').CreateWindow().open();

Window1.js

exports.CreateWindow = function(){
    try{
        var window1 = Ti.UI.createWindow({
            title:'Window 1',backgroundColor:'yellow',
        });

        var button1 = Ti.UI.createButton({
            top:'50dp', center:'50%',
            borderWidth:'1dp',borderColor:'black',
            title:' Open Window 2  '            
        });
        window1.add(button1);

        button1.addEventListener('click', function() { 
          var win2 = require('Window2').CreateWindow();
          win2.open();
        });

        return window1;
    }
    catch(e){
        alert('Window 1 Error: ' + e);
    }                   
};

Window2.js

exports.CreateWindow = function(){
    try{
        var window2 = Ti.UI.createWindow({
                            title:'Window 2',layout:'vertical'
                            ,top:'200dp',bottom:'200dp',width:'80%'
                            ,backgroundColor:'orange'
         });

        var button2 = Ti.UI.createButton({
            top:'50dp', center:'50%',
            borderWidth:'1dp',borderColor:'black',
            title:' Close Window 2  '    
        });
        window2.add(button2);

        button2.addEventListener('click', function() { 
            window2.close();                                                  
        });

        // create a table row
        var tvRow1   = Ti.UI.createTableViewRow({ });

        //create a label to display location name
        var labelRow1 = Ti.UI.createLabel({
              color:'#000000',
              left:'15dp',
              top:'10dp',
              text:'Label in row 1'
            });                         
        tvRow1.add(labelRow1);

        // define table section for this group of rows
        var tableViewSection1 = Ti.UI.createTableViewSection({ });
        // push row into table view section 
        tableViewSection1.add(tvRow1);

        //create array to store table sections
        var arrayTableSections = [];

        //add table section to array
        arrayTableSections.push(tableViewSection1);        

        // create table 
        var table2 = Titanium.UI.createTableView({
                    data: arrayTableSections,
                    top:'50dp',
                    left:'50dp',
                    right:'50dp',
                    height:Ti.UI.SIZE,
                    backgroundColor:'#ffffff' ,
                    borderColor:'black',borderWidth:'1dp'
        });

        // add table to window
        window2.add(table2);         

        return window2;
    }
    catch(e){
        alert('Window 2 Error: ' + e);
    }                   
};

【问题讨论】:

  • 尝试在窗口关闭时将用于声明 window2 的变量设置为“未定义”,看看是否会清除对象。
  • 遗憾的是没有变化。 win2 = 未定义;
  • 设置 win2 = null;然后尝试
  • 有什么理由不使用 Alloy?
  • 您是否愿意在 Alloy 中重建应用程序?可能不得不走那条路。

标签: ios appcelerator


【解决方案1】:

您遇到的具体问题是您手动创建元素而不是清理。因此,在 Window2 关闭时,您应该从其父级中删除所有元素,并最终从 Window2 中删除:

  • 从行中删除标签
  • 从该部分中删除该行
  • 从表格中删除部分
  • 从窗口中删除表格
  • 从窗口中移除按钮

然后

  • 使标签、行、节、表和按钮无效

最重要的是:

  • 清除指向节/行数组的指针。

完成后,确保您有一个 removeEvenlistener 来删除事件处理程序。

最后,关闭窗口并取消它。

最后一件事,如果您以后不需要它,请不要在 window1 中创建 window2 指针:

require("window2").createWindow().open();

优于:

var window2 = require("window2").createWindow();

window2.open();

这样做我能够打开 window2 8 次以上,然后全部清理干净。

【讨论】:

  • 如果您使用 Alloy,您会帮自己一个忙并减少问题——即使不是全部,它也会为您做大部分。
猜你喜欢
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2021-11-18
  • 2018-04-11
  • 2020-04-07
  • 1970-01-01
相关资源
最近更新 更多