I am trying to read data from big files sequentially. Since the files can be bigger than the available memory, I don't want the file to be completely loaded into memory.
class RecordImporter {
func `import`(url: URL) {
guard let reader = RecordReader(url: url) else { return }
self.resume(from: reader)
}
private func resume(from reader: RecordReader) {
while let _ = reader.nextData() {
}
//Checking memory here is about 300Mb, the test file size.
return
}
}
class RecordReader {
private var fileHandle: FileHandle
init?(url: URL) {
do {
self.fileHandle = try FileHandle(forReadingFrom: url)
}
catch {
return nil
}
}
func nextData() -> Data? {
let data = self.fileHandle.readData(ofLength: 10000)
guard !data.isEmpty else { return nil }
return data
}
}
After the while loop is done, the memory is about 300Mb, even though I am reading the data in 10Kb chunks.
For some reason the chunks are not being released.
Any idea what might be happening?