美文网首页
获取Mac端的内存使用情况

获取Mac端的内存使用情况

作者: Tungkay | 来源:发表于2016-08-06 19:40 被阅读1089次

    最近遇到一个需求,要求实现Mac上的内存使用情况监测。之前没有做过这方面东西,查了一天资料把它给实现了。这个过程很坑,资料比较少,所以想着写下来,方便有需要的人使用。

    要获取Mac的内存情况,需要用到vm_statistics64_data_t或者 vm_statistics_data_t,前者对应64位的机器后者是32位的机器。由于现在大部分电脑都是64位的了,所以直接看vm_statistics64_data_t的实现。

    typedef struct vm_statistics64  vm_statistics64_data_t;
    

    可以看到它其实是一个名为vm_statistics64的结构体,我们再看看这个vm_statistics64的实现。

    /* 
    * vm_statistics64
    *
    * History:
    *   rev0 -  original structure.
    *   rev1 -  added purgable info (purgable_count and purges).
    *   rev2 -  added speculative_count.
    *      ----
    *   rev3 -  changed name to vm_statistics64.
    *       changed some fields in structure to 64-bit on 
    *       arm, i386 and x86_64 architectures.
    *   rev4 -  require 64-bit alignment for efficient access
    *       in the kernel. No change to reported data.
    *
    */
    
        struct vm_statistics64 {
        natural_t   free_count;     /* # of pages free */
        natural_t   active_count;       /* # of pages active */
        natural_t   inactive_count;     /* # of pages inactive */
        natural_t   wire_count;     /* # of pages wired down */
        uint64_t    zero_fill_count;    /* # of zero fill pages */
        uint64_t    reactivations;      /* # of pages reactivated */
        uint64_t    pageins;        /* # of pageins */
        uint64_t    pageouts;       /* # of pageouts */
        uint64_t    faults;         /* # of faults */
        uint64_t    cow_faults;     /* # of copy-on-writes */
        uint64_t    lookups;        /* object cache lookups */
        uint64_t    hits;           /* object cache hits */
        uint64_t    purges;         /* # of pages purged */
        natural_t   purgeable_count;    /* # of pages purgeable */
        /*
         * NB: speculative pages are already accounted for in "free_count",
         * so "speculative_count" is the number of "free" pages that are
         * used to hold data that was read speculatively from disk but
         * haven't actually been used by anyone so far.
         */
        natural_t   speculative_count;  /* # of pages speculative */
        
        /* added for rev1 */
        uint64_t    decompressions;     /* # of pages decompressed */
        uint64_t    compressions;       /* # of pages compressed */
        uint64_t    swapins;        /* # of pages swapped in (via compression segments) */
        uint64_t    swapouts;       /* # of pages swapped out (via compression segments) */
        natural_t   compressor_page_count;  /* # of pages used by the compressed pager to hold all the compressed data */
        natural_t   throttled_count;    /* # of pages throttled */
        natural_t   external_page_count;    /* # of pages that are file-backed (non-swap) */
        natural_t   internal_page_count;    /* # of pages that are anonymous */
        uint64_t    total_uncompressed_pages_in_compressor; /* # of pages (uncompressed) held within the compressor. */
        } __attribute__((aligned(8)));
    

    里面定义了OSX各种类型的内存,我们需要对这些值进行处理就可以获得正确的内存情况。

    • 总的内存: (active_count + inactive_count + speculative_count + wire_count + compressor_page_count + free_count) * 4096

    • 使用的内存: (active_count + inactive_count + speculative_count + wire_count + compressor_page_count - purgeable_count - external_page_count) * 4096

    • 应用内存: internal_page_count * 4096

    • 联动内存: wire_count * 4096

    • 已压缩: compressor_page_count * 4096

    • 已缓存文件: (purgeable_count + external_page_count) * 4096

    • 内存压力: (vm_stat.wire_count + vm_stat.compressor_page_count) / 总的内存 * 100%

    以上是一些常用的内存情况的数值。这里乘以4096是由平台决定,64位就需要乘以4096。我们可以通过以下方式获取vm_statistics64_data_t的值。

    - (void)memoryUsage
    {
     vm_size_t page_size;
    mach_port_t mach_port;
    mach_msg_type_number_t count;
    vm_statistics64_data_t vm_stat;
    
    mach_port = mach_host_self();
    count = HOST_VM_INFO64_COUNT;
    
    if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
        KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO64,
                                          (host_info64_t)&vm_stat, &count))
    {
    
        // 仅仅打印了内存压力,你可以打印其他的值
        // 这里的page_size就是4096,是由平台决定的,64位机器的值就是4096
       // 使用long long仅仅是害怕数值太大,不是必要的
        long long useAndCache = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.speculative_count + vm_stat.wire_count + vm_stat.compressor_page_count) * page_size;
        
        long long cache = (vm_stat.purgeable_count + vm_stat.external_page_count)  * page_size;
        
        long long used = useAndCache - cache;
        
        long long free = useAndCache + vm_stat.free_count - used;
        
        long long pressure = (vm_stat.wire_count + vm_stat.compressor_page_count) * page_size;
    
        NSLog(@"memory_pressure: %.2f%%",pressure * 1.0 / (free + used) * 100);
    }
    
    }
    

    我们可以通过命令来进行测试,打开终端输入memory_pressure -p 40,系统会使电脑的内存压力保持在60%,这样就可以验证是否正确了。更简单的方法是装一个iStat,可以直观看到内存的使用情况。

    最后需要注意的是,上面获取到的值都是byte为单位的,记得转换为自己需要的单位。

    相关文章

      网友评论

          本文标题:获取Mac端的内存使用情况

          本文链接:https://www.haomeiwen.com/subject/gknksttx.html