I am trying to compare 2 files. When the files are very large (I'm testing with 1,7G) the process grows and grows until it stalls. Obviously ARC is not freeing up space correctly (or the objective-c librarys don't clean up after themselves).
In Instruments I see that "Malloc 4,0KB" has increasingly high Live Bytes - which points directly to NSFileHandle or NSData, as I am using a buffer size of 4096 Bytes.
I have tried a lot of different things already, like initializing the files with every loop and setting them to nil at the end of every loop. Nothing helps.
Any ideas? Would a NSAutoreleasePool help?
Here's the core code:
NSFileHandle *file1;
NSFileHandle *file2;
int bufferSize = 4096;
file1 = [NSFileHandle fileHandleForReadingAtPath: [self.input1 stringValue]];
file2 = [NSFileHandle fileHandleForReadingAtPath: [self.input2 stringValue]];
int long long size1 = [file1 seekToEndOfFile];
int long long size2 = [file2 seekToEndOfFile];
int long long pos1 = 0;
int long long pos2 = 0;
if(size1 != size2) {
NSLog(@"Files have different sizes");
return;
}
do {
[file1 seekToFileOffset: pos1];
[file2 seekToFileOffset: pos2];
NSLog(@"Reading at: %lli",[file1 offsetInFile]);
if(![[file1 readDataOfLength: bufferSize] isEqualToData: [file2 readDataOfLength: bufferSize]]) {
break;
}
pos1 += bufferSize;
pos2 += bufferSize;
} while(pos1 < size1);
if(pos1 < size1) {
NSLog(@"Files do not match");
} else {
NSLog(@"Files are an exact match");
}
As has happened to me before, after posting the question I had some new idea how to do it.
I used
NSData initWithContentsOfFile: options: NSDataReadingMappedAlways error:nil
to read the file, instead of NSFile. Now it works.
int bufferSize = 4096;
NSData *file1;
NSData *file2;
NSRange range1;
NSRange range2;
unsigned char buffer1[bufferSize];
unsigned char buffer2[bufferSize];
range1.length = bufferSize;
range2.length = bufferSize;
range1.location = 0;
range2.location = 0;
int long long size1 = [[[NSFileManager defaultManager] attributesOfItemAtPath: [self.input1 stringValue] error:nil] fileSize];
int long long size2 = [[[NSFileManager defaultManager] attributesOfItemAtPath: [self.input2 stringValue] error:nil] fileSize];
if(size1 != size2) {
NSLog(@"Files have different sizes");
return;
}
file1 = [[NSData alloc] initWithContentsOfFile:[self.input1 stringValue] options: NSDataReadingMappedAlways error: nil];
file2 = [[NSData alloc] initWithContentsOfFile:[self.input2 stringValue] options: NSDataReadingMappedAlways error: nil];
do {
[file1 getBytes: buffer1 range: range1];
[file2 getBytes: buffer2 range: range2];
NSData *data1 = [[NSData alloc] initWithBytes: buffer1 length: bufferSize];
NSData *data2 = [[NSData alloc] initWithBytes: buffer2 length: bufferSize];
if (![data1 isEqualToData: data2]) {
break;
}
NSLog(@"Reading: %i",(int)(((float)range1.location / (float)size1) * 10000) / 100);
range1.location += bufferSize;
range2.location += bufferSize;
} while(range1.location < size1);
if(range1.location < size1) {
NSLog(@"Files do not match");
} else {
NSLog(@"Files are an exact match");
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments