Reputation: 53
it is possible on the iPhone to get the memory consumption for a specific program? For example, I want to know how many memory my program is used without using the instrument tool.
Thanks in advance. Alex
Upvotes: 0
Views: 349
Reputation: 170317
I'd highly recommend using the Memory Monitor instrument to track a particular application's memory usage.
You can get system-wide memory usage statistics via a function like the following:
#import <mach/mach.h>
#import <mach/mach_host.h>
static void print_free_memory () {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
NSLog(@"Failed to fetch vm statistics");
/* Stats in bytes */
natural_t mem_used = (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * pagesize;
natural_t mem_free = vm_stat.free_count * pagesize;
natural_t mem_total = mem_used + mem_free;
NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);
}
This code is copied from Landon Fuller's post on the topic. Noel Llopis also has a nice writeup on investigating memory usage from within an application. Unfortunately, it doesn't look like it's easy to know an application's memory usage from within that application itself.
Upvotes: 1
Reputation: 34945
This is tricky. Instruments really is the way to go here I think. Or jailbreak your iPhone and install the top
command from Cydia.
There are also some values that you can get with sysctl
but I think those are global memory usage and not per app.
Upvotes: 0