title: Objective-C looping through an array takes a lot of memroy
body: <p>I'm trying to read a file from disk, separate it by line, store it into an <code>NSArray</code>, then loop through that array and do some pretty basic operations on the data. However, if I'm using a small-ish file (~5000 lines of data, one word per line), I end up using a few hundred megabytes of memory by the end of the loop. If I'm using a very large file (~200,000 lines), I end up using a few gigabytes of memory! Why does this happen? I should see an initial spike of memory as I load the file into the array, but then memory usage should stay fairly constant. I was using ARC, but I didn't trust it so now I'm doing all the memory management myself and I'm sure I'm releasing all temporary things I've allocated in my loop.</p>
  
  <pre><code>NSString *fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  
  NSScanner *scanner = [NSScanner scannerWithString:fileContents];
  NSMutableArray *wordList = [[[NSMutableArray alloc] init] autorelease];
  while (![scanner isAtEnd]) {
      NSString *line = [[NSString alloc] init];
      [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&amp;line];
      [wordList addObject:line];
      [line release];
  }
  return wordList;
  </code></pre>