Following C program helps you to identify what is the total and free memory / RAM available in your Linux machine.
$ vim checkfreemem.c
#include <stdio.h>
#include <sys/sysinfo.h>
void printmemsize(char *str, unsigned long ramsize) {
printf("%s: %ld in bytes / %ld in KB / %ld in MB / %ld in GB\n",str, ramsize, ramsize/1024, (ramsize/1024)/1024, ((ramsize/1024)/1024)/1024);
}
int main(int argc, char **argv) {
struct sysinfo info;
sysinfo(&info);
printf("uptime: %ld\n", info.uptime);
// print total ram size
printmemsize("totalram", info.totalram);
printmemsize("freeram", info.freeram);
printmemsize("sharedram", info.sharedram);
printmemsize("bufferram", info.bufferram);
printmemsize("freeswap", info.freeswap);
printf("current running processes: %d\n", info.procs);
return 0;
}
$ gcc -o checkfreemem checkfreemem.c
$ ./checkfreemem
uptime: 53186
totalram: 8206098432 in bytes / 8013768 in KB / 7825 in MB / 7 in GB
freeram: 225759232 in bytes / 220468 in KB / 215 in MB / 0 in GB
sharedram: 639688704 in bytes / 624696 in KB / 610 in MB / 0 in GB
bufferram: 3016544256 in bytes / 2945844 in KB / 2876 in MB / 2 in GB
freeswap: 2036477952 in bytes / 1988748 in KB / 1942 in MB / 1 in GB
current running processes: 824
As you can see, our program displays total RAM, Free RAM and other details.
This you can corelate with Linux “free” command as,
$ free
total used free shared buff/cache available
Mem: 8013768 1481188 891768 545628 5640812 5647400
Swap: 2097148 128368 1968780
We can check this numbers in more easier way as,
$ free -h
total used free shared buff/cache available
Mem: 7.6G 1.4G 898M 507M 5.4G 5.4G
Swap: 2.0G 125M 1.9G