Swift memory will not release
<p>I have a class that get all the user assets, get the image and then perform face detection on each of the images. I am calling my class from the viewDidLoad function like this:</p>

<pre><code>   override func viewDidLoad() {

    _ = autoreleasepool{

        return faceProcessing()    
    }
  }
</code></pre>

<p>And this is the class:</p>

<pre><code>class faceProcessing{

let manager = PHImageManager.default()
let option = PHImageRequestOptions()

init() {
    let realm = try! Realm()
    let assets = getAssets(realm: realm)

    option.isSynchronous = true
    option.deliveryMode = .fastFormat
    option.isNetworkAccessAllowed = true

    var featuresCount = 0

    for assetIndex in 0...assets.count-1{


        featuresCount += autoreleasepool{() -&gt; Int? in
            print(assetIndex)
            var facesCount = 0
           faceDetection(asset: assets[assetIndex]) { (features) in
                facesCount = features
            }

            return facesCount
        }!
    }

    print(featuresCount)    
}

func getAssets(realm: Realm)-&gt;[PHAsset]{

    return autoreleasepool{ () -&gt; [PHAsset] in
    let images = realm.objects(Image.self).filter("asset != nil")
    let ids: [String] = images.map {$0.id}
    let options = PHFetchOptions()
    options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    let assets = PHAsset.fetchAssets(withLocalIdentifiers: ids, options: options)
    var assetArray = [PHAsset]()

    if(assets.count &gt; 1){
            for assetIndex in 0...assets.count-1{
            assetArray.append(assets[assetIndex])
        }
    }
    return assetArray
    }
}

func faceDetection(asset: PHAsset, completionHandler:@escaping (Int)-&gt;Void){    
  manager.requestImageData(for: asset, options: option){
        (data, responseString, imageOriet, info) in
        if (data != nil){
           weak var faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])

            let faces = (faceDetector?.features(in: CIImage(data: data!)!))
            faceDetector = nil
            completionHandler((faces?.count)!)
        }else{
            print(info)
        }

    }
}
}
</code></pre>

<p>The problem is that after finishing the process of getting the images and detecting the faces, the memory will not release. Before the process the memory was about 60MB, during the process it was between 400MB to 500MB, and when the process finished it went back to 300MB. Is there is a way I could release the entire memory and have 60MB after the process will finish ?</p>

<p>Here is how the instruments looks:</p>

<p><a href="https://i.stack.imgur.com/C6I41.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C6I41.png" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/5iTjk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5iTjk.png" alt="enter image description here"></a></p>

<p>It looks like the face detection is not releasing its memory, so its there is a way I could release the CIDetection from memory?</p>