性能工具 - 泄漏

时间:2011-09-27 13:07:28

标签: ios xcode jailbreak

如果我使用性能工具测试我的代码 - 泄漏,并且它没有检测到任何泄漏。这是否意味着代码没有泄漏任何内存?

我有一个破牢的iPhone,我可以监控可用的内存。如果有人知道,那就是SBSettings。我测试了我的应用程序,它有一个UITableView,当我滚动浏览tableView时,我可以看到可用的内存丢失。从300MB到30MB,它似乎无法进一步下降。它通常不会与游戏以外的其他应用程序相比下降太多。我正在使用带有2个按钮,1个textView和3个UILabels的自定义UITableViewCell。

所以,是的。如果性能工具没有检测到任何泄漏,我安全吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"StatusTableCell";

    StatusTableCell *cell = (StatusTableCell *)
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle]
                                    loadNibNamed:@"StatusTableCell"
                                    owner:nil options:nil];
        for (id currentObjects in topLevelObjects){
            if ([currentObjects isKindOfClass:[StatusTableCell class]]){
                cell = (StatusTableCell *) currentObjects;
                break;
            }
        } 
        [cell.cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];
    }
     /// some other stuff
     return cell;
}

2 个答案:

答案 0 :(得分:1)

如果应用程序运行时,您应该考虑性能工具中 LiveBytes 的值,这是一个问题。如果您不使用可重用单元格,则可能会发生这种情况。如果您有可重复使用的细胞,请检查它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusablecell"];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reusablecell"];
        [cell autorelease];

    }
//update cell here

return cell;
}

答案 1 :(得分:1)

不,你不一定安全。

当程序不再具有对象的引用时,会发生内存泄漏。因此,如果一个对象被释放,但它所保留的对象不是(例如,在dealloc方法中未正确释放),则会出现泄漏。

但是,如果拥有对象本身从未释放,则不会检测到泄漏。

要查找这些类型的内存问题,请运行allocations instruments工具。单击标记堆按钮,然后在应用程序中执行某种可重复操作(例如,在表视图中选择一行以将详细信息视图推送到导航堆栈,然后点击后退按钮)。再次单击标记堆按钮。然后重复几次动作。理想情况下,您应该看不到堆增长,堆镜头之间没有持久对象。