How to calculate total free memory

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

How to calculate total free memory

 
-1
  #1
Sep 7th, 2006
  1. Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)?
  2.  
  3. Thanks
  4. Iqbal
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to calculate total free memory

 
0
  #2
Sep 7th, 2006
Assuming you mean MS-Windows operating system, call GlobalMemoryStatus.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to calculate total free memory

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

Re: How to calculate total free memory

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

Re: How to calculate total free memory

 
2
  #5
Sep 8th, 2006
  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:
  1. #include <stdio.h>
  2. void foo(void)
  3. {
  4. char *cmd="awk '{ if(NR==2){print $4}}' /proc/meminfo";
  5. FILE *cmdfile=popen(cmd,"r");
  6. char result[256]={0x0};
  7. while(fgets(result,sizeof(result),cmdfile)!=NULL)
  8. {
  9. printf("%s\n",result);
  10. }
  11. pclose(cmdfile);
  12. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: How to calculate total free memory

 
1
  #6
Sep 11th, 2006
Originally Posted by jim mcnamara View Post
  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:
  1. #include <stdio.h>
  2. void foo(void)
  3. {
  4. char *cmd="awk '{ if(NR==2){print $4}}' /proc/meminfo";
  5. FILE *cmdfile=popen(cmd,"r");
  6. char result[256]={0x0};
  7. while(fgets(result,sizeof(result),cmdfile)!=NULL)
  8. {
  9. printf("%s\n",result);
  10. }
  11. pclose(cmdfile);
  12. }

Thank you very much Jim. It is really a great help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 4924 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC