【发布时间】: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