我在我的iPad应用程序中同步来自服务器的大约5000张图像。这些图像的大小约为2.5 GB,我的iPad也有足够的空间。
但在同步仅375张图片后,我的应用程序开始崩溃,原因如下
malloc: *** mmap(size=1048576) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2013-02-14 06:20:50.058 AAA[1250:907] ***
Terminating app due to uncaught exception 'NSMallocException',
reason: 'Attempt to allocate 1048576 bytes for NS/CFData failed'
*** First throw call stack:
我正在使用核心数据在文档目录中保存图像。
如果对申请保存数据有任何限制,请指导我。我正在主线上执行所有这些操作。
for (int i = 0; i < [shadowElement3 count]; i++)
{
NSMutableArray* array = [[NSMutableArray alloc] init];
Product* failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:context];
NSXMLElement* element11 = [shadowElement3 objectAtIndex:i];
NSString* strPath = [element11 stringValueForNode:@"B1_COD"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strPath];
NSLog(@"%@",[element11 stringValueForNode:@"img"]);
NSData* receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [element11 stringValueForNode:@"img"]]];
[receivedData writeToFile:savedImagePath atomically:YES];
[array addObject:savedImagePath];
}
答案 0 :(得分:4)
您可能需要使用自动释放pool。根据该文档,“如果你编写一个创建许多临时对象的循环。”
答案 1 :(得分:4)
有几件事:
enumerateObjectsUsingBlock:
而不是for / loop。有关原因,请参阅here。尝试以下内容:
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:[shadowElement3 count]];
NSXMLElement* element11;
NSString* strPath;
NSString *savedImagePath;
Product* failedBankInfo;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[context setUndoManager:nil];
for (int i = 0; i < [shadowElement3 count]; i++)
{
@autoreleasepool{
failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:context];
element11 = [shadowElement3 objectAtIndex:i];
strPath = [element11 stringValueForNode:@"B1_COD"];
savedImagePath= [documentsDirectory stringByAppendingPathComponent:strPath];
NSData* receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [element11 stringValueForNode:@"img"]]];
[receivedData writeToFile:savedImagePath atomically:YES];
[array addObject:savedImagePath];
// you don't seem to be doing anything with the Managed Object you created,
// I assume you are saving the path. So do so and batch save the context.
// This will help free up memory.
failedBankInfo.pathName = savedImagePath;
if ( i % 100 ==0 ){
NSError *error;
[context performBlock:^{
[context save:&error];
// handle error
// reset the context cause you don't seem to be doing anything else
// with the objects. This will free up memory
[context reset];
}];
}
}
}
// One last save
NSError *error;
[context performBlock:^{
[context save:&error];
// handle error is exercise for the reader.
// reset the context cause you don't seem to be doing anything else
// with the objects. This will free up memory
[context reset];
}];
祝你好运。