从 Instruments 读取内存分配结果

Posted

技术标签:

【中文标题】从 Instruments 读取内存分配结果【英文标题】:reading memory allocation results from Instruments 【发布时间】:2012-07-24 05:10:09 【问题描述】:

我正在对我的 ios 应用程序的内存分配运行分析器,并且我检测到当前创建了 8MB 内存并且仍然存在于我的应用程序中。显然有什么不对劲。所以我深入研究,这是我可以给你看的图片:

知道为什么会这样吗?这似乎是一个自动释放的对象,所以它不应该被释放而不是生活在内存中吗?

这是我调用函数 parseTagsInComment 的方式:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *commentsText = [NSString stringWithFormat:@"%@ %@", self.imageComment_.username_, self.imageComment_.text_];

    NSRange range;
    range.location = 0;
    range.length = commentsText.length;

    NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:commentsText];
    [attrStr setFont:[UIFont fontWithName:@"HelveticaNeue" size:14] range:range];
    self.commentAttributedString_ = attrStr;
    [attrStr release];


    dispatch_async(dispatch_get_main_queue(), ^
        [weakSelf.commentsText_ setAlpha:0.0];
        [weakSelf.commentsPostedTime_ setAlpha:0.0];
        [weakSelf.commentsText_ setFrameWidth:weakSelf.contentView.frameWidth - weakSelf.profilePicture_.frameWidth - kCommentsPadding];
        [weakSelf.commentsText_ setFrameHeight:weakSelf.imageComment_.commentHeight_ - 30];
        [weakSelf.commentsText_ setAttributedString:weakSelf.commentAttributedString_];
        [weakSelf.commentsText_ setLinkColor:weakSelf.textColor_];

        NSString *timePosted = [NSString timestampToString:weakSelf.imageComment_.createdTime_];
        CGSize commentsTimeSize = [timePosted sizeWithFont:weakSelf.commentsPostedTime_.font constrainedToSize:CGSizeMake(weakSelf.commentsText_.frameWidth, 50)];
        [weakSelf.commentsPostedTime_ setText:timePosted];


        [UIView animateWithDuration:0.3 animations:^
            [weakSelf.commentsText_ setAlpha:1.0];
            [weakSelf.commentsPostedTime_ setAlpha:1.0];
         completion:^(BOOL finished)
            [weakSelf parseTagsInComment];
        ];
    );
    [pool release]; 
);

【问题讨论】:

您确定这些百分比不代表每行代码所花费的时间量吗? (即,这是内存数据而不是分析数据)? @Turix 我怎么知道?我很确定它是内存.. 因为我在 Instruments 而不是时间分析器上运行了分配测试 是的,分配(或泄漏)应该是你想要的,你是对的。 (虽然我问我问题的原因是因为你的屏幕截图中的百分比对我来说似乎更合理。)顺便说一句,当我从 xcode 运行它时,这看起来不像我从 Allocations 或 Profile 获得的输出,所以很难说。 嗯,输出是在双击 CFString (malloc) 后,它使用了大部分内存 @adit 你如何获得带有内存百分比的代码视图? 【参考方案1】:

我认为函数 parseTagsInComment 要么使用一些委托方法调用,要么从工作线程的执行路径(不在主线程上)调用。

因此,函数中的第一行应该创建一个自动释放池,最后一行应该销毁该池。

-(void) parseTagsInComment

   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   //Body of your function
   [pool release];

【讨论】:

实际上它是在 dispatch_async 上调用的。我在上面添加了一些代码 @adit 你试过他的建议了吗? (我认为值得一试——这对我来说很有意义,因为在 dispatch_async 上自动释放的内存可能要等到下一次刷新主池时才会释放,而在分配测试中你可能还没有做到这一点。) @Turix 是的,我认为它似乎解决了我的问题.. 但是,我能否获得关于 dispatch_async 上自动发布的更详细的解释,直到下一次刷新邮件池时才会发布。 .我也需要了解这一点,而不仅仅是使用解决方案 @adit 查看此线程:***.com/questions/4141123/… 在您的情况下,我猜您正在查看分配工具中的“时间窗口”,其中不包括调度线程的自动释放池的耗尽.

以上是关于从 Instruments 读取内存分配结果的主要内容,如果未能解决你的问题,请参考以下文章

如何解决此内存泄漏?

iPhone:使用 Instruments 检测当前内存使用情况?

读取视频帧 iPhone 时出现内存问题

iOS使用Instruments的工具

Apple Instruments 在跟踪 iOS 分配时停止工作

使用 Instruments 通过模态视图控制器改进内存管理