从for循环生成字符串会导致iOS上的内存过载
所以我在这里面临一个记忆问题。 我试图从位置对象数组生成一个GPX字符串 我有一个函数,它解析数组中的每个对象,并将信息添加到GPX字符串中。我的数组可以包含1000多个对象,因此for循环可以运行很长时间并生成大量字符串从for循环生成字符串会导致iOS上的内存过载,ios,for-loop,memory-management,nsstring,out-of-memory,Ios,For Loop,Memory Management,Nsstring,Out Of Memory,所以我在这里面临一个记忆问题。 我试图从位置对象数组生成一个GPX字符串 我有一个函数,它解析数组中的每个对象,并将信息添加到GPX字符串中。我的数组可以包含1000多个对象,因此for循环可以运行很长时间并生成大量字符串 - (NSString *) createGPXFromPositions:(NSArray*)pPositions andHikeName:(NSString*)pHikeName{ @autoreleasepool { NSDateFormatter *for
- (NSString *) createGPXFromPositions:(NSArray*)pPositions andHikeName:(NSString*)pHikeName{
@autoreleasepool {
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
//[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
formatter.dateFormat = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
dateString = [formatter stringFromDate:[NSDate date]];
NSString *lText = @"<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"1.1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" creator=\"MyApp\">\n\r";
lText = [NSString stringWithFormat:@"%@<trk>\n\r", lText];
lText = [NSString stringWithFormat:@"%@<name>%@</name>\n\r", lText, pHikeName];
lText = [NSString stringWithFormat:@"%@<desc>%@</desc>\n\r", lText, dateString];
lText = [NSString stringWithFormat:@"%@<trkseg>\n\r", lText];
for (int i=0; i<pPositions.count; i++) {
EMTrackPoint *lLocation = [pPositions objectAtIndex:i];
@autoreleasepool {
lText = [NSString stringWithFormat:@"%@<trkpt lat=\"%f\" lon=\"%f\">\n\r", lText, lLocation.coordinate.latitude, lLocation.coordinate.longitude];
lText = [NSString stringWithFormat:@"%@<ele>%f</ele>\n\r", lText, lLocation.altitude];
lText = [NSString stringWithFormat:@"%@<time>%@</time>\n\r", lText, [formatter stringFromDate:lLocation.timestamp]];
lText = [NSString stringWithFormat:@"%@<extensions speed=\"%f\"/>\n\r", lText, lLocation.mSpeed];
lText = [NSString stringWithFormat:@"%@</trkpt>\n\r", lText];
}
}
lText = [NSString stringWithFormat:@"%@</trkseg>\n\r", lText];
lText = [NSString stringWithFormat:@"%@</trk>\n\r", lText];
lText = [NSString stringWithFormat:@"%@</gpx>\n\r", lText];
return lText;
}
}
然而,在一些循环之后,我遇到了内存压力问题,我遇到了didReceiveMemoryWarning,然后我的应用程序崩溃了
我认为调用stringWithFormat会分配内存,但当我使用ARC时,我的字符串应该在不再需要时释放,对吗?
这可能是一个noob问题,但如果有人能帮助我理解我做错了什么,以及我应该在这里应用什么样的最佳实践,我将非常感谢 我没有关注问题的原因,而是关注其后果。
由于NSString有一个stringByAppendingString方法,所以我没有想到改用NSMutableString。基本的初级开发人员错误,谢谢你在这件事上帮助我热吻,duci9y和Bruno Werminghoff 从存储使用的角度来看,这是一种可怕的构造字符串的方法。看看NSMutableString。使用一个可变字符串并附加到它,而不是每次都创建一个新字符串。谢谢,这解决了我的问题!有时解决方案非常简单,以至于您根本看不到它:/n是否可以将格式为的字符串附加到NSMutableString?格式字符串更容易阅读。
NSMutableString*str=[NSMutableString new];[str appendFormat:@“此处格式%@”,“任意对象”]
UIApplication * application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
//To make the code block asynchronous
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *lGPX = [mTraceRecorder createGPXFromPositions:mTraceRecorder.mUserTrace andHikeName:mHikeInfo.mName];
//Clean up code. Tell the system that we are done.
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
});