User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,786 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,383 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2320 | Replies: 5
Reply
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Question How to calculate total free memory

  #1  
Sep 7th, 2006
  Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)?
 
Thanks
Iqbal
 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: How to calculate total free memory

  #2  
Sep 7th, 2006
Assuming you mean MS-Windows operating system, call GlobalMemoryStatus.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: How to calculate total free memory

  #3  
Sep 7th, 2006
And here is an example C program (freeware). I have not run this but it looks promising.
Reply With Quote  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: How to calculate total free memory

  #4  
Sep 8th, 2006
It will be helpful if somebody explains how to calculate total free memory in linux OS.
Thanks
Reply With Quote  
Join Date: May 2004
Posts: 177
Reputation: jim mcnamara is on a distinguished road 
Rep Power: 5
Solved Threads: 9
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: How to calculate total free memory

  #5  
Sep 8th, 2006
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);
}
Reply With Quote  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: How to calculate total free memory

  #6  
Sep 11th, 2006
Originally Posted by jim mcnamara View Post
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:17 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC