title: Memory warning and crash (ARC) - how to identify why it's happening?
body: <p>I've started to use the ARC recently and since then I blame it for every single memory problem. :) Perhaps, you could help me better understand what I'm doing wrong.</p>
  
  <p>My current project is about CoreGraphics a lot - charts drawing, views filled with thumbnails and so on. I believe there would be no problem while using manual memory management, except maybe a few zombies... But as of now, application simply crashes every time I try to either create a lot of thumbnails or redraw a bit more complicated chart.</p>
  
  <p>While profiling with Instruments I can see an awfully high value in resident memory as well as in the dirty one. Heap analysis shows rather alarming irregular grow... </p>
  
  <p>When drawing just a few thumbnails, resident memory grows for about 200 MB. When everything is drawn, memory drops back on almost the same value as before drawing. However, with a lot of thumbnails, a value in resident memory is higher than <a href="https://dl.dropbox.com/u/5552845/memory/memory-draw9-crash.png" rel="nofollow">400 MB</a> and that obviously crashes the app. I've tried to limit number of thumbnails drawn at the same time (NSOperationQueue and its maxConcurrentOperationCount), but as releasing so much memory seems to take a bit more time, it didn't really solve the issue.</p>
  
  <p>Right now my app basically doesn't work as the real data works with a lot of complicated charts = lot of thumbnails.</p>
  
  <p>Every thumbnail is created with this code I got from around here: (category on UIImage)</p>
  
  <pre><code>+ (void)beginImageContextWithSize:(CGSize)size
  {
      if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
          if ([[UIScreen mainScreen] scale] == 2.0) {
              UIGraphicsBeginImageContextWithOptions(size, YES, 2.0);
          } else {
              UIGraphicsBeginImageContext(size);
          }
      } else {
          UIGraphicsBeginImageContext(size);
      }
  }
  
  + (void)endImageContext
  {
      UIGraphicsEndImageContext();
  }
  
  + (UIImage*)imageFromView:(UIView*)view
  {
      [self beginImageContextWithSize:[view bounds].size];
      BOOL hidden = [view isHidden];
      [view setHidden:NO];
      [[view layer] renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      [self endImageContext];
      [view setHidden:hidden];
      return image;
  }
  
  + (UIImage*)imageFromView:(UIView*)view scaledToSize:(CGSize)newSize
  {
      UIImage *image = [self imageFromView:view];
      if ([view bounds].size.width != newSize.width ||
          [view bounds].size.height != newSize.height) {
          image = [self imageWithImage:image scaledToSize:newSize];
      }
      return image;
  }
  
  + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
  {
      [self beginImageContextWithSize:newSize];
      [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      [self endImageContext];
      return newImage;
  }
  </code></pre>
  
  <p>Is there some other way which wouldn't eat so much memory or is something really wrong with the code when using ARC?</p>
  
  <p>The other place where memory warning + crash is happening is when there is too much redrawing of any view. It doesn't need to be quick, just many times. Memory stacks up until it crashes and I'm not able to find anything really responsible for it. (I can see a growing resident/dirty memory in VM Tracker and a heap growth in Allocations instrument)</p>
  
  <p>My question basically is: how to find why it is even happening? My understanding is when there is no owner for given object, it's released ASAP. My inspection of code suggests a lot of objects are not released at all even though I don't see any reason for it to happen. I don't know about any retain cycles...</p>
  
  <p>I've read through the Transitioning to ARC Release Notes, bbum's article about heap analysis and probably a dozen of others. Differs somehow heap analysis with and without ARC? I can't seem to do anything useful with its <a href="https://dl.dropbox.com/u/5552845/memory/heap-analysis2.png" rel="nofollow">output</a>.</p>
  
  <p>Thank you for any ideas.</p>
  
  <p><strong>UPDATE:</strong> (to not force everybody read all the comments and to hold my promise)</p>
  
  <p>By carefully getting through my code and adding @autoreleasepool, where it had any sense, memory consumption got lowered. The biggest problem was calling <code>UIGraphicsBeginImageContext</code> from background thread. After fixing it (see @Tammo Freese's answer for details) deallocation occurred soon enough to not crash an app.</p>
  
  <p>My second crash (caused by many redrawing of the same chart), was completely solved by adding  <code>CGContextFlush(context)</code> at the end of my drawing method. Shame on me.</p>
  
  <hr>
  
  <p>A small warning for anyone trying to do something similar: <strong>use OpenGL.</strong> CoreGraphics is not quick enough for animating big drawings, especially not on an iPad 3. (first one with retina)</p>