1

I am running a profiler on memory allocations on my iOS app and I am detecting that 8MB of memory are currently created and still lives in my app. Clearly there is something wrong. So I drilled down and here's the image that I can show you:

enter image description here

Any idea why this is the cause? This seems to be an auto released object, so shouldn't it be released instead of living in memory?

Here's how I am calling the function 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]; 
});
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
adit
  • 32,574
  • 72
  • 229
  • 373
  • Are you sure those percentages don't represent the amount of *time* each line of code takes? (i.e., that this is memory data and not profiling data)? – Turix Jul 24 '12 at 05:26
  • @Turix how do I know that? I am pretty sure it's memory.. because I ran the allocation test on Instruments not time profiler – adit Jul 24 '12 at 05:26
  • Yeah, Allocations (or Leaks) should be what you want, you're right. (Although the reason I asked my question is because the percentages in your screen shot seem more plausible as times to me.) BTW, that doesn't look like the output that I get from either Allocations or Profile when I run it from xcode, so it's hard to tell. – Turix Jul 24 '12 at 05:32
  • well that output is after double clicking on CFString (malloc) where it uses most of the memory – adit Jul 24 '12 at 05:51
  • @adit how do you get that code view with mem percentages? – Mariano Latorre Apr 29 '13 at 08:42

1 Answers1

1

I think the function parseTagsInComment is either called with some delegates method or from the execution path of worker thread (not on main thread).

So, your first line in the function should create a autorelease pool and in last line it should destroy the pool.

-(void) parseTagsInComment
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   //Body of your function
   [pool release];
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • actually it's called on an dispatch_async.. I added some code above – adit Jul 24 '12 at 05:57
  • @adit Did you try what he suggested? (I think it's worth a try -- it makes sense to me since the memory autoreleased on the `dispatch_async` may not be released until the next flush of the main pool, which you may not have gotten to yet in your Allocations test.) – Turix Jul 24 '12 at 06:06
  • @Turix yes I think it seems to solve my issue.. however, can I get a more detailed explanation about this auto-released on dispatch_async may not be released until the next flush of the mail pool.. I need to understand this too and not just use the solution – adit Jul 24 '12 at 06:08
  • @adit See this thread: http://stackoverflow.com/questions/4141123/do-you-need-to-create-an-nsautoreleasepool-within-a-block-in-gcd In your case, my guess is you were looking at a "time window" within your Allocations tool that didn't include a drain of the dispatch thread's autorelease pool. – Turix Jul 24 '12 at 06:59