954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to calculate total free memory

Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)?
 
Thanks
Iqbal
Iqbal_h_a
Light Poster
30 posts since Aug 2006
Reputation Points: 51
Solved Threads: 1
 

Assuming you mean MS-Windows operating system, call GlobalMemoryStatus .

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Iqbal_h_a
Light Poster
30 posts since Aug 2006
Reputation Points: 51
Solved Threads: 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);
}
jim mcnamara
Junior Poster
180 posts since May 2004
Reputation Points: 62
Solved Threads: 10
 
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.:)

Iqbal_h_a
Light Poster
30 posts since Aug 2006
Reputation Points: 51
Solved Threads: 1
 

#include

then use the getrusage() function

wsgeek
Newbie Poster
1 post since Mar 2010
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You