Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)?
 
Thanks
Iqbal
WolfPack commented: Code Tags not used properly. +0

Recommended Answers

All 6 Replies

And here is an example C program (freeware). I have not run this but it looks promising.

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

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);
}
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.:)

#include <sys/resource.h>

then use the getrusage() function

commented: respond to 4-year-old dead thread. I doubt the op will give a shit about your answer. -5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.