我正在尝试在我的应用中处理大型文本文件。我知道我想在读取数据时要小心消耗的内存量。读取一段数据后,应用程序无需保留数据。
感谢“Martin R”和帖子Read a file/URL line-by-line帮助我开始努力。
我正在尝试监视应用程序的内存消耗,因为它读入大数据文件,以便我可以确定它的行为符合预期。这是我遇到问题的地方。
当我在Xcode中使用Command-I运行Instruments并且我监视分配时,我看到在读取文件期间应用程序偷看~15MB然后再下降。这是相当可重复的+/- 0.5MB。
当我在Xcode中使用Command-R运行应用程序然后让它读完文件,然后在Instruments中按下记录时,内存消耗现在膨胀到~360MB。
所以要澄清一下,我测量内存分配的两种方法是:
简介:
1. Xcode Command-I。
2.仪器记录分配。观察~15MB
模拟和简介:
1. Xcode Command-R。
2.让app运行到“IDLE”
3.仪器记录。观察~360MB。
我一直想弄清楚这里的一些事情 Q1。为什么不同? (这可能会回答我的所有问题)
Q2。我是否有真正的问题或者这只是一个问题,因为调试代码如何注释到模拟器上?
Q3。与Q2类似,如果我在真实设备上运行调试版本,它会有同样的问题吗?
Q4。对于我的应用程序,解析文件时可以接受~15MB,但不会是~360MB。还有另一种方法可以继续在我的设备上进行调试而不需要这个360MB的点击吗?
版本8.1(8B62)
塞拉利昂
2.7Ghz i5
MacBook Pro Circa 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.
}
}
答案 0 :(得分:1)
在使用长时间运行的while
循环时,我遇到过类似的问题。问题是,分配到当前自动释放池的任何内容都不会被释放,直到循环退出。
为了防范这种情况,您可以将{while循环中的内容包装在autoreleasepool(invoking:)
中。这将导致循环的每次迭代都有自己的自动释放池,每次都会耗尽。
它看起来像这样:
/// Return next line, or nil on EOF.
func nextLine() -> String? {
precondition(fileHandle != nil, "Attempt to read from closed file")
var result: String? = nil
// Read data chunks from file until a line delimiter is found:
while !atEof, result == nil {
result = autoreleasepool {
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
}
}
return result
}
至于你的内存增长是否是调试环境的副作用,很难说。但无论如何,防范这种增长可能是明智的。