Skip to content

Instantly share code, notes, and snippets.

@raidzero
Created June 10, 2017 17:31
Show Gist options
  • Save raidzero/dad668a0019d2520925064671a13508b to your computer and use it in GitHub Desktop.
Save raidzero/dad668a0019d2520925064671a13508b to your computer and use it in GitHub Desktop.
memory.c
#include <stdio.h>
int main(void) {
FILE* fp = fopen("/proc/meminfo", "r");
if (fp != NULL) {
char buf[128];
int total, avail;
while (fgets(buf, 128, fp)) {
if (sscanf(buf, "MemTotal: %d", &total)) {
continue;
}
if (sscanf(buf, "MemAvailable: %d", &avail)) {
break;
}
}
fclose(fp);
float used = (total - avail) / (float) total * 100;
printf("%.0f%%\n", used);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment