【发布时间】:2016-11-13 16:36:04
【问题描述】:
我正在尝试在我的应用程序中处理一个大文本文件。我知道我要小心读取数据时消耗的内存量。一旦读取了一条数据,应用程序就不需要保留数据。
感谢“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. 让应用程序运行到“IDLE”。
3.仪器记录。观察~360MB。
我一直试图在这里弄清楚一些事情。
Q1。为什么有区别? (这可能会回答我所有的问题)
第二季度。我是否有真正的问题,或者这只是一个问题,因为调试代码是如何注释到模拟器上的?
第三季度。与 Q2 类似,如果我在真实设备上运行调试版本,是否会出现同样的问题?
第四季度。对于我的应用程序,解析文件时可以接受 ~15MB,但不能接受 ~360MB。是否有另一种方法可以继续在我的设备上进行调试而不会受到 360MB 的影响?
版本 8.1 (8B62)
塞拉利昂
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.
}
}
【问题讨论】:
-
附带说明,每当您想使用仪器进行测试时,请尝试使用真实设备,因为 macbook pro 的模拟器可能比真实设备表现更好。见this video
-
"作为旁注,每当您想使用仪器进行测试时,请尝试使用真实设备,因为 macbook pro 的模拟器可能比真实设备表现更好。"或者更糟。将其视为布局的便利工具,仅此而已。