私のアプリで大きなテキストファイルを処理しようとしています。私は、データを読み込んでいる間に消費されるメモリの量に注意したいと思うことを知っています。一度データが読み込まれると、アプリはそのデータを保持する必要はありません。ios iPhoneシミュレータはメモリ使用量分析を膨らませますか?
「Martin R」と投稿Read a file/URL line-by-lineのおかげで私の努力が始まりました。
大きなデータファイルを読み込むときに私のアプリケーションのメモリ消費量を監視しようとしているので、期待通りに動作しているかどうか確認できます。ここで問題に取り組んでいます。
XcodeからCommand-Iを使用してInstrumentsを実行し、割り当てを監視すると、ファイルの読み取り中にアプリが〜15MBでピークを確認してから元に戻ります。これは+/- 0.5MBというかなり反復可能です。
XcodeからCommand-Rを使用してアプリケーションを実行した後、ファイルを読み終えてからInstruments内のレコードを押すと、メモリ消費量が〜360MBまで膨れます。だから、明確にする
、私はメモリ割り当ての測定を行っている2つの方法があります。
プロフィール:
1. Xcodeのコマンド-I。
2.機器記録割当。 15MBを観察してください
シミュレーションとプロファイル:
1. Xcode Command-R。
2.アプリを「IDLE」に実行させます。
3.機器レコード。 360MBを観察してください。
私はここでいくつかのことを理解しようとしています。
Q1。なぜ違い? (これはすべての私の質問に答えるかもしれません)
Q2。実際の問題がありますか?これは、デバッグコードにシミュレータに注釈が付けられているためですか?
Q3。 Q2と同様に、実際のデバイスでデバッグビルドを実行すると、同じ問題が発生しますか?
4。私のアプリケーションでは、ファイルを解析するとき〜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.
}
}
を参考にして、楽器を使用してテストしたいときはいつでも、**実際のデバイス**を試してみてください。これは、マックブックのシミュレータが実際のデバイスより優れているからです。 「このビデオ(https://www.youtube.com/watch?v=cR4Wc4JGOMg&index=27&list=WL) – Honey
」を参考にしてください。機器を使ってテストしたいときはいつでも、実際のデバイスを試してみてください。シミュレータは実際のデバイスより優れた動作をする可能性があります。または悪化する。それをレイアウトの便利なツールと考えてください。 – Confused