Skip to content

Instantly share code, notes, and snippets.

@ajithbh
Last active March 19, 2025 08:39
Show Gist options
  • Save ajithbh/8154257 to your computer and use it in GitHub Desktop.
Save ajithbh/8154257 to your computer and use it in GitHub Desktop.
Available memory using C
void mem_avail(void)
{
char *cmd = "awk '{ if (NR == 2) { print $4 }}' /proc/meminfo";
FILE *cmdfile = popen(cmd, "r");
char result[256] = { 0 };
while (fgets(result, sizeof(result), cmdfile) != NULL) {
printf("%s\n", result);
}
pclose(cmdfile);
}
// Alternately for free memory and other info
//------------------------------------------
#inclde <sys/sysinfo.h>
int sysinfo(struct sysinfo *info);
struct sysinfo {
long uptime; /* Seconds since boot */
unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
unsigned long totalram; /* Total usable main memory size */
unsigned long freeram; /* Available memory size */
unsigned long sharedram; /* Amount of shared memory */
unsigned long bufferram; /* Memory used by buffers */
unsigned long totalswap; /* Total swap space size */
unsigned long freeswap; /* swap space still available */
unsigned short procs; /* Number of current processes */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding to 64 bytes */
};
unsigned long mem_avail()
{
struct sysinfo info;
if (sysinfo(&info) < 0)
return 0;
return info.freeram;
}
@mikkorantalainen
Copy link

mikkorantalainen commented Jul 20, 2022

A user can have 10000 GB RAM, in such case, even unsigned long long int will overflow.

10000 GB RAM would be "only" 10000 × 1024 × 1024 × 1024 = 1,07×10¹³ bytes but unsigned long long int can contain numbers up to 2^64-1 or about 1,84×10¹⁹ so that would definitely overflow long long int.

If you have more than 10000 EB RAM, then you would need to be more careful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment