IOS内存等信息

1. 获取IOS APP占用的内存

#import <mach/mach.h>

// ...

void report_memory(void) {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
void report_memory(void) {
static unsigned last_resident_size=;
static unsigned greatest = ;
static unsigned last_greatest = ; struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
int diff = (int)info.resident_size - (int)last_resident_size;
unsigned latest = info.resident_size;
if( latest > greatest ) greatest = latest; // track greatest mem usage
int greatest_diff = greatest - last_greatest;
int latest_greatest_diff = latest - greatest;
NSLog(@"Mem: %10u (%10d) : %10d : greatest: %10u (%d)", info.resident_size, diff,
latest_greatest_diff,
greatest, greatest_diff );
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
last_resident_size = info.resident_size;
last_greatest = greatest;
}
void report_memory(void)
{
struct mach_task_basic_info info;
mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
kern_return_t kerr = task_info(mach_task_self(),
MACH_TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
NSLog(@"Memory in use (in bytes): %u", info.resident_size);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
//test 1
struct mach_task_basic_info info;
mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
kern_return_t kerr = task_info(mach_task_self(),
MACH_TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
CCLOG("Memory in use (in bytes): %llu", info.resident_size);
} else {
CCLOG("Error with task_info()");
} //test 2
mach_port_t host_port = mach_host_self();
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(host_port, &pagesize);
// host_statistics(host_t host_priv, host_flavor_t flavor, host_info_t host_info_out, <#mach_msg_type_number_t *host_info_outCnt#>)
// host_statistics64(host_t host_priv, host_flavor_t flavor, host_info64_t host_info64_out, mach_msg_type_number_t *host_info64_outCnt)
int r = host_statistics(host_port, HOST_VM_INFO, (host_info64_t)&vm_stat, &host_size);
if (r != KERN_SUCCESS)
CCLOG("Failed to fetch vm statistics");
float active = (float)(vm_stat.active_count * pagesize) / 1048576.0f;
float inactive = (float)(vm_stat.inactive_count * pagesize)/1048576.0f;
float wire = (float)(vm_stat.wire_count * pagesize)/1048576.0f; unsigned long int mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
unsigned long int mem_free = vm_stat.free_count * pagesize;
unsigned long int mem_total = mem_used + mem_free;
CCLOG("page size: %u", pagesize);
CCLOG("memory active: %f, memory inactive: %f, memory wire: %f", active, inactive, wire);
CCLOG("memory used: %lu, memory free: %lu, memory total: %lu", mem_used, mem_free, mem_total);

Resident memory is a measurement of the memory that has been allocated to your application and has not yet been reclaimed by the system, but some/most of the resident memory could be reclaimed by the system.

http://*.com/questions/18642421/understanding-task-basic-info-task-resident-size

http://*.com/questions/787160/programmatically-retrieve-memory-usage-on-iphone

C/C++ tip: How to get the process resident set size (physical memory use)

http://nadeausoftware.com/articles/2012/07/c_c_tip_how_get_process_resident_set_size_physical_memory_use#taskinfoforcurrentresidentsetsize

这篇文章救了我一天:

http://gamesfromwithin.com/whered-that-memory-go

上一篇:ASP.NET中的Request和Respone对象的使用


下一篇:203 Remove Linked List Elements 删除链表中的元素