【问题标题】:Live bytes growing with ARC over 200 MB on iPhone使用 ARC 在 iPhone 上超过 200 MB 的实时字节增长
【发布时间】:2014-01-31 16:02:59
【问题描述】:

当我使用仪器运行我的 iPhone 应用程序时,当我从显示模态视图控制器来回切换时,它显示实时字节在增长,但即使在使用超过 200 MB 后应用程序也不会被终止。

这里到底发生了什么?我是不是太占内存了? 顺便说一句,我正在使用带有 ARC 的 iOS 7。

下图显示了活动字节。

谢谢。

更新: 我的模态视图控制器中的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ActionCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    Action *action = self.actions[indexPath.section][indexPath.row];
    cell.textLabel.text = action.title;
    cell.textLabel.textColor = self.sectionColors[indexPath.section];

    CustomCellBackgroundView *customCellBackgroundView = [[CustomCellBackgroundView alloc] init];
    customCellBackgroundView.borderColor = self.sectionColors[indexPath.section];
    customCellBackgroundView.fillColor = [self.sectionColors[indexPath.section] colorWithAlphaComponent:0];

    if ([self.actions[indexPath.section] count] == 1) {
        customCellBackgroundView.position = CustomCellBackgroundViewPositionSingle;
    } else if (indexPath.row == 0) {
        customCellBackgroundView.position = CustomCellBackgroundViewPositionTop;
    } else if (indexPath.row == [self.actions[indexPath.section] count] - 1) {
        customCellBackgroundView.position = CustomCellBackgroundViewPositionBottom;
    } else {
        customCellBackgroundView.position = CustomCellBackgroundViewPositionMiddle;
    }

    cell.backgroundView = customCellBackgroundView;

    return cell;
}

模态视图控制器为调用模态的主视图控制器保留一个弱委托。 并且不 [self.presentedViewController dismissViewControllerAnimated:YES completion:NULL];永久释放模态视图控制器(及其所有子视图和变量)?

顺便说一句,CustomCellBackgroundView 可能不是问题,因为我有另一个模态视图控制器,它以相同的方式使用它,但不会增加内存使用量。

奇怪的是,即使使用了 200 MB,应用程序也没有被杀死,为什么会这样(不是在模拟器上运行,而是在实际 iPhone 上运行)?

【问题讨论】:

  • 如果没有看到涉及的代码,这是不可能调试的。通常,当我使用 ARC 泄漏时,这是因为我使用了核心基础对象而忘记了 ARC 不处理这些。
  • 也许你有一个参考周期。很可能您的模态视图控制器永远不会被释放。
  • ARC 并不意味着你有干净的内存!只要您有强变量或持有对对象的引用,它们就会保持活动状态。
  • 大部分内存正被单元格背景使用,看起来...您是否正确使用出队来设置单元格?
  • ArtOfWarfare,我刚刚用一些代码更新了这个问题,向您展示了我如何使单元格出列。

标签: objective-c ios7 automatic-ref-counting


【解决方案1】:

这是我花了一些时间才理解的问题,但即使方舟很棒,并且有助于分配!没有你的帮助,你不能编写一个复杂的应用程序,并把所有的数据管理都交给 ARK 处理。

每一个viewController,你都必须有一个“ViewWillDissapear”方法,并且你需要把你使用的每一个对象都置为nil,所以ARK知道去掉它就可以了。

//重要 如果你在你的应用程序中使用图像,那么在发布它们之后将它们变为 nil 是非常重要的。 如果你使用任何上下文,你必须手动释放! 让您在每个以太之上都没有保持许多观点! 如果你使用音频文件,你必须在之后发布它!

最后一点,确保你没有持有对不需要持有强引用的对象的“强”引用。因为他们永远不会被释放。

希望这会有所帮助, 快乐编程。

【讨论】:

  • 如果我有一个视图控制器,其属性类似于 @property (strong, nonatomic) NSArray *array;我必须做self.array = nil吗?当视图控制器消失(不再引用它)时,这个数组不会被释放吗?
  • 首先,视图控制器,视情况而定。可以说,您使用导航控制器方法“PushViewController”,视图仍在视图堆栈中。其次,强引用是双向的。这就是与“弱”引用的区别,只要两个对象中的一个还活着,他仍然持有对以太的强引用。干杯!
【解决方案2】:

我很抱歉这个愚蠢的问题,这是一个新手错误,问题是我通过在存储在我的视图控制器中的块内调用 self 上的方法来创建一个保留循环。

【讨论】:

    猜你喜欢
    • 2018-12-22
    • 1970-01-01
    • 2010-12-28
    • 2021-08-21
    • 2013-07-19
    • 1970-01-01
    • 2014-01-23
    • 2011-05-06
    • 1970-01-01
    相关资源
    最近更新 更多