我sysinfo
用来获取内存信息。但结果似乎是错误的,因为它与/proc/meminfo
.
我的代码:
#include <stdio.h>
#include <sys/sysinfo.h>
double megabyte = 1024 * 1024;
long
get_free_mem() {
struct sysinfo si;
int error = sysinfo(&si);
printf("code error=%d\n",error);
printf("available: %lu\n", si.freeram );
printf("total: %lu\n", si.totalram);
printf("mem_unit: %u \n", si.mem_unit);
return 0;
}
int main(int argc, char* argv[])
{
get_free_mem();
return 0;
}
输出信息是:
$./get_mem
code error=0
available: 67733753856
total: 135443968000
mem_unit: 1
但/proc/meminfo
显示:
$cat /proc/meminfo
MemTotal: 8388608 kB
MemFree: 4790464 kB
Buffers: 0 kB
Cached: 3042084 kB
并free
显示:
$free -m
total used free shared buffers cached
Mem: 8192 3513 4678 0 0 2970
-/+ buffers/cache: 543 7648
Swap: 1952 0 1952
很明显我的代码有问题,因为我在这台服务器上确实没有 135443968000 字节(几乎 126 Gb,太大了)。
核心:
Linux my_hostname 2.6.32-220.23.2.ali1113.el5.x86_64 #1 SMP Thu Jul 4 20:09:15 CST 2013 x86_64 x86_64 x86_64 GNU/Linux