I am looping through an array of file paths retrieved by a core data predicate, zipping and uploading each file per iteration of the loop, about 4 or 5000 files in total. (The technique I use for zipping is basically the one described here: Best way to loop through a lot of files and zip each one separately?) Currently, I’m running the app in debug mode from Xcode. After a little while, with each iteration of the loop, not only does the app seem to run slower, Xcode does too – indeed, my whole computer becomes less responsive. I’m now researching general rules for efficient loops involving NSStrings, NSArrays, NSNotifications, etc but I’m also hoping someone could also point out obvious bottlenecks and inefficiencies in my code. Although my code uses garbage collection, I tried putting in release calls with the small hope that might make a difference though my understanding is that it shouldn’t. Here is an abridged version of my code with just the important parts:
//Zip paths saved in Core Data and upload them to a server
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
//get path of the shell script that zips a file
NSString *zipShellScript = [[NSBundle mainBundle] pathForResource:@"backupzipper"
ofType:@"sh"];
//get array of File managed objects related to User managed object
NSArray *filesToUpload = [User files];
//args for the shell script
//NSString *sourceFilePath = [NSString string];
//NSString *targetFilename = [NSString string];
//NSString *targetFilePath = [NSString string];
//the following appears to make a big improvement
NSString *sourceFilePath = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFileName = [[NSString alloc] initWithString:[NSString string]];
NSString *targetFilePath = [[NSString alloc] initWithString:[NSString string]];
NSError *error;
for (File *file in filesToUpload) //loop through array of File managed objects
{
NSTask *task = [[NSTask alloc] init];
sourceFilePath = file.path;
targetFilename = [NSString stringWithFormat:@"%@.zip",
[[sourceFilePath lastPathComponent]
stringByDeletingPathExtension]]; //e.g. misc.doc will produce misc.zip
[task setArguemnts:[NSArray arrayWithObjects:zipShellScript,
sourceFilePath, //full path of file to zip e.g. /Users/stifin/documents/misc.doc
workingDirectory, //where the zip will be placed e.g. /Users/stifin/Library/Application Support/MyApp/
targetFilename, //filename after zipping e.g. misc.zip
nil]];
[sourceFilePath release]; //my attempt to reduce memory usage even though this is garbage collected app
[targetFilename release];
[task setLaunchPath@"/bin/sh"];
[task setStandardInput:[NSPipe pipe]]; //fixes odd behaviour where NSLog no longer works after NSTask
//do zip
[task launch];
[task waitUntilExit];
if ([task terminationReason] == NSTaskTerminationReasonExit)
{
[task release]; //doubt this helps
[self uploadFile:targetFilePath]; //method just sleeps for 1 sec to simulate upload time
file.dateUploaded = [NSDate date];
error = nil;
if ([context save:&error])
NSLog(@"Saved ok");
else
NSLog(@"Save error: %@", [error localizedDescription]);
//delete zip from working directory
[self cleanUpFile:targetFilePath];
[targetFilePath release]; //doubt this helps
//send notification of file processed
info = [NSDictionary dictionaryWithObjectsAndKeys:
file.filename, @"name",
file.sizeBytes, @"size",
[NSNumber numberWithBool:YES], @"success",
nil];
[nc postnNotificationName:@"FileProcessed" object:nil userInfo:info];
[info release]; //doubt this helps
}
else {
//handle task failure
}
}
[nc removeObserver:self];
As noted by the documentation, -release is ignored. If you want to collect immediately, use
[[NSGarbageCollector defaultCollector] collectIfNeeded];or, even-collectExhaustively. Don’t do this every trip through the loop, just every hundred or so.Of course all this assumes you’ve used Instruments to make sure this is the issue. You haven’t said whether you’ve done so, which leads me to believe you’re making assumptions. The problem might just be elsewhere (though I agree this is by far the most likely candidate – GC + many allocations in a tight loop needs some extra care). Not a good way to work. Measure first, then optimize.