sysinfo system call not returning correct freeram value
Asked Answered
M

3

4

I recently wrote the following C code using sysinfo systemcall to display system statistics, what amused me was that the freeram variable of sysinfo structure doesn't return the amount of free RAM instead it is returning the current RAM usage. I had to use a workaround to show the correct value by subtracting freeram from totalram. I have tried googling about this specific variable but to no avail. Any insight into this weird behavior would be really helpful.

/*
 * C program to print the system statistics like system uptime, 
 * total RAM space, free RAM space, process count, page size
 */

#include <sys/sysinfo.h>    // sysinfo
#include <stdio.h>
#include <unistd.h>     // sysconf
#include "syscalls.h"       // just contains a wrapper function - error

int main()
{
    struct sysinfo info;

    if (sysinfo(&info) != 0)
        error("sysinfo: error reading system statistics");

    printf("Uptime: %ld:%ld:%ld\n", info.uptime/3600, info.uptime%3600/60, info.uptime%60);
    printf("Total RAM: %ld MB\n", info.totalram/1024/1024);
    printf("Free RAM: %ld MB\n", (info.totalram-info.freeram)/1024/1024);
    printf("Process count: %d\n", info.procs);
    printf("Page size: %ld bytes\n", sysconf(_SC_PAGESIZE));

    return 0;
}
Maclean answered 24/1, 2012 at 13:30 Comment(2)
info.freeram works correctly on my box. Get rid of the "syscalls.h".Quenby
syscalls.h can't possible affect the code in anyway. See the answer by @shadyabhi below.Maclean
J
1

Remove

#include "syscalls.h"

May be, you borrowed the code from somewhere and edited. Double quotes are used to import unofficial header files. That custom header file is not really required.

It's not needed. You code will run fine.

On my PC, freeram value of $free -m matches with info.freeram of the program. Apparently, freeram is not what you think it's showing.

Read more about the http://www.redhat.com/advice/tips/meminfo.html

MemFree is the free memory & MemFree + Buffers + Cached is available memory (which you want). So, you are just understanding the term freeram wrongly.

Jerriejerrilee answered 24/1, 2012 at 13:33 Comment(4)
If you are trying to run the code I posted above, it's definitely correct, since the workaround is present in the code: printf("Free RAM: %ld MB\n", (info.totalram-info.freeram)/1024/1024); try running instead with after replacing to printf("Free RAM: %ld MB\n", info.freeram/1024/1024); Also syscalls.h just contains a wrapper function error.Maclean
Agree with you, free -m behaves similarly. I went through the link you provided but can't understand yet why freeram/MemFree give values which are close to memory used statistic shown by GNOME System Monitor; shouldn't this be the other way round? (Or to put it in other words, why is freeram not equal to free ram on my system?)Maclean
@Maclean As I told, in Gnome System monitor, it shows MemFree + Buffers + Cached. (I have already mentioned it in answer)Jerriejerrilee
Finally found the answer in its full detail after reading linuxatemyram.com/index.htmlMaclean
P
3

The "free ram" field is meaningless to most people. The closest thing to a real "free ram" value is taking the fields from /proc/meminfo and subtracting Committed_AS from MemTotal. The result could be negative if swap is in use (this means there's more memory allocated than will fit in physical ram); if you want to count swap as memory too, just use MemTotal+SwapTotal as your total.

Pudgy answered 24/1, 2012 at 15:13 Comment(7)
@r please shed some light on why is it meaningless.Maclean
Any modern system will aim to keep all memory "in use" at all times, but there's a difference between using it and committing it to long-term use for data that can't be discarded or synced to disk. Most of the "in use" memory you see is actually cached copies of files on disk. Some of these are programs currently executing, others are files that have been read once that the kernel expects might be read again. Committed memory, on the other hand, stores actual variable data that doesn't exist anywhere on disk (unless it gets swapped).Pudgy
And for practical purposes, memory that's "in use" but not committed is free.Pudgy
Finally found the answer in its full detail after reading linuxatemyram.com/index.html :-)Maclean
Nice link, amusing, +1. :-)Pudgy
@R.. One comment, the MemAvailable field from /proc/meminfo is now the correct way to check for the available memory. See the commit /proc/meminfo: provide estimated available memory. Credits to @VickyChijwani, that posted a comment with that link here.Hillell
@SRG: It's really not. It's an estimate and includes memory that can only be used by the kernel for caching or kernel resources when overcommit is disabled, and which will lead to OOM-killing if you use that much when overcommit is enabled. Commit_Limit-Committed_AS is the limit for what you can safely allocate.Pudgy
J
1

Remove

#include "syscalls.h"

May be, you borrowed the code from somewhere and edited. Double quotes are used to import unofficial header files. That custom header file is not really required.

It's not needed. You code will run fine.

On my PC, freeram value of $free -m matches with info.freeram of the program. Apparently, freeram is not what you think it's showing.

Read more about the http://www.redhat.com/advice/tips/meminfo.html

MemFree is the free memory & MemFree + Buffers + Cached is available memory (which you want). So, you are just understanding the term freeram wrongly.

Jerriejerrilee answered 24/1, 2012 at 13:33 Comment(4)
If you are trying to run the code I posted above, it's definitely correct, since the workaround is present in the code: printf("Free RAM: %ld MB\n", (info.totalram-info.freeram)/1024/1024); try running instead with after replacing to printf("Free RAM: %ld MB\n", info.freeram/1024/1024); Also syscalls.h just contains a wrapper function error.Maclean
Agree with you, free -m behaves similarly. I went through the link you provided but can't understand yet why freeram/MemFree give values which are close to memory used statistic shown by GNOME System Monitor; shouldn't this be the other way round? (Or to put it in other words, why is freeram not equal to free ram on my system?)Maclean
@Maclean As I told, in Gnome System monitor, it shows MemFree + Buffers + Cached. (I have already mentioned it in answer)Jerriejerrilee
Finally found the answer in its full detail after reading linuxatemyram.com/index.htmlMaclean
U
1

you need to multiply by mem_unit.

Unilingual answered 28/1, 2017 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.