I use sysinfo
to get memory information. But the result seems to be wrong, as it isn't consistent with /proc/meminfo
.
My codes:
#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;
}
And output info is:
$./get_mem
code error=0
available: 67733753856
total: 135443968000
mem_unit: 1
But /proc/meminfo
shows:
$cat /proc/meminfo
MemTotal: 8388608 kB
MemFree: 4790464 kB
Buffers: 0 kB
Cached: 3042084 kB
And free
shows:
$free -m
total used free shared buffers cached
Mem: 8192 3513 4678 0 0 2970
-/+ buffers/cache: 543 7648
Swap: 1952 0 1952
It's obviously that something wrong in my codes, because I indeed don't have 135443968000 Bytes (almost 126 Gb, that's to large) on this server.
kernel:
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