内存信息

可通过终端命令和代码的方式查看系统内存的使用情况,CPU信息一般只需要在终端查看,代码中很少用到。

终端命令

//z6、z6s、H500s平台下通过ADB查看:
    adb shell cat -ZkKz /proc/meminfo    //查看内存信息
    adb shell cat -ZkKz /proc/cpuinfo    //CPU信息
    adb shell busybox top -ZkKz          //实时查看CPU使用情况

//其他平台下通过ADB查看:
    adb shell cat /proc/meminfo          //查看内存信息
    adb shell cat /proc/cpuinfo          //CPU信息
    adb shell busybox top                //实时查看CPU使用情况

//通过调试串口查看:
    cat /proc/meminfo                    //查看内存信息
    cat /proc/cpuinfo                    //CPU信息
    busybox top                          //实时查看CPU使用情况

代码


#define MAX_LINE_LENGTH 256
/*
 * @内存信息
 */
typedef struct{
    unsigned long MemTotal;
    unsigned long MemFree;
    unsigned long MemAvailable;
    unsigned long Buffers;
    unsigned long Cached;
    unsigned long SwapCached;
    unsigned long Active;
    unsigned long Inactive;
    unsigned long Active_anon;
    unsigned long Inactive_anon;
    unsigned long Active_file;
    unsigned long Inactive_file;
// 添加其他内存信息
}MemInfo;

MemInfo Getmeminfo(){
    MemInfo memInfo;
    FILE *file = fopen("/proc/meminfo", "r");
    if (file == NULL) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    char line[MAX_LINE_LENGTH];

    // 逐行读取文件内容并解析内存信息
    while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
        if (strstr(line, "MemTotal:") != NULL) {
            sscanf(line, "MemTotal: %lu kB", &memInfo.MemTotal);
        } else if (strstr(line, "MemFree:") != NULL) {
            sscanf(line, "MemFree: %lu kB", &memInfo.MemFree);
        } else if (strstr(line, "MemAvailable:") != NULL) {
            sscanf(line, "MemAvailable: %lu kB", &memInfo.MemAvailable);
        }
        // 添加其他内存信息的解析逻辑
    }
    fclose(file);
    return memInfo;
}
powered by Gitbooklast modified: 2024-01-02 19:25:01

results matching ""

    No results matching ""