【问题标题】:Memory Leak in Titanium tabbed iPhone applicationTitanium 选项卡式 iPhone 应用程序中的内存泄漏
【发布时间】:2012-12-20 12:15:04
【问题描述】:

这让我头疼了几个星期——希望有人能提供帮助。我正在使用 Titanium 移动设备中的 2.1.4 SDK 为 iOs 5.0 构建应用程序。我的应用程序从数据库中提取数据,并使用标签和表格将其显示在可滚动视图中。我注意到它会在一段时间后崩溃,并且根据工具,滚动视图中的视图以及标签和表格视图永远不会从内存中释放。我使用了很多 commonJS,我的代码看起来像这样(精简):

ApplicationWindow 模块,我从 app.js 调用

function ApplicationWindow(title) {
var self = Ti.UI.createWindow({
    title:title,
    backgroundColor:'white'
});

var button = Ti.UI.createButton({
    height:44,
    width:200,
    title:L('openWindow'),
    top:20
});
self.add(button);

var QuizWindow = require('/ui/common/QuizWindow');

button.addEventListener('click', function() {
    //containingTab attribute must be set by parent tab group on
    //the window for this work
    qw = new QuizWindow({title: 'KCT', backgroundColor: 'white', navBarHidden: true, tabBarHidden: true});
    self.containingTab.open(qw);
});

return self;
};

module.exports = ApplicationWindow;

然后我调用 QuizWindow 模块:

function QuizWindow(args){
var instance = Ti.UI.createWindow(args);
var QuestionView = require('/ui/common/QuestionView');

var db = Ti.Database.install("/kct.sqlite","kct");
var q = db.execute("SELECT * FROM questions");

var sv = Ti.UI.createScrollableView({
    bottom: 40
});

while(q.isValidRow()){
    var id = q.fieldByName("qid");
    var question = q.fieldByName("q");
    var set = q.fieldByName("set");
    var info = q.fieldByName("info");

    var v = new QuestionView(id,set,question,info);
    sv.addView(v);
    q.next();
}

q.close();
db.close();

var button = Ti.UI.createButton({
    height:44,
    width:200,
    title:"Close",
    bottom:10
});

button.addEventListener('click', function() {
    instance.close({animation: true});
});

instance.add(sv);
instance.add(button);
return instance;
};
module.exports = QuizWindow;

这是 QuestionView:

function QuestionView (id,set,question,infolert) {
var qview = Ti.UI.createView({
    top: 0,
    width: 'auto',
    height: 'auto',
    backgroundColor: 'white',
    questionSet: set,
    questionID: id,
    info: infolert,
    isAnswered: false       
});

var qLabel = gui.Header(question);
qLabel.top = 5;
qLabel.left = 10;
qLabel.right = 10;
qLabel.color = "#3B3B3B";
qLabel.font = {fontSize: gui.defaultFontSize+4, fontWeight: 'bold'};

var dcalc = (qLabel.text.length / 30) * ((gui.defaultFontSize+4)*1.2) + 5;

var answView = Ti.UI.createTableView({
    backgroundColor: 'white',
    top: dcalc,
    _parent: qview
});

var changeheight = function(e) {
    var lheight = e.source.rect.height;
    if(lheight && lheight > 10) answView.top = lheight + 5;
    return;
};

qLabel.addEventListener('postlayout', changeheight);


qview.add(qLabel);
qview.add(answView);

return qview;

}
 module.exports = QuestionView;

我最初有一些代码可以从数据库中提取数据以获取答案,并用它填充 tableView,但为了简洁起见,我将其删除。即使没有它,也没有任何东西被释放。我真的很茫然,任何建议都值得赞赏。

【问题讨论】:

    标签: javascript iphone memory-leaks titanium titanium-mobile


    【解决方案1】:

    你可以看看我的 Gist。通过使用它,您可以极大地解决内存问题。只需在您的项目中包含该类并像这样使用它。

    https://gist.github.com/3898340

    var mem = new MemoryClean();
        mem.clean(tableView);
        mem.clean(headerView);
        mem.clean(footerView);
        mem = null;
    

    这是我一直在使用的。

    function openAppointments(e) {
        var mem = new MemoryClean();
        mem.clean(headerView);
        mem.clean(footerView);
        mem = null;
        win.close();
    }
    

    【讨论】:

    • 我尝试使用构造函数传递 mem,向其中添加每个视图、标签和表格,并在关闭窗口时将其设置为 null,但没有成功。他们还在堆积。也许我做错了,你有任何使用方法的例子吗?
    • 信息语句在控制台中打印了什么。 Ti.API.warn("这是我们的内存" +Ti.Platform.availableMemory);.我认为在关闭窗口时,它应该被释放。
    • 它一直给我一个错误,没有释放内存......也许这只是我。
    【解决方案2】:

    最后,我解决了。是这样的:

    var answView = Ti.UI.createTableView({
        (...)
        _parent: qview
    });
    

    引用答案表中的视图使其及其所有子项不会被释放。一旦我删除了它,它们只需关闭窗口即可释放。希望这对某人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 2014-02-06
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多