title: CALayer seems to retain its contents - how to release resources?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/10494980/cant-release-unused-calayer-memory-when-using-multiple-layers">Can’t release unused CALayer memory when using multiple layers</a>  </p>
  </blockquote>
  
  
  
  <p>I have an app which allows the user to browse a series of images that are CALayers. I found that after I added more than 20 to the screen the iPad 2 would crash - obviously I need to dynamically load them in when visible on the screen. </p>
  
  <p>So I implemented this, by removing the CALayer from its SuperLayer when no longer needed. What I found however is that the memory does not disappear when viewed in the 'Activity Monitor". It does however get freed when I 'simulate a memory warning' in the simulator. Fine you might think - that's what memory warnings are for. <em>However</em> - I still find the iPad runs out of RAM as I browse the images, the memory usages goes up and up until it crashes. Does anyone know a way to force a CALayer to release its resources? </p>
  
  <p>Here's my code, <strong>note if I omit assigning the layer contents</strong> then the memory usage remains low (but of course you don't see the image)</p>
  
  <pre><code>    UIImage* image = [UIImage imageNamed:@"imageName"];
  
      frontLayer = [CALayer layer];
      frontLayer.bounds = CGRectMake(0, 0, 952, 650);
      frontLayer.contents = (id) image.CGImage;
  </code></pre>
  
  <p>Note I am <strong>not using ARC</strong>, and after I remove the layer from its superlayer I release it (it is retained by a property). The fact that the memory seems to get reclaimed with a low memory warning makes me think it's not a problem with the way I am retaining/releasing but I am open to ideas.</p>