I am reading a file from disk on Mac. The following code in a function keeps making memory usage go up. I don't have any variable to reference the "content", just test to read it in and think the system will release it after the function exit. but it does not. System actually only releases the memory when the for-loop is done.
My question is, how to let system to release the "content" object after the read function exits? I even delay 10s after the function call in the loop.
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func onRead(_ sender: NSButton) {
for n in 0..<100
{
readFile()
sleep(10)//not enough time for system to recycle the memory??
}
}
func readFile()
{
print ("test read file")
let content = FileManager.default.contents(atPath: "/Users/swiftuser/Documents/testfile.txt")
}
}