• c++获取mac 下总物理内存、所使用内存、当前进程所使用内存


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    enum BYTE_UNITS
    {
        BYTES = 0,
        KILOBYTES = 1,
        MEGABYTES = 2,
        GIGABYTES = 3
    };
    
    template <class T>
    T convert_unit(T num, int to, int from = BYTES)
    {
        for (; from < to; from++)
        {
            num /= 1024;
        }
        return num;
    }
    
    // 获取mac系统下总物理内存、所使用内存
    void getMemUsePercentage()
    {
        u_int64_t total_mem = 0;
        float used_mem = 0;
    
        vm_size_t page_size;
        vm_statistics_data_t vm_stats;
    
        // Get total physical memory
        int mib[] = { CTL_HW, HW_MEMSIZE };
        size_t length = sizeof(total_mem);
        sysctl(mib, 2, &total_mem, &length, NULL, 0);
    
        mach_port_t mach_port = mach_host_self();
        mach_msg_type_number_t count = sizeof(vm_stats) / sizeof(natural_t);
        if (KERN_SUCCESS == host_page_size(mach_port, &page_size) &&
            KERN_SUCCESS == host_statistics(mach_port, HOST_VM_INFO,
                (host_info_t)&vm_stats, &count)
            )
        {
            used_mem = static_cast<float>(
                (vm_stats.active_count + vm_stats.wire_count) * page_size);
        }
    
        uint usedMem = convert_unit(static_cast<float>(used_mem), MEGABYTES);
        uint totalMem = convert_unit(static_cast<float>(total_mem), MEGABYTES);
        cout << "\nusedMem:" << usedMem << endl;
        cout << "totalMem:" << totalMem << endl;
    }
    
    // 获取mac系统下当前进程使用内存
    int runGetDynamicProcInfo(unsigned int& m_nMemUsed)
    {
        unsigned int task = 0;
        int error = 0;
        unsigned int count = 0;
        struct task_basic_info ti;
    
        error = task_for_pid(mach_task_self(), getpid(), &task);
        if (error != KERN_SUCCESS)
        {
            m_nMemUsed = 0;
            return 0;
        }
        count = TASK_BASIC_INFO_COUNT;
        error = task_info(task, TASK_BASIC_INFO, (task_info_t)&ti, &count);
        if (error != KERN_SUCCESS)
        {
            m_nMemUsed = 0;
            return 0;
        }
    
        vm_region_basic_info_data_64_t b_info;
        unsigned long address = SHARED_REGION_BASE_PPC;
        unsigned long size = 0;
        unsigned int object_name = 0;
        count = VM_REGION_BASIC_INFO_COUNT_64;
        error = vm_region_64(task, &address, &size, VM_REGION_BASIC_INFO, (vm_region_info_t)&b_info, &count, &object_name);
        if (error == KERN_SUCCESS) 
        {
            if (b_info.reserved && size == (SHARED_REGION_NESTING_SIZE_PPC) &&
                ti.virtual_size > (SHARED_REGION_NESTING_SIZE_PPC + SHARED_REGION_NESTING_MIN_PPC))
            {
                ti.virtual_size -= (SHARED_REGION_NESTING_SIZE_PPC + SHARED_REGION_NESTING_MIN_PPC);
            }
        }
        m_nMemUsed = (ti.resident_size / 1024 / 1024);
        std::cout << "m_nMemUsed:" << m_nMemUsed << std::endl;
        return 0;
    }
    
    int main()
    {
        unsigned int m_usfmem = 0;
    
        while (1)
        {
            runGetDynamicProcInfo(m_usfmem);
            getMemUsePercentage();
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    
        return 0;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
  • 相关阅读:
    Elasticsearch
    提前预警,防患未然:看千寻位置如何助力兴国县“跑赢”地质灾害
    Qt状态机框架
    node.js PM2部署项目
    C# | AES加解密 - 快速上手
    LeetCode:买卖股票 系列 含冷冻期、含手续费(C++)
    2022年高考都结束了,还有人真觉得程序员下班后不需要学习吗?
    C# 给Json添加Note
    Spring Reactive:响应式编程与WebFlux的深度探索
    【FOC】FOC控制流程图
  • 原文地址:https://blog.csdn.net/weijianjain/article/details/133345820