How to determine free and used memory in Solaris

Have you ever needed a quick method of working out how much memory a Solaris system has or the currently used or the amount of free memory? This article provides a quick and dirty option of providing this info.

Memory

To determine the amount of memory installed, we can use the prtconf command. For example:

# prtconf | grep Memory
Memory size: 262144 Megabytes

From the above output, we see that we have 256GB memory installed.

Unused Memory

Using the sar command we can get the number of free memory pages. For example:

# sar -r 1 1
SunOS smurf 5.10 s10_51 sun4u    03/18/2004

08:16:18 freemem freeswap
08:16:23 6916895 89225128

From the above output, we see that we have 6916895 free memory pages. However, this isn't user friendly. We can determine the amount of free memory in GB using the following:

      freemem * pagesize / 1024 / 1024 / 1024

To determine the pagesize value, we simply use the pagesize command. For example:

# pagesize
8192

Note: On SPARC systems the page size is 8k, and for x86 systems the pagesize is 4k.

Using the values for freemem and pagesize, we can calculate the values as:

      6916895 * 8192 / 1024 / 1024 / 1024

Thus, we have 52.77GB of feee memory.

For example:

# echo "scale=2; 6916895*8192/1024/1024/1024" | bc -l 
52.77

So if we take 52.77GB freemmem from 256GB memory, we are currently using 203.23GB.

Scripting

Putting all this together in a script makes it easier for those who don't want to do the calculations themselves:

#!/usr/bin/ksh
#- simple script to calculate free/used system memory

sysMem=`prtconf | grep Memory | head -1 | awk '{printf("%.2f\n", $3 / 1024)}'`
pageSize=`pagesize | awk '{printf("%.2f\n", $1 / 1024)}'
sarMem`sar -r 1 1 | tail -1 | awk '{print $2}'`
freeMem=`echo $sarMem $pageSize | awk '{printf("%.2f\n", $1 * $2 / 1024 / 1024)}'`
usedMem=`echo $sysMem $freeMem | awk '{printf("%.2f\n", $1 - $2)}'`

echo "Avail Mem: $sysMem GB"
echo "Used Mem:  $usedMem GB"
echo "Free Mem:  $freeMem GB"

Sample output:

# ~/scripts/freemem.ksh
Avail Mem: 32.00 GB
Used Mem: 24.49 GB
Free Mem:  7.51 GB