Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 314
Abhisek Mishra Avatar asked Dec 06 '25 03:12

Abhisek Mishra


1 Answers

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.

like image 119
Petesh Avatar answered Dec 08 '25 17:12

Petesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!