How much memory do I have?

Here is a quick and easy way to find out how much RAM do you have. It should work on most Linux machines:

cat /proc/meminfo

The file /proc/meminfo contains all the information you may ever want to know about your RAM. In most cases we are interested in the line labeled MemTotal which of course shows you the total amount of memory on your system (usually in KB). In most cases it will show up as the first line of the output so we can easily pick it out using head:

cat /proc/meminfo | head -n 1

If you want just the numeric value, you can use awk to strip the text from the line:

cat /proc/meminfo | head -n 1 | awk '/[0-9]/ {print $2}'

Once we have the number, we can easily convert it into MB for added readability:

#!/bin/bash
mem=$(cat /proc/meminfo | head -n 1 | awk '/[0-9]/ {print $2}')
echo $[$mem/1024]MB

Note that bash only knows how do do integer division which means that this number is only an approximation. In most cases this is ok, as you don’t really care for the fractions of megabyte that might be hiding there. But if you want you can pipe the result into bc for floating point arithmetic operations:

echo $(echo $mem/1024 | bc -l)MB

Instead of using head you can simply grep for the values that you need:

cat /proc/meminfo | grep MemTotal
cat /proc/meminfo | grep MemFree
cat /proc/meminfo | grep SwapTotal
cat /proc/meminfo | grep SwapFree

Simply substitute the head statement in any of the examples above for an appropriate grep and you can get and manipulate a whole range of memory related values from within you bash scripts.

Update 02/20/2007 10:58:31 PM

Note that if you want to know exactly what kind of dimms are in your system you should use:

lshw -C memory

This command will give you detailed breakdown of memory installed on your system.

[tags]linux, memory, ram, proc, meminfo, bash, scripting[/tags]

This entry was posted in Uncategorized. Bookmark the permalink.



Leave a Reply

Your email address will not be published. Required fields are marked *