How do you determine the amount of Linux system RAM in C++?

前端 未结 4 1590
迷失自我
迷失自我 2020-12-04 17:54

I just wrote the following C++ function to programatically determine how much RAM a system has installed. It works, but it seems to me that there should be a simpler way to

相关标签:
4条回答
  • 2020-12-04 18:34

    On Linux, you can use the function sysinfo which sets values in the following struct:

       #include <sys/sysinfo.h>
    
       int sysinfo(struct sysinfo *info);
    
       struct sysinfo {
           long uptime;             /* Seconds since boot */
           unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
           unsigned long totalram;  /* Total usable main memory size */
           unsigned long freeram;   /* Available memory size */
           unsigned long sharedram; /* Amount of shared memory */
           unsigned long bufferram; /* Memory used by buffers */
           unsigned long totalswap; /* Total swap space size */
           unsigned long freeswap;  /* swap space still available */
           unsigned short procs;    /* Number of current processes */
           unsigned long totalhigh; /* Total high memory size */
           unsigned long freehigh;  /* Available high memory size */
           unsigned int mem_unit;   /* Memory unit size in bytes */
           char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
       };
    

    If you want to do it solely using functions of C++ (i would stick to sysinfo), i recommend taking a C++ approach using std::ifstream and std::string:

    unsigned long get_mem_total() {
        std::string token;
        std::ifstream file("/proc/meminfo");
        while(file >> token) {
            if(token == "MemTotal:") {
                unsigned long mem;
                if(file >> mem) {
                    return mem;
                } else {
                    return 0;       
                }
            }
            // ignore rest of the line
            file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        return 0; // nothing found
    }
    
    0 讨论(0)
  • 2020-12-04 18:41

    There's no need to use popen(), you can just read the file yourself. Also, if there first line isn't what you're looking for, you'll fail, since head -n1 only reads the first line and then exits. I'm not sure why you're mixing C and C++ I/O like that; it's perfectly OK, but you should probably opt to go all C or all C++. I'd probably do it something like this:

    int GetRamInKB(void)
    {
        FILE *meminfo = fopen("/proc/meminfo", "r");
        if(meminfo == NULL)
            ... // handle error
    
        char line[256];
        while(fgets(line, sizeof(line), meminfo))
        {
            int ram;
            if(sscanf(line, "MemTotal: %d kB", &ram) == 1)
            {
                fclose(meminfo);
                return ram;
            }
        }
    
        // If we got here, then we couldn't find the proper line in the meminfo file:
        // do something appropriate like return an error code, throw an exception, etc.
        fclose(meminfo);
        return -1;
    }
    
    0 讨论(0)
  • 2020-12-04 18:44

    Even top (from procps) parses /proc/meminfo, see here.

    0 讨论(0)
  • 2020-12-04 18:48

    Remember /proc/meminfo is just a file. Open the file, read the first line, close the file. Voilá!

    0 讨论(0)
提交回复
热议问题