본문 바로가기
프로그래밍/iOS

디바이스 메모리 체크

by 백룡화검 2011. 5. 26.
간단하게 메모리맄 체크하실때는
프리메모리만 따로 계산해서
사용하셔도 무방합니당;

출력은 각자 맘껏 바꾸세용 ㅎ;



#include <mach/host_info.h>

#include <mach/mach_init.h>

#include <mach/mach_host.h>

#include <mach/processor_info.h>



void checkMemory()

{

    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");


    //FIXME: NSRealMemoryAvailable 아이맥에선 1슬롯만 나오는듯?

    natural_t mem_real = NSRealMemoryAvailable();

    natural_t mem_free = vm_stat.free_count * pagesize;

    natural_t mem_used = (vm_stat.active_count +

                          vm_stat.inactive_count +

                          vm_stat.wire_count) * pagesize;

    

    natural_t mem_total = mem_used + mem_free;

    printf("\n");

    printf("**  MEMORY  **\n");

    printf("Real : %03u,%03u,%03u  %3umb\n",mem_real/1000/1000, mem_real/1000%1000, mem_real%1000, mem_real/1024/1024 );

    printf("Total: %03u,%03u,%03u  %3umb\n",mem_total/1000/1000,mem_total/1000%1000,mem_total%1000,mem_total/1024/1024);

    printf("Free : %03u,%03u,%03u  %3umb\n",mem_free/1000/1000, mem_free/1000%1000, mem_free%1000, mem_free/1024/1024 );

    printf("Used : %03u,%03u,%03u  %3umb\n",mem_used/1000/1000, mem_used/1000%1000, mem_used%1000, mem_used/1024/1024 );

    

    printf("\n");

}

출처 : 맥부기 카페