gpt4 book ai didi

iphone - 我不明白为什么我的 iPhone 应用程序内存不足

转载 作者:行者123 更新时间:2023-12-03 20:20:23 26 4
gpt4 key购买 nike

我正在将文本文件中的一堆数据加载到 CoreData 中。由于某种原因,下面代码中的 for(行中行)循环的每次迭代都会分配新的内存。它工作正常,直到无法分配新内存为止。我不明白为什么它不为每次迭代重用相同的内存。我究竟做错了什么?任何帮助将不胜感激。

`

-(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];

}`

最佳答案

您可能会在此循环内生成大量自动释放的对象。请记住,自动释放的对象在整个事件循环结束之前仍然有效,此时自动释放池将被耗尽。通常这个过程或多或少是不可见的,但是如果你有分配大量自动释放对象的循环,你可能耗尽堆。

您可以通过在循环中创建和耗尽内部自动释放池来缓解这种情况,如下所示:

for (line in lines) {  
NSAutoreleasePool * localPool = [[NSAutoreleasePool alloc] init];
...
[localPool release];
}

您还应该显式释放不再需要的任何创建/保留的对象,例如上面的“图表”等。

关于iphone - 我不明白为什么我的 iPhone 应用程序内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4662715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com