[英]I can't figure out why my iPhone App is running out of memory
I am loading a bunch of data from a text file into CoreData. 我正在从文本文件中将一堆数据加载到CoreData中。 For some reason, every iteration through the for (line in lines) loop in the code below is allocating new memory.
出于某种原因,在下面的代码中通过for(行中的行)循环的每次迭代都是分配新的内存。 It works fine up until it can't allocate new memory.
它工作正常,直到它无法分配新的内存。 I can't see why it's not reusing the same memory for each iteration.
我不明白为什么它不会为每次迭代重复使用相同的内存。 What am I doing wrong?
我究竟做错了什么? Any help would be appreciated.
任何帮助,将不胜感激。
` `
-(IBAction)loadDataInThread { - (IBAction)loadDataInThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MileMarkers" ofType:@"csv"];
NSData *myString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [myString componentsSeparatedByString:@"\r"];
status.hidden = FALSE;
// NSManagedObjectContext *context = [self managedObjectContext];
NSFileManager *fileman = [NSFileManager defaultManager];
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"];
NSString *line;
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]];
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSManagedObject *newManagedObject;
NSArray *fields;
// NSLog(@"Loading Markers");
for (line in lines) {
fields = [line componentsSeparatedByString:@","];
self.stat = [NSString stringWithFormat:@"%@ %@ %@ %@",[fields objectAtIndex:0],[fields objectAtIndex:1],[fields objectAtIndex:2],[fields objectAtIndex:3]];
NSLog(stat);
[self performSelectorOnMainThread:@selector(updateStatus) withObject:nil waitUntilDone:YES];
newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"MileMarkers" inManagedObjectContext:self.managedObjectContext];
NSNumber *f1 = [myNumFormatter numberFromString:[fields objectAtIndex:0]];
NSNumber *f2 = [myNumFormatter numberFromString:[fields objectAtIndex:1]];
NSNumber *f3 = [myNumFormatter numberFromString:[fields objectAtIndex:2]];
NSNumber *f4 = [myNumFormatter numberFromString:[fields objectAtIndex:3]];
[newManagedObject setValue:f1 forKey:@"ICWID"];
[newManagedObject setValue:f2 forKey:@"MileMarker"];
[newManagedObject setValue:f3 forKey:@"Latitude"];
[newManagedObject setValue:f4 forKey:@"Longitude"];
NSMutableArray *charts = [[NSMutableArray alloc] initWithCapacity:4];
NSDirectoryEnumerator *e = [fileman enumeratorAtPath:tileDirectory];
NSString *path;
int idx = 0;
/*
for (path in e) {
NSString *pathFileName = [NSString stringWithFormat:@"%@/%@/tilemapresource.xml",tileDirectory,[path stringByDeletingPathExtension]];
// NSLog(@"parsing %@",pathFileName);
BOOL checkFile = [self checkForLatLong:pathFileName latitude:f3 longitude:f4 ];
if (checkFile) {
NSLog(@"Match %d",idx);
if (idx < 4){
self.counter.text = [NSString stringWithFormat:@"%d",idx];
[charts insertObject:path atIndex:idx];
idx++;
}
}
}
[path release];
for (int lp=1; lp<[charts count]; lp++) {
NSString *keyName = [NSString stringWithFormat:@"Chart%d",lp+1];
[newManagedObject setValue:[charts objectAtIndex:lp] forKey:keyName];
}
[charts release];
*/
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[error release];
}
[myNumFormatter release];
NSLog(@"Markers Loaded");
[self performSelectorOnMainThread:@selector(dataLoaded) withObject:nil waitUntilDone:YES];
[pool release];
} ` }
It's likely that you're producing lots of autoreleased objects inside this loop. 很可能你在这个循环中产生了很多自动释放的对象。 Remember that autoreleased objects are still valid until the end of the whole event loop, at which point the autorelease pool is drained.
请记住,自动释放的对象在整个事件循环结束之前仍然有效,此时自动释放池已耗尽。 Normally this process works more or less invisibly, but if you have loops that allocate lots of autoreleased objects, you can run out of heap.
通常这个过程或多或少是不可见的,但如果你有循环分配大量自动释放的对象,你可以用完堆。
You can alleviate this by creating and draining an inner autorelease pool within your loop, like so: 您可以通过在循环中创建和排空内部自动释放池来缓解这种情况,如下所示:
for (line in lines) {
NSAutoreleasePool * localPool = [[NSAutoreleasePool alloc] init];
...
[localPool release];
}
You should also explicitly release any created/retained objects that you don't need any more, like "charts" etc in the above. 您还应该显式释放您不再需要的任何创建/保留对象,例如上面的“图表”等。
The "charts" mutable array is not being released. “图表”可变数组尚未发布。
This line, 这条线,
NSMutableArray *charts = [[NSMutableArray alloc] initWithCapacity:4];
完成后你必须发布图表 ,如下所示:
[a1 release]
Classic memory management problem. 经典的内存管理问题。
Check this out... memory management 检查一下... 内存管理
There are tons of other links out there as well... 那里还有很多其他链接......
In Objective-C, always remember this tag line- 在Objective-C中,始终记住此标记行 -
When you use alloc or new or make a copy, you should definitely release it.
That is you are managing the resources not the compiler. 那就是你管理资源而不是编译器。 So it is your job to release them.
所以发布它们是你的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.