运行数百万次迭代循环时,“无法分配区域”malloc错误

时间:2012-11-15 11:26:17

标签: objective-c malloc nsdictionary instruments

感谢我在SO上收到了很多帮助,我得到了一个算法来检查大约15,000个8个字母的单词列表中的任何部分字谜,而不是总共50,000个单词的列表(所以我假设共有1.08亿次迭代)。我为每次比较称这种方法一次(所以7.5亿次)。我得到了以下错误,总是在第119次迭代中通过1,350应该有:

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

我已将内存问题缩小为大量已分配的CFS字符串(不可变)。知道我可以做些什么来解决这个问题吗?我正在使用ARC和一个@autoreleasepool,不知道我还能做什么,似乎有些东西没有被发布。

AnagramDetector.h

#import <Foundation/Foundation.h>

@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

AnagramDetector.m

@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 < [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

代码在启动时运行(目前在AppDelegate内部,因为没有VC):

@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--;
    }
}

Instruments

1 个答案:

答案 0 :(得分:5)

问题似乎是许多自动释放的对象填满了等待释放的内存。所以解决方案是添加自己的自动释放池作用域来收集自动释放的对象并尽快释放它们。

我建议你做这样的事情:

for (NSString *currentEightLetterWord in [eightLetterWordsDictionary allKeys]) {
    @autoreleasepool { 
        for (NSString *currentWord in [allWordsDictionary allKeys]) {
        }
    }
}

现在,@autoreleasepool { .. }内的所有自动释放对象都将在外循环的每次迭代中释放。

正如您所看到的,ARC可能会让您无法考虑大多数引用计数和内存管理问题,但在使用直接或间接创建自动释放对象的方法时,对象仍然可以在使用ARC的自动释放池中结束。

我不建议使用的替代解决方案是尽量避免使用将使用自动释放的方法。然后does:contain:可能被笨拙地改写成这样的东西:

- (BOOL) does: (NSString* ) longWord contain: (NSString *) shortWord {
    NSMutableString *haystack = [longWord mutableCopy];
    NSMutableString *needle = [shortWord mutableCopy];
    while([haystack length] > 0 && [needle length] > 0) {
        NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
        [set addCharactersInRange:NSMakeRange([needle characterAtIndex:0], 1)];
        if ([haystack rangeOfCharacterFromSet:set].location == NSNotFound) return NO;
        haystack = [haystack mutableCopy];
        [haystack deleteCharactersInRange:NSMakeRange(0, [haystack rangeOfCharacterFromSet: set].location)];
        needle = [needle mutableCopy];
        [needle deleteCharactersInRange:NSMakeRange(0, 1)];
    }
    return YES;
}