0
我在iPhone應用程序中遇到內存問題,給我一個很難的時間。 以下是錯誤消息我得到:malloc:mmap(size = XX)失敗(錯誤代碼= 12)
malloc: * mmap(size=9281536) failed (error code=12) * error: can't allocate region
我使用ARC爲這個應用程序,如果可能有用的信息。 下面的代碼只是使用Bundle中的文件來加載核心數據實體。
奇怪的是崩潰只發生在90多個循環之後;儘管在我看來,由於「內容」的大小越來越小,內存請求也應該越來越小。
這裏是代碼,如果任何人可以看到一個缺陷,請讓我知道。
NSString *path,*contents,*lineBuffer;
path=[[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"txt"];
contents=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
int counter=0;
while (counter<10000) {
lineBuffer=[contents substringToIndex:[contents rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location];
contents=[contents substringFromIndex:[lineBuffer length]+1];
newItem=[NSEntityDescription insertNewObjectForEntityForName:@"myEntityName"
inManagedObjectContext:context];
[newItem setValue:lineBuffer forKey:@"name"];
request=[[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName:@"myEntityName"
inManagedObjectContext:context]];
error=nil;
[context save:&error];
counter++;
}