Hi guys , i need to get the total virtual memory in ubuntu but i need to write a C++ code for that, any idea on how to go about doing it? any references? or website that i can refer to ?
Open /proc/meminfo for reading and look for the lines MemTotal and SwapTotal. Sum those up, and pronto! you have the total of virtual memory. Or you malloc() until it fails, although this probably won't give you a correct value and is generally considered rude.
But I don't think that either C or C++ has any function to give you that value, since memory is usually handled by the OS.
can we use ps u den we pipe into the c++
getting the Total Virtual Memory size occupied by all processes in unix by c++
any one can help?
First, please don't bump up your questions. If you need a fast answer, provide us with some more information on what you need and what you've tried so far.
Second, as I said, C/C++ has no knowledge of virtual memory, since that is handled by the OS. On Linux you can get all the information you need from /proc/meminfo (description here)
pludi gave a fully correct answer. There are other ways involving accessing kernel memory directly, but that is very advanced programming.
You can also call popen() with a command of "vmstat -s".
This is the output of vmstat -s, you want the stuff in red
$ vmstat -s
2096612 total memory
788440 used memory
0 active memory
0 inactive memory
1308172 free memory
0 buffer memory
0 swap cache
1572864 total swap
13312 used swap
1559552 free swap
17632780 non-nice user cpu ticks
0 nice user cpu ticks
18800467 system cpu ticks
1772277920 idle cpu ticks
0 IO-wait cpu ticks
0 IRQ cpu ticks
0 softirq cpu ticks
11051537 pages paged in
22309640 pages paged out
11051537 pages swapped in
345219 pages swapped out
481163032 interrupts
1720440275 CPU context switches
1240017420 boot time
0 forks
It is easier to read the /proc/meminfo "file"