-1
Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)?
 
Thanks
Iqbal
Votes + Comments
Code Tags not used properly.
4
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by wsgeek
1

It will be helpful if somebody explains how to calculate total free memory in linux OS.
Thanks

2
cat /proc/meminfo

shows the current state of memory. The format varies somewhat with Linux distributions. My version of Linux has what you want in the 4 th column on the second line. So, you will need to get the awk statement working for you. Then put it in the C code.
You could also just read /proc/meminfo (read only) as a regular file and find the value using C only.
snippet:

#include <stdio.h>
void foo(void)
{
   char *cmd="awk '{ if(NR==2){print $4}}' /proc/meminfo";
   FILE *cmdfile=popen(cmd,"r");
   char result[256]={0x0};
   while(fgets(result,sizeof(result),cmdfile)!=NULL) 
   {
       printf("%s\n",result);
   }
   pclose(cmdfile);
}
1
cat /proc/meminfo

shows the current state of memory. The format varies somewhat with Linux distributions. My version of Linux has what you want in the 4 th column on the second line. So, you will need to get the awk statement working for you. Then put it in the C code.
You could also just read /proc/meminfo (read only) as a regular file and find the value using C only.
snippet:

#include <stdio.h>
void foo(void)
{
   char *cmd="awk '{ if(NR==2){print $4}}' /proc/meminfo";
   FILE *cmdfile=popen(cmd,"r");
   char result[256]={0x0};
   while(fgets(result,sizeof(result),cmdfile)!=NULL) 
   {
       printf("%s\n",result);
   }
   pclose(cmdfile);
}

Thank you very much Jim. It is really a great help.:)

-2

#include <sys/resource.h>

then use the getrusage() function

Votes + Comments
respond to 4-year-old dead thread. I doubt the op will give a shit about your answer.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.