sysinfo returns incorrect value for freeram (even with mem_unit)
Asked Answered
M

1

1

My full C MATE applet can be found at github here: https://github.com/geniass/mate-resource-applet/tree/cmake (BRANCH CMAKE). It's a hacky mess right now, so see the code below.

I couldn't find an applet to display my computer's free ram, so that's basically what this is. I am using sysinfo to get this information, and it works fine for my system's total ram (roughly 4GB, it shows 3954 MB). htop shows 3157 MB used of 3954 MB.

However, the value sysinfo gives for free ram (136 MB) is obviously wrong (if free ram is ram that hasn't been allocated or something, I don't know).

This question is the same problem, but the solution, involving mem_unit, doesn't work because mem_unit = 1 on my system.

Here's a minimal program that gives the same values:

#include <stdio.h>
#include <sys/sysinfo.h>


int main() {
    /* Conversion constants. */
    const long minute = 60;
    const long hour = minute * 60;
    const long day = hour * 24;
    const double megabyte = 1024 * 1024;
    /* Obtain system statistics. */
    struct sysinfo si;
    sysinfo (&si);
    /* Summarize interesting values. */
    printf ("system uptime : %ld days, %ld:%02ld:%02ld\n", 
            si.uptime / day, (si.uptime % day) / hour, 
            (si.uptime % hour) / minute, si.uptime % minute);
    printf ("total RAM   : %5.1f MB\n", si.totalram / megabyte);
    printf ("free RAM   : %5.1f MB\n", si.freeram / megabyte);
    printf ("mem_unit:   : %u\n", si.mem_unit);
    printf ("process count : %d\n", si.procs);
    return 0;
}

Output:

system uptime : 0 days, 10:25:18
total RAM   : 3953.9 MB
free RAM   : 162.1 MB
mem_unit:   : 1
process count : 531

What's going on here? Is freeram not what I think it is?

Mainland answered 15/1, 2013 at 20:6 Comment(5)
Well... what do you define as "free RAM"?!Consuela
If Linux keeps some stuff in RAM even if it doesn't have to (it can throw them away at any time), is that RAM free or used ?Alloplasm
@KerrekSB Well how does htop define "free ram"? And about Linux keeping stuff in memory, I would say... free?Mainland
Linux uses a lot of the free memory as caches for buffers - and releases automatically that buffer allocated memory when application need some. See blog.scoutapp.com/articles/2010/10/06/… .Elizebethelizondo
@ring0 thanks that makes sense as usual with Linux. You should add it as an answer (that you add tbebuffers and cache to get total free memory)Mainland
E
1

Linux doesn't like when free memory is used nowhere. So when there is free memory available, it takes it temporarily as cache memory and buffers. The free memory seems very low, but as soon as a program requires memory, Linux reduces its cache / buffers usage and give the program what it wants.

The Linux command free -m displays the state of the memory, cache and buffers.

See this link for example and detailed information.

Essonite answered 16/1, 2013 at 8:36 Comment(1)
Just realised that sysinfo doesn't have cachedram, only bufferram. So I'll just use /proc/meminfo then.Mainland

© 2022 - 2024 — McMap. All rights reserved.