Abhisek Mishra
Abhisek Mishra

Reputation: 41

iOS8 , Xcode6 how to get memory usage programmatically as shown by Xcode

I am using the following to get the memory usage:

 struct task_basic_info info;
    mach_msg_type_number_t sizeNew = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                                   TASK_BASIC_INFO,
                                   (task_info_t)&info,
                                   &sizeNew);
    if( kerr == KERN_SUCCESS ) {
        printf("Memory in use (in bytes): %u", info.resident_size);
    } else {
        printf("Error with task_info(): %s", mach_error_string(kerr));

    }

But the memory returned by this is much higher than that of shown by XCode6, any one else facing the same issue ?

Upvotes: 3

Views: 202

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94584

Resident set size (RSIZE) is not the same as the 'amount of memory used'. it includes the code as well.

You're probably looking for the top equivalent of RPRVT from the top program.

Obtaining that information requires walking the VM information for the process. Using the code for libtop.c, function libtop_update_vm_regions as a template, you would need to walk through the entire memory map adding up all the private pages. There's a simpler example of walking the address space, which can be used as a basis for calculating this size. You're looking for the VPRVT value, not the RPRVT value.

I don't currently have a mac to hand to write out an example with any degree of confidence that would work.

Upvotes: 1

Related Questions