I have a simple command line tool in swift, which is doing image operations, but the memory is never released. I have extracted the code to repro the issue. The following code is simply loading 50 times a tif image and uncompresses it, and I assume that Swift should release my variables at the end of each loop, however it doesn't and eventually takes more than 4 GB in memory:
let imageUrlPath = "folder/file.tif"
for _ in 0...50
{
let sourceImg = NSImage(byReferencingFile: imageUrlPath)
let sourceBitmapRep = NSBitmapImageRep(cgImage: sourceImg!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
let sourceImageData = sourceBitmapRep.representation(using: .tiff, properties: [:])
}
RunLoop.main.run() // Total memory taken is 4.3 GB
How can I force memory release ?