Reputation: 24768
is there a way to find out the total amount of RAM my system has using C? I am on Ubuntu 12.04. I need to write an application in C that should ideally query the total amount of RAM during run time.
Upvotes: 5
Views: 138
Reputation: 103
you can use top command will show cpu usage memory usage ,etc
Upvotes: 0
Reputation: 359
Only way to get physical RAM available is by using BIOS calls if you don't want the OS to do it for you. All the information you need http://wiki.osdev.org/index.php?title=How_Do_I_Determine_The_Amount_Of_RAM&redirect=no
Upvotes: 1
Reputation: 2067
@abc This documentation can help you. http://valgrind.org/docs/manual/ms-manual.html
Upvotes: 1
Reputation: 20842
The cleanest way is to use procfs (see Dietrich's answer).
However, for more details about the hardware (RAM, CPU count, speed, model numbers, misc other devices) you can extract tons of info from dmesg:
dmesg | grep Memory
You can use the C stdlib popen() to read from dmesg if you have privs, and parse all sorts of info. I have used this for a monitoring system like Spong to extract as much information as possible about the node. You can even monitor it live for feedback from hardware / device commands you issue (dmesg | tail -f).
Keep in mind dmesg isn't always available, depending on privs.
Upvotes: 3
Reputation: 213338
On Linux, this is available from /proc/meminfo
. Example:
MemTotal: 16469432 kB MemFree: 792136 kB MemAvailable: 15201832 kB Buffers: 5806244 kB Cached: 8637760 kB ...
Just open this as an ordinary file, and parse the contents.
Upvotes: 5