| | |
How to calculate total free memory
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
C Syntax (Toggle Plain Text)
Can anybody please tell me how to calculate/find total remaining(unused/free) memory in the system(using any functions)? Thanks Iqbal
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
C Syntax (Toggle Plain Text)
cat /proc/meminfo
You could also just read /proc/meminfo (read only) as a regular file and find the value using C only.
snippet:
C Syntax (Toggle Plain Text)
#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); }
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
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.C Syntax (Toggle Plain Text)
cat /proc/meminfo
You could also just read /proc/meminfo (read only) as a regular file and find the value using C only.
snippet:
C Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Class Project
- Next Thread: Pointer tutor
Views: 4924 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






