I have an issue on an iPad app where every images (all 1024x768) displayed takes 3Mo on RAM without freeing them when not needed, leading to a crash after some time.

I load images using "contentsOfFile" method which should (in my understanding) release memory when the memory is low and the app doesn't need it anymore but it just doesn't seem to be the case (even when i simulate a memory warning on simulator).

I made a really simple test class to illustrate the problem below (swift 1.2).

When the application loads it creates a view, add sublayers filled with the images, and then when I tap on the screen I expect the 72Mo taken by the uncompressed bitmaps data (used by Core Animation to render the layer) to be freed (or at least to be freed when i simulate a memory warning), which it never does.

(Note : I use ARC on both my main project and this test project)

```
ViewController: UIViewController {

  func tap(gesture : UITapGestureRecognizer)
  {
    if gesture.state == UIGestureRecognizerState.Ended {
      var tmpV = self.view.viewWithTag(1000)
      // tried everything to release memory
      for subLayer : CALayer in tmpV?.layer.sublayers as! [CALayer!] {
        subLayer.contents = nil
      }
      tmpV?.layer.sublayers.removeAll(keepCapacity: false)
      tmpV?.removeFromSuperview()
      tmpV = nil
    }
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    //add gesture to remove images from view
    var gesture = UITapGestureRecognizer(target: self, action: Selector("tap:"))
    self.view.addGestureRecognizer(gesture)

    //loading images
    var imagesArray : [UIImage!]! = self.fillImagesArray()

    //adding those images to a view
    let tmpV = UIView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768))
    for img in imagesArray {
      var layer = CALayer()
      layer.frame = CGRect(x: 0, y: 0, width: 1024, height: 768)
      layer.contents = img.CGImage
      tmpV.layer.addSublayer(layer)
    }
    tmpV.tag = 1000
    self.view.addSubview(tmpV)    
  }

  func fillImagesArray() -> [UIImage!]{
    var array : [UIImage!] = []
    for i in 0 ... 23 {
      var image : UIImage?
      var filename = NSBundle.mainBundle().pathForResource("image \(i)", ofType: "png")
      image = UIImage(contentsOfFile: filename!)      
      array.append(image)
    }
    return array
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }
}

```

EDIT:

To add a few more details, here is a screenshot taken from Profile/Allocations tool, so as I said I guess that the memory is taken by CA when rendering the layers.

![Memory screenshot](https://i.stack.imgur.com/rziRS.png)
When the images aren't displayed on screen, the memory is still really low, but once they got displayed they aren't released even after removal.

I tried to draw the tmpV layer, remove the sublayers and then adding the drawn image to tmpV.layer.contents in `viewDidAppear`, and if I do so my image is correct and the memory stays low but it cost around 1.5 seconds to draw the layer (I need my app to be fast and I can't afford this time).

The only way I found to release the memory was to add the images into a class variable array, and emptied it on `tap` func. But I'd need to reload the images again if I want to re-create an image and I'd like to avoid that.