title: "Can't allocate region" malloc error when running millions of iterations of loop
body: <p>Thanks to a lot of help I've received here on SO, I've gotten an algorithm to check a list of around 15,000 8-letter words for any partial anagrams, against a list of around 50,000 total words (so I suppose a total of 108 million iterations). I call this method once for each comparison (so 750 million times). I'm getting the following error, always somewhere in the midst of the 119th iteration through the 1,350 there should be:</p>
  
  <pre><code>AnagramFINAL(2960,0xac8c7a28) malloc: *** mmap(size=2097152) failed (error code=12)
  *** error: can't allocate region
  *** set a breakpoint in malloc_error_break to debug
  </code></pre>
  
  <p>I've narrowed the memory issue down to being a huge number of allocated CFStrings (immutable). Any idea what I can do to remedy the issue? I'm using ARC and an <code>@autoreleasepool</code>, not sure what else I could do, it seems something isn't being released when it should be.</p>
  
  <p>AnagramDetector.h</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface AnagramDetector : NSObject {
  
          NSDictionary *allEightLetterWords;
  NSDictionary *allWords;
  
      NSFileManager *fileManager;
      NSArray *paths;
      NSString *documentsDirectory;
      NSString *filePath;
  }
  
  - (BOOL) does: (NSString *) longWord contain: (NSString *) shortWord;
  - (NSDictionary *) setupAllWordList;
  - (NSDictionary *) setupEightLetterWordList;
  - (void) saveDictionary: (NSMutableDictionary *)currentArray;
  
  @end
  </code></pre>
  
  <p>AnagramDetector.m</p>
  
  <pre><code>@implementation AnagramDetector
  
  - (id) init {
      self = [super init];
      if (self) {
          fileManager = [NSFileManager defaultManager];
          paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          documentsDirectory = [paths objectAtIndex:0];
      }
      return self;
  }
  
  - (BOOL) does: (NSString *) longWord contain: (NSString *) shortWord {
      @autoreleasepool {
            NSMutableString *longerWord = [longWord mutableCopy];
            for (int i = 0; i &lt; [shortWord length]; i++) {
                NSString *letter = [shortWord substringWithRange: NSMakeRange(i, 1)];
                NSRange letterRange = [longerWord rangeOfString: letter];
                if (letterRange.location != NSNotFound) {
                    [longerWord deleteCharactersInRange: letterRange];
                } else {
                    return NO;
                }
            }
          return YES;
      }
  }
  
  - (NSDictionary *) setupAllWordList {
  
      @autoreleasepool {
          NSString *fileWithAllWords = [[NSBundle mainBundle] pathForResource:@"AllDefinedWords" ofType:@"plist"];
          allWords = [[NSDictionary alloc] initWithContentsOfFile: fileWithAllWords];
          NSLog(@"Total number of words: %d.", [allWords count]);
      }
      return allWords;
  }
  
  
  - (NSDictionary *) setupEightLetterWordList {
  
      @autoreleasepool {
          NSString *fileWithEightWords = [[NSBundle mainBundle] pathForResource:@"AllDefinedEights" ofType:@"plist"];
          allEightLetterWords = [[NSDictionary alloc] initWithContentsOfFile: fileWithEightWords];
          NSLog(@"Total number of words: %d.", [allEightLetterWords count]);
      }
      return allEightLetterWords;
  }
  
  - (void) saveDictionary: (NSMutableDictionary *)currentArray {
  
      @autoreleasepool {
          filePath = [documentsDirectory stringByAppendingPathComponent: @"A.plist"];
          [fileManager createFileAtPath:filePath contents: nil attributes: nil];
          [currentArray writeToFile: filePath atomically:YES];
          [currentArray removeAllObjects];
      }
  }
  
  @end
  </code></pre>
  
  <p>Code running on launch (inside AppDelegate for now, since no VC):</p>
  
  <pre><code>@autoreleasepool {
  
      AnagramDetector *detector = [[AnagramDetector alloc] init];
  
      NSDictionary *allWords   = [[NSDictionary alloc] initWithDictionary:[detector setupAllWordList]];
      NSDictionary *eightWords = [[NSDictionary alloc] initWithDictionary:[detector setupEightLetterWordList]];
  
      int remaining = [eightWords count];
  
      for (NSString *currentEightWord in eightWords) {
          if (remaining % 10 == 0) NSLog(@"%d ::: REMAINING :::", remaining);
          for (NSString *currentAllWord in allWords) {
              if ([detector does: [eightWords objectForKey: currentEightWord] contain: [allWords objectForKey: currentAllWord]]) {
                  // NSLog(@"%@ ::: CONTAINS ::: %@", [eightWords objectForKey: currentEightWord], [allWords objectForKey: currentAllWord]);
              }
          }
          remaining--;
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/87rSC.png" alt="Instruments"></p>