swift - Is the ios iPhone simulator causing memory usage analysis to swell? -


i trying process large text file in app. know want careful amount of memory being consumed while read data. once piece of data read app not need keep data around.

thanks “martin r” , post read file/url line-by-line helping me jump start effort.

i trying monitor memory consumption of app reads in large data file can sure behaving expected. here’s running problem.

when run instruments using command-i within xcode , monitor allocations see during read of file app peeks @ ~15mb , drops down. repeatable +/- 0.5mb.

when run app using command-r within xcode , let finish reading through file, , press record within instruments, memory consumption swells ~360mb.

so clarify, 2 ways have done measurement of memory allocations are:
profile:
1. xcode command-i.
2. instruments record allocations. observe ~15mb
simulate , profile:
1. xcode command-r.
2. let app run “idle”.
3. instruments record. observe ~360mb.

i have been trying figure out few things here.
q1. why difference? (this may answer questions)

q2. have real problem or problem because of how debug code annotated on simulator?

q3. similar q2, if run debug build on real device, have same issue?

q4. app, ~15mb acceptable when parsing file, ~360mb not be. there way can continue debug on device without taking 360mb hit?

version 8.1 (8b62)
sierra
2.7ghz i5
macbook pro circa 2015

sample code attached. first part of file merely copy of code referenced post reader convenience. 1 can take code , run in xcode. @ bottom viewcontroller viewdidload() method things "run". memory “swell” after “file opened”.

// //  import uikit  /*   * stackoverflow:  * https://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift   * posted martin r.  * 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 read closed file")      // read data chunks file until line delimiter found:     while !ateof {       if let range = buffer.range(of: delimdata) {         // convert complete line (excluding delimiter) string:         let line = string(data: buffer.subdata(in: 0..<range.lowerbound), encoding: encoding)         // remove line (and delimiter) 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 delimiter).           let line = string(data: buffer data, encoding: encoding)           buffer.count = 0           return line         }       }     }     return nil   }    /// start reading beginning of file.   func rewind() -> void {     filehandle.seek(tofileoffset: 0)     buffer.count = 0     ateof = false   }    /// close underlying file. no reading must done after calling 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()     // additional setup after loading view, typically 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 , discard */         while astreamreader.nextline() != nil {           wordcnt += 1         }        } // if let ...     } // if nil ...      print ("final wordcnt := \(wordcnt)")   } // viewdidload     override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated.   }   } 

i've encountered problems when using long running while loops. problem allocated current autorelease pool won't deallocated until loop exits.

to guard against this, can wrap contents of while loop in autoreleasepool(invoking:). cause each iteration of loop have own autorelease pool drained each time.

it this:

/// return next line, or nil on eof. func nextline() -> string? {   precondition(filehandle != nil, "attempt read closed file")    var result: string? = nil    // read data chunks file until line delimiter found:   while !ateof, result == nil {     result = autoreleasepool {       if let range = buffer.range(of: delimdata) {         // convert complete line (excluding delimiter) string:         let line = string(data: buffer.subdata(in: 0..<range.lowerbound), encoding: encoding)         // remove line (and delimiter) 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 delimiter).           let line = string(data: buffer data, encoding: encoding)           buffer.count = 0           return line         }       }       return nil     }   }   return result } 

as whether memory growth side effect of debug environment, it's hard say. wise guard against kind of growth regardless.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -