我正在尝试在我的应用程序中处理一个大文本文件。我知道我要小心读取数据时消耗的内存量。一旦读取了一条数据,应用程序就不需要保留该数据。
感谢“Martin R”和这篇文章逐行读取文件/URL帮助我开始努力。
我正在尝试监视我的应用程序在读取大数据文件时的内存消耗,以便我可以确保它的行为符合预期。这就是我遇到问题的地方。
当我在 Xcode 中使用 Command-I 运行 Instruments 并监控分配情况时,我发现在读取文件期间,应用程序会先查看约 15MB,然后又下降。这是相当可重复的+/- 0.5MB。
当我在 Xcode 中使用 Command-R 运行应用程序,然后让它完成文件读取,然后在 Instruments 中按下 Record 时,内存消耗现在膨胀到约 360MB。
因此,为了澄清这一点,我测量内存分配的两种方法是:
轮廓:
1.Xcode 命令-I。
2. 工具记录分配。观察~15MB
模拟和分析:
1.Xcode Command-R。
2. 让应用程序运行到“IDLE”。
3. 仪器记录。观察~360MB。
我一直试图在这里弄清楚一些事情。
Q1.为什么有区别? (这可以回答我所有的问题)
Q2。我是否遇到了真正的问题,或者这只是由于调试代码如何注释到模拟器上而导致的问题?
Q3。与 Q2 类似,如果我在真实设备上运行调试版本,是否会出现相同的问题?
Q4。对于我的应用程序,解析文件时 ~15MB 是可以接受的,但 ~360MB 则不行。有没有其他方法可以继续在我的设备上进行调试,而不会占用 360MB 的空间?
版本 8.1 (8B62)
Sierra
2.7GHz i5
MacBook Pro 2015 年左右
附示例代码。该文件的第一部分只是参考帖子中代码的副本,以方便读者。人们可以按原样使用此代码并在 Xcode 中运行它。底部是 ViewController ViewDidLoad() 方法,其中的内容“运行”。内存“膨胀”是在“文件打开”之后。
//
//
import UIKit
/* Originally from
* stackoverflow:
* https://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift
* posted by Martin R.
* Much thanks!
*/
class StreamReader {
let encoding : String.Encoding
let chunkSize : Int
var fileHandle : FileHandle!
let delimData : Data
var buffer : Data
var atEof : Bool
init?(path: String, delimiter: String = "\n", encoding: String.Encoding = .utf8,
chunkSize: Int = 4096) {
guard let fileHandle = FileHandle(forReadingAtPath: path),
let delimData = delimiter.data(using: encoding) else {
return nil
}
self.encoding = encoding
self.chunkSize = chunkSize
self.fileHandle = fileHandle
self.delimData = delimData
self.buffer = Data(capacity: chunkSize)
self.atEof = false
}
deinit {
self.close()
}
/// Return next line, or nil on EOF.
func nextLine() -> String? {
precondition(fileHandle != nil, "Attempt to read from closed file")
// Read data chunks from file until a line delimiter is found:
while !atEof {
if let range = buffer.range(of: delimData) {
// Convert complete line (excluding the delimiter) to a string:
let line = String(data: buffer.subdata(in: 0..<range.lowerBound), encoding: encoding)
// Remove line (and the delimiter) from the buffer:
buffer.removeSubrange(0..<range.upperBound)
return line
}
let tmpData = fileHandle.readData(ofLength: chunkSize)
if tmpData.count > 0 {
buffer.append(tmpData)
} else {
// EOF or read error.
atEof = true
if buffer.count > 0 {
// Buffer contains last line in file (not terminated by delimiter).
let line = String(data: buffer as Data, encoding: encoding)
buffer.count = 0
return line
}
}
}
return nil
}
/// Start reading from the beginning of file.
func rewind() -> Void {
fileHandle.seek(toFileOffset: 0)
buffer.count = 0
atEof = false
}
/// Close the underlying file. No reading must be done after calling this method.
func close() -> Void {
fileHandle?.closeFile()
fileHandle = nil
}
}
extension StreamReader : Sequence {
func makeIterator() -> AnyIterator<String> {
return AnyIterator {
return self.nextLine()
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path2WordList = Bundle.main.path(forResource: "large_text_file", ofType: "txt")
var wordCnt: Int = 0
if nil != path2WordList {
if let aStreamReader = StreamReader(path: path2WordList!) {
defer { aStreamReader.close() }
print("File openned")
/* Read and discard */
while aStreamReader.nextLine() != nil {
wordCnt += 1
}
} // if let ...
} // if nil ...
print ("Final wordCnt := \(wordCnt)")
} // viewDidLoad
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}