title: CALayer memory usage on draw - implicit cache?
body: <p>I'm having a peculiar problem with <code>CoreGraphics</code>/<code>CoreAnimation</code> on the iPhone. To better explain how the problem manifests itself, I'll walk you through my current setup and illustrate with code where appropriate.</p>
  
  <p>I'm trying to draw a bunch of preloaded images in a <code>UIView</code>'s <code>CALayer</code>, but whenever the image displays, the app's memory usage spikes and the memory is not reclaimed whenever the image changes.</p>
  
  <p>Preloading of the images is done by reading them through <code>UIImage</code>'s facilities, rendering them onto a Bitmap Context and extracting a <code>CGImageRef</code> out of that context. The purpose of this is to decompress and scale the images, so that these operations do not occur on every draw. A similar recommendation can be found in an Apple Q&amp;A on the matter (search for <code>CGContextDrawImage</code> performance if you're curious). The context is set up with 8 bits per component and premultiplied alpha.</p>
  
  <p>After the images are decompressed into a bitmap, they're stored in a <code>NSArray</code> and are later assigned (not retained) to a custom <code>UIView</code> subclass that does the drawing. I've tried various approaches to actually drawing the images, and by far the fastest method is directly setting the view's <code>CALayer</code> <code>contents</code> property. Other methods such as <code>drawLayer:inContext:</code> and <code>drawRect:</code> have varying impacts on the framerate, but they all exhibit the same memory behavior.</p>
  
  <p>The problem is.. after the <code>contents</code> property changes, I see a spike in memory in Instruments and that memory doesn't go down even after the image is no longer being displayed. Object Allocations stay constant, so my only guess is that <code>CoreAnimation</code> is creating some implicit cache to speed up drawing. As I said however, that cache is not released when it should, and the gradual build-ups lead to a crash after only a couple of minutes of running.</p>
  
  <p>The <code>contents</code> property retains the object and I don't explicitly release it because I want the original images to stay in memory for the duration of the application's execution; on top of that, a high retain count wouldn't account for the memory spikes I see.</p>
  
  <p>When examining the stack, I see that <code>CoreAnimation</code> makes calls to functions such as <code>CA::Render::copy_image</code>, which leads me to believe it's duplicating the layer's contents somewhere out of reach. I suppose there's a valid reason for this, but not knowing how and when to clear this is currently a show-stopping bug.</p>
  
  <p>So can anyone with intricate knowledge of CA please explain to me if I'm doing something wrong and how I can work around this behaviour.</p>
  
  <p>Thanks.</p>