Memory leak when comparing large files (under ARC)

Chris

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");
}
Chris

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

NSPlaceholderString Memory Leak with ARC

From Dev

memory leak in cellForRowAtIndexPath with ARC

From Dev

NSPlaceholderString Memory Leak with ARC

From Dev

memory leak with malloc / free with arc

From Dev

iOS drawInRect memory leak - ARC

From Dev

memory leak with malloc / free with arc

From Dev

memory leak in cell with UILabel with ARC

From Dev

CALayer causing memory leak with ARC

From Dev

Memory is not released under ARC

From Dev

Comparing large text files

From Dev

Large celery task memory leak

From Dev

NSArray or NSMutableArray possible memory leak using ARC

From Dev

Memory leak while resizing UIImage in arc

From Dev

Memory Leak in "executeFetchRequest" Core Data (ARC)

From Dev

NSArray or NSMutableArray possible memory leak using ARC

From Dev

objective c memory leak ARC with UIImage

From Dev

how to prevent ARC memory leak on iOS

From Dev

Memory leak when using DnsGetCacheDataTable

From Dev

Vulkan: Memory leak when rendering

From Dev

Memory leak when using WKScriptMessageHandler

From Dev

Memory leak when using MBProgressHUD

From Dev

Memory leak when using DnsGetCacheDataTable

From Dev

ARC: find memory leak calling drawInRect through a block

From Dev

Memory leak using ARC being indicated in iOS simulator

From Dev

AFNetworking 2 Post Asynchronous Memory Leak with ARC enabled

From Dev

Out of Memory Exception when handling large files in C#

From Dev

Python memory errors when hashing large number of files in sequence

From Dev

How to prevent memory leaks in RubyMotion when reading large files in loops

From Dev

Out of memory exception when using xlsx module with large files

Related Related

HotTag

Archive