I'm typing a shell script to find out the total physical memory in some RHEL linux boxes.
First of all I want to stress that I'm interested in the total physical memory recognized by kernel, not just the available memory. Therefore, please, avoid answers suggesting to read /proc/meminfo or to use the free, top or sar commands -- In all these cases, their "total memory" values mean "available memory" ones.
The first thought was to read the boot kernel messages:
Memory: 61861540k/63438844k available (2577k kernel code, 1042516k reserved, 1305k data, 212k init)
But in some linux boxes, due to the use of EMC2's PowerPath software and its flooding boot messages in the kernel startup, that useful boot kernel message is not available, not even in the /var/log/dmesg file.
The second option was the dmidecode command (I'm warned against the possible mismatch of kernel recognized RAM and real RAM due to the limitations of some older kernels and architectures). The option --memory simplifies the script but I realized that older releases of that command has no --memory option.
My last chance was the getconf command. It reports the memory page size, but not the total number of physical pages -- the _PHYS_PAGES system variable seems to be the available physical pages, not the total physical pages.
# getconf -a | grep PAGES PAGESIZE 4096 _AVPHYS_PAGES 1049978 _PHYS_PAGES 15466409
My question: Is there another way to get the total amount of physical memory, suitable to be parsed by a shell script?
Total online memory
Calculate the total online memory using the sys-fs.
Example output for 4 GB system:
Explanation
Number of bytes in a memory block (hex value). Using 0x in front of the value makes sure it's properly handled during the calculation.
Iterating over all available memory blocks to verify they are online and add the calculated block size to totalmem if they are.
You can change or remove this if you prefer another memory state.
This will provide you with the total memory in your system in human readable format and automatically scale to the appropriate unit ( e.g. bytes, KB, MB, or GB).
If you're interested in the physical RAM, use the command
dmidecode
. It gives you a lot more information than just that, but depending on your use case, you might also want to know if the 8G in the system come from 2x4GB sticks or 4x2GB sticks.Total memory in
Mb
:or:
These are the ways :
1. /proc/meminfo
You can write a code or script to parse it.
2. Use sysconf by using below macros
sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGESIZE);
3. By using sysinfo system call
int sysinfo(struct sysinfo *info);
struct sysinfo { .