How to free memory from VNGeneratePersonSegmentationRequest

Viewed 174

I’m playing with the new VNGeneratePersonSegmentationRequest Vision API to make a simple background removal filter

I made a small project to test it, works great, but I’m running into issues with memory. After executing the request the app’s memory consumption adds 300MBs that are never freed.

I’ve cycled through a bunch of images and requests in a test run and thankfully memory consumption remains constant even when filtering more images, but I worry about that initial memory that is never freed, even when inducing a memory warning. I suspect the Vision Framework needs that memory after being called, but my app doesn’t handle video frames or anything, so it’s memory going to waste

//Autorelease pool doesn't helps
    autoreleasepool {
        // Create request
        let request = VNGeneratePersonSegmentationRequest()

        request.revision = VNGeneratePersonSegmentationRequestRevision1
        request.qualityLevel = .accurate
        request.outputPixelFormat = kCVPixelFormatType_OneComponent8

       let handler:VNImageRequestHandler = VNImageRequestHandler(ciImage: inputs.ciImage,
                                            options: [:])

        //A jump in memory usage after running the handler
        //Subsequent calls don't add to memory usage
        do{
            try handler.perform([request])
        }
        catch{
            return
        }
        
        //Even if I delete this chunk of code, memory consumption remains high

        let mask = request.results?.first!
        if let maskBuffer = mask?.pixelBuffer{
            self.personMask = CIImage(cvPixelBuffer: maskBuffer)
            let maskScaleX = inputs.ciImage.extent.width / personMask!.extent.width
            let maskScaleY = inputs.ciImage.extent.height / personMask!.extent.height
            self.personMask = personMask!.transformed(by: __CGAffineTransformMake(
                maskScaleX, 0, 0, maskScaleY, 0, 0))

        }
    }
0 Answers
Related