My coworker and I are having a killer of a time trying to figure out this problem. We want to parse a file (custom made 3d object file). The problem is in using the stdio function calls like fread, fseek, etc.
In the below code, when I check the virutal memory before call and after call I am missing like 1MB each time the function is run and I have no idea why this happens.
FILE *fp= fopen([path UTF8String], "rb");
char *buffer = (char *) malloc(MAX_STRING_LENGTH);
while(!feof(fp))
{
fgets(buffer, MAX_STRING_LENGTH, fp);
}
if(0 == fclose(fp))
NSLog(@"File is closed");
free(buffer)
Path is generated from ios Library and MAX_STRING_LENGTH is an arbitrarily large number (eg 2000000)
Also used the function from http://www.cplusplus.com/reference/clibrary/cstdio/fread/ in the example and still the memory error persists.
I know that buffer is not the problem because if I use just create the buffer and free it then no memory is taken but if I use the while loop then after the file is closed and I check the memory available I have one less MB than before. it has been driving me and my coworker crazy trying to fix this for the past week.
EDIT::
- (float)print_usage_memory
{
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
const int availablePages = vmStats.free_count;
float availableMemory = [self pagesToMB:availablePages];
/*
*/
const int totalPages = vmStats.wire_count + vmStats.active_count + vmStats.inactive_count + vmStats.free_count;
const int activePages = vmStats.active_count;
const int wiredPages = vmStats.wire_count;
const int purgeablePages = vmStats.purgeable_count;
NSMutableString* txt = [[NSMutableString alloc] initWithCapacity:512];
[txt appendFormat:@"\nTotal: %d (%.2fMB)\n", totalPages, [self pagesToMB:totalPages]];
[txt appendFormat:@" nAvailable: %d (%.2fMB)\n", availablePages, availableMemory];
[txt appendFormat:@" nActive: %d (%.2fMB)\n", activePages, [self pagesToMB:activePages]];
[txt appendFormat:@"nWired: %d (%.2fMB)\n", wiredPages, [self pagesToMB:wiredPages]];
[txt appendFormat:@"nPurgeable: %d (%.2fMB)\n", purgeablePages, [self pagesToMB:purgeablePages]];
NSLog(@"%@", txt);
[txt release];
txt = nil;
return availableMemory;
}
- (float)pagesToMB:(const int)pages
{
return pages*4/1024;
}
Thanks for any help you might be able to give, or any insights into this.