linux和windows获取RAM全局瞬时占用

 linux

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

class SystemMonitor {
public:
    long getMemoryUsage() const {
        std::ifstream meminfoFile("/proc/meminfo");
        if (!meminfoFile.is_open()) {
            std::cerr << "Failed to open /proc/meminfo file.\n";
            return -1;
        }

        std::string line;
        long totalMemory = -1;
        long freeMemory = -1;

        // Read the file line by line
        while (std::getline(meminfoFile, line)) {
            std::istringstream iss(line);
            std::string key;
            long value;

            if (iss >> key >> value) {
                if (key == "MemTotal:") {
                    totalMemory = value;
                } else if (key == "MemFree:") {
                    freeMemory = value;
                    break;  // No need to continue after finding MemFree
                }
            }
        }

        if (totalMemory == -1 || freeMemory == -1) {
            std::cerr << "Failed to extract memory information from /proc/meminfo.\n";
            return -1;
        }

        // Calculate used memory
        long usedMemory = totalMemory - freeMemory;

        return usedMemory / 1024;
    }
};

int main() {
    SystemMonitor monitor;

    // Example: Print used memory every 1 second
    for (int i = 0; i < 5; ++i) {
        sleep(1);
        std::cout << "Used Memory: " << monitor.getMemoryUsage() << " MB\n";
    }

    return 0;
}

windows

#include <iostream>
#include <windows.h>

class SystemMonitor {
public:
    long getMemoryUsage() const {
        MEMORYSTATUSEX memoryStatus;
        memoryStatus.dwLength = sizeof(memoryStatus);

        if (GlobalMemoryStatusEx(&memoryStatus)) {
            // 获取已用内存大小(物理内存 + 页面文件)
            long usedMemory = static_cast<long>(memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys +
                                               memoryStatus.ullTotalPageFile - memoryStatus.ullAvailPageFile);
            return usedMemory / (1024 * 1024);  // 转换为 MB
        } else {
            std::cerr << "Failed to get memory information.\n";
            return -1;
        }
    }
};

int main() {
    SystemMonitor monitor;

    // Example: Print used memory every 1 second
    for (int i = 0; i < 5; ++i) {
        Sleep(1000);
        std::cout << "Used Memory: " << monitor.getMemoryUsage() << " MB\n";
    }

    return 0;
}

相关推荐

  1. linuxwindows获取RAM全局瞬时占用

    2023-12-28 10:18:02       50 阅读
  2. 获取cpu全局瞬时占用[windows][linux][c++]

    2023-12-28 10:18:02       63 阅读
  3. ROM RAM

    2023-12-28 10:18:02       26 阅读
  4. WindowsLinux端口占用查看、杀死的方法

    2023-12-28 10:18:02       33 阅读
  5. linuxwindows对比

    2023-12-28 10:18:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 10:18:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 10:18:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 10:18:02       87 阅读
  4. Python语言-面向对象

    2023-12-28 10:18:02       96 阅读

热门阅读

  1. C++ 图形界面的贪吃蛇。

    2023-12-28 10:18:02       49 阅读
  2. uniapp中各种状态的按钮

    2023-12-28 10:18:02       67 阅读
  3. conda环境配置torch+cuda+cudnn+dgl用到的一些命令

    2023-12-28 10:18:02       45 阅读
  4. 多元函数极值@条件极值

    2023-12-28 10:18:02       67 阅读
  5. Springboot2+mybatisplus+多数据源更换mysql数据库为pgsql

    2023-12-28 10:18:02       56 阅读
  6. Track-Anything 追踪万物

    2023-12-28 10:18:02       68 阅读
  7. 算法设计与分析 | 动态规划

    2023-12-28 10:18:02       52 阅读
  8. 【pg】多个字段查重过滤

    2023-12-28 10:18:02       74 阅读
  9. 如何用matlab制作游戏?

    2023-12-28 10:18:02       66 阅读
  10. C++ 中使用 std::map 的一个示例

    2023-12-28 10:18:02       56 阅读