Skip to content

Instantly share code, notes, and snippets.

@randhawp
Created June 3, 2020 17:21
Show Gist options
  • Save randhawp/f72032087566f815751472a913725208 to your computer and use it in GitHub Desktop.
Save randhawp/f72032087566f815751472a913725208 to your computer and use it in GitHub Desktop.
cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d' ' -f2
or
free -m | head -2 | tail -1 | tr -s ' ' | cut -d' ' -f4
------------------------------------------------------------------------
#include <iostream>
int check_free_ram(){
FILE *fp;
int status;
char result[24];
std::string cmd="free -m | head -2 | tail -1 | tr -s ' ' | cut -d' ' -f4";
fp = popen(cmd.c_str(), "r");
if (fp == NULL)
return -1;
while (fgets(result, PATH_MAX, fp) != NULL);
//printf("%s", result);
status = pclose(fp);
if (status == -1) {
return -1;
} else {
return atoi(result);
}
---------------------------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment