Tuesday, October 4, 2011

Available, used and free memory in Solaris

To get the amount of memory installed use prtdiag:

# prtconf | grep Memory
Memory size: 16384 Megabytes

The output shows ~16GB total memory. Then use sar to get the current free memory:

# sar -r 1 1

SunOS overmind 5.9 Generic_122300-13 sun4u    10/04/2011

09:13:23 freemem freeswap
09:13:24 1007947 31669782

In this case 1007947KB. At least we need the pagesize:

# pagesize

The pagesize in this example is the default pagesize of 8192 Bytes. Then we begin to calcualte all. First we will divide 8192 through 1024 to get the pagesize in KB. The result will be obviously 8, which will be multiplied with the free memory:

# echo 1012114*(8192/1024) | bc -l

The result is the above value in KB. To get the amount of free memory in GB we need to divide it by 1024 twice:

# echo 8096912/1024/1024 | bc -l

With ~16GB available memory and ~7.7GB free memory we can see that ~8.3 memory is used. Attached a small script that can do the job:

# vi /usr/local/sbin/

# Available memory
memory=`prtconf | grep Memory | head -1 | awk 'BEGIN {FS=" "} {print $3}'`
gb_memory=`echo "scale=2; $memory/1024" | bc -l`

# Free memory
kb_pagesize=`echo "scale=2; $pagesize/1024" | bc -l`
sar_freemem=`sar -r 1 1 | tail -1 | awk 'BEGIN {FS=" "} {print $2}'`
gb_freemem=`echo "scale=2; $kb_pagesize*$sar_freemem/1024/1024" | bc -l`

# Used Memory
gb_usedmem=`echo "scale=2; $gb_memory-$gb_freemem" | bc -l`

# Conclusion
echo "Avai Mem: $gb_memory GB"
echo "Free Mem: $gb_freemem GB"
echo "Used Mem: $gb_usedmem GB"

Save it and make it executable:

# chmod 755

A sample output may be like this:

# /usr/local/sbin/
Avai Mem: 32.00 GB
Free Mem: 7.51 GB
Used Mem: 24.49 GB

Update 11/22/2012: now it also should work for Solaris x86 (prtdiag vs. prtconf)