How can I find the total amount of RAM that my system uses C

Is there a way to find out the total amount of RAM that my system uses C with? I'm on Ubuntu 12.04. I need to write an application in C that should ideally query the total amount of RAM at runtime.

+5
source share
5 answers

On Linux, this is available from /proc/meminfo . Example:

  MemTotal: 16469432 kB
 MemFree: 792136 kB
 MemAvailable: 15201832 kB
 Buffers: 5806,244 kB
 Cached: 8637760 kB
 ...

Just open it as a regular file and parse the contents.

+5
source

The cleanest way is to use procfs (see Dietrich's answer).

However, for more detailed information about the equipment (RAM, number of processors, speed, model numbers, other devices), you can extract tons of information from dmesg:

 dmesg | grep Memory 

You can use C stdlib popen () to read from dmesg, if you have privileges, and parse all kinds of information. I used this for a monitoring system such as Spong to extract as much information as possible about the node. You can even track it in real time for feedback with your hardware / device commands (dmesg | tail -f).

Remember that dmesg is not always available, depending on privileges.

+3
source

@abc This documentation can help you. http://valgrind.org/docs/manual/ms-manual.html

+1
source

The only way to get available physical RAM is to use BIOS calls if you don't want the OS to do this for you. All the necessary information http://wiki.osdev.org/index.php?title=How_Do_I_Determine_The_Amount_Of_RAM&redirect=no

+1
source

you can use the top command, showing cpu processor memory usage, etc.

0
source

Source: https://habr.com/ru/post/1205005/


All Articles