我正在开发一个 OS X 应用程序,该应用程序一直运行并处理他队列中的所有内容。这些作业将其进程记录在标准输出中。我正在尝试使用在后台线程中运行的 while 循环实时从标准输出中获取此信息。问题是运行 12 小时后,内存使用量从 25MB 上升到 >750MB。该应用程序正在使用 ARC,所以我在这里有点迷路。任何人都知道如何解决这个问题?
NSFileHandle *fileStd = [stdPipe fileHandleForReading];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: cmd];
[task setArguments: arguments];
NSPipe *stdPipe = [NSPipe pipe];
NSPipe *errPipe = [NSPipe pipe];
[task setStandardOutput: stdPipe];
[task setStandardError: errPipe];
NSFileHandle *fileStd = [stdPipe fileHandleForReading];
[task launch];
NSData *readData;
NSString *logString;
while ((readData = [fileStd availableData]) && [readData length]){
logString = [[NSString alloc] initWithData: readData encoding: NSUTF8StringEncoding];
[self parseLog:logString];
}
readData = nil;
}
[task waitUntilExit];
和 parseLog 方法:
+ (void) parseLog:(NSString *)output {
NSArray *chunks = [output componentsSeparatedByString: @"\r"];
NSString *lastLine = [chunks lastObject];
[lastLine writeToFile:@"/tmp/status.log" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSRange textRange = [lastLine rangeOfString:@"<TEST> "];
if(textRange.location != NSNotFound) {
NSMutableDictionary *info = [NSMutableDictionary dictionary];
[info setObject:lastLine forKey:@"test"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"handleNotification" object:nil userInfo:info];
}
}