Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding amount of RAM using C++

How would i find out the amount of RAM and details about my system like CPU type, speed, amount of physical memory available. amount of stack and heap memory in RAM, number of processes running.

Also how to determine if there is any way to determin how long it takes your computer to execute an instruction, fetch a word from memory (with and without a cache miss), read consecutive words from disk, and seek to a new location on disk.


Edit: I want to accomplish this on my linux system using g++ compiler. are there any inbulit functions for this..? Also tell me if such things are possible on windows system.

I just got this question out of curiosity when I was learning some memory management stuff in c++. Please guide me through this step by step or may be online tutorials ll do great. Thanks.

like image 546
anurag-jain Avatar asked Oct 22 '10 21:10

anurag-jain


1 Answers

With Linux and GCC, you can use the sysconf function included using the <unistd.h> header.

There are various arguments you can pass to get hardware information. For example, to get the amount of physical RAM in your machine you would need to do:

sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);

See the man page for all possible usages.

You can get the maximum stack size of a process using the getrlimit system call along with the RLIMIT_STACK argument, included using the <sys/resource.h> header.

To find out how many processes are running on the current machine you can check the /proc directory. Each running process is represented as a file in this directory named by its process ID number.

like image 100
Charles Salvia Avatar answered Oct 06 '22 01:10

Charles Salvia