Created
June 10, 2017 17:31
-
-
Save raidzero/dad668a0019d2520925064671a13508b to your computer and use it in GitHub Desktop.
memory.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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