无需外部工具即可获得全系统内存

时间:2010-07-09 12:38:25

标签: c macos memory

要获得总安装RAM,我可以使用popen("sysctl -n hw.memsize", "r"),但有没有一些库可以在不运行外部工具的情况下获取此信息?

2 个答案:

答案 0 :(得分:5)

的sysctl

您可以使用sysctl(更详细here

  

本节中使用的符号是   在sysctl.h文件中声明。 -   功能:

int sysctl (int *names, int nlen, void *oldval, size_t *oldlenp, void *newval, size_t newlen)

的getrusage

getrusage

#include <sys/resource.h> 

int getrusage(int who, struct rusage *usage); 

struct rusage {
    struct timeval ru_utime; /* user time used */
    struct timeval ru_stime; /* system time used */
    long   ru_maxrss;        /* maximum resident set size */
    long   ru_ixrss;         /* integral shared memory size */
    long   ru_idrss;         /* integral unshared data size */
    long   ru_isrss;         /* integral unshared stack size */
    long   ru_minflt;        /* page reclaims */
    long   ru_majflt;        /* page faults */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block input operations */
    long   ru_oublock;       /* block output operations */
    long   ru_msgsnd;        /* messages sent */
    long   ru_msgrcv;        /* messages received */
    long   ru_nsignals;      /* signals received */
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

答案 1 :(得分:1)

您可以使用api getrusage

     int who = RUSAGE_SELF;
    struct rusage usage={0,};
    int ret=0;
    ret = getrusage(who, &usage);

查找rusage的结构并选择您关注的值。