1

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
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
thomaslee
  • 407
  • 1
  • 7
  • 21
  • Chanses are that you have same issue as one mentioned here: http://stackoverflow.com/questions/8987636/sysinfo-system-call-not-returning-correct-freeram-value – HighPredator Feb 27 '15 at 08:26
  • Thanks for help. I have seen the linked question before. But not completely solve my confusion. What puzzled me is why my C codes get total memory size is 126GB, while the real value is 8GB. – thomaslee Feb 27 '15 at 09:05
  • Weird, because for me your snippet shows correct values. Also it is very strange to see `mem_unit: 1`. Usually, it must be `4096` Are you sure you're executing exactly the same code snipped as you've posted? – GreenScape Feb 27 '15 at 10:43
  • Yes, I execute exactly the above code. It's so strange that I tried the same code on another server, but it gave correct result and `mem_unit=4096` as you say. I had to dejectedly give up to find the reason , and now I read `/proc/meminfo` to get the memory info. – thomaslee Feb 27 '15 at 15:15

2 Answers2

2

To get the correct memory value for say, total RAM, you need to multiply si.total and si.memunit and then divide by 1024.

Same goes for Free RAM, Buffer RAM, etc.

EDIT: There is nothing wrong with memunit being set to 1.

See also C - sysinfo() returning bad values i686

grtcdr
  • 45
  • 2
  • 7
1

The actual free memory available is:

Actual Free Memory = Free (39 MB) + Buffers (95) + Cached (3590) = 3,724 MB

That’s 95x more free memory than than we initially thought. Whenever there is free memory available, it takes it temporarily as cache memory and buffers. Linux reduces its cache / buffers usage and give the program what it wants.

See this link and this link for example and detailed information.

Vineet1982
  • 7,730
  • 4
  • 32
  • 67
  • Thanks for reply! May be my statement is not clear enough. The problem is that, my C codes get total memory is 126GB, but the actual memory size is 8GB. – thomaslee Feb 27 '15 at 08:59
  • Linux needs lots of Memory and Whenever there is free memory available, it takes it temporarily as cache and cache is included in your memory details – Vineet1982 Feb 27 '15 at 09:06