943,697 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10110
  • C RSS
Sep 7th, 2006
-1

How to calculate total free memory

Expand Post »
  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
Similar Threads
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Sep 7th, 2006
0

Re: How to calculate total free memory

Assuming you mean MS-Windows operating system, call GlobalMemoryStatus.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 7th, 2006
0

Re: How to calculate total free memory

And here is an example C program (freeware). I have not run this but it looks promising.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Sep 8th, 2006
1

Re: How to calculate total free memory

It will be helpful if somebody explains how to calculate total free memory in linux OS.
Thanks
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Sep 8th, 2006
2

Re: How to calculate total free memory

  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. }
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Sep 11th, 2006
1

Re: How to calculate total free memory

  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.
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Mar 24th, 2010
-2
Re: How to calculate total free memory
#include <sys/resource.h>

then use the getrusage() function
Reputation Points: 5
Solved Threads: 0
Newbie Poster
wsgeek is offline Offline
1 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with a program
Next Thread in C Forum Timeline: Does any one knows CUDA?? If yes then please help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC