Interpreting Free
To see how much memory you are currently using, run free -m. It will provide output like:
           total   used  free   shared buffers cached
Mem:Â Â Â Â Â Â Â 90Â Â Â Â Â 85 Â Â Â Â Â 4Â Â Â Â Â 0 Â Â Â Â Â 3Â Â Â Â Â Â 34
-/+ buffers/cache:Â 46Â Â Â Â Â 43
Swap:Â Â Â Â Â Â 9Â Â Â Â Â Â Â 0 Â Â Â Â Â 9
The top row ‘used’ (85) value will almost always nearly match the top row mem value (90). Since Linux likes to use any spare memory to cache disk blocks (34).
The key figure to look at is the buffers/cache row used value (46). This is how much space your applications are currently using. For best performance, this number should be less than your total (90) memory. To prevent out of memory errors, it needs to be less than the total memory (90) and swap space (9).
Interpreting PS
If you want to see where all your memory is going, run ps aux. That will show the percentage of memory each process is using. You can use it to identify the top memory users (usually Apache, MySQL and Java processes).
For example in this output snippet:
USER PID %CPU %MEM VSZÂ Â Â Â RSSÂ Â TTYÂ Â STAT Â START TIME COMMAND
root 854 0.5Â 39.2 239372Â 36208 pts/0 SÂ Â Â Â 22:50 0:05 /usr/local/jdk/bi
n/java -Xms16m -Xmx64m -Djava.awt.headless=true -Djetty.home=/opt/jetty -cp /opt
/jetty/ext/ant.jar:/opt/jetty/ext/jasper-compiler.jar:/opt/jetty/ext/jasper-runt
ime.jar:/opt/jetty/ext/jcert.jar:/opt/jetty/ext/jmxri.jar:/opt/jetty/ext/jmxtool
We can see that java is using up 39.2% of the available memory.
Interpreting vmstat
vmstat helps you to see, among other things, if your server is swapping. Take a look at the following run of vmstat doing a one second refresh for two iterations.
# vmstat 1 2
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
0 0 0 39132 2416 804 15668 4 3 9 6 104 13 0 0 100
0 0 0 39132 2416 804 15668 0 0 0 0 53 8 0 0 100
0 0 0 39132 2416 804 15668 0 0 0 0 54 6 0 0 100
The first row shows your server averages. The si (swap in) and so (swap out) columns show if you have been swapping (i.e. needing to dip into ‘virtual’ memory) in order to run your server’s applications. The si/so numbers should be 0 (or close to it). Numbers in the hundreds or thousands indicate your server is swapping heavily. This consumes a lot of CPU and other server resources and you would get a very (!) significant benefit from adding more memory to your server.
Some other columns of interest: The r (runnable) b (blocked) and w (waiting) columns help see your server load. Waiting processes are swapped out. Blocked processes are typically waiting on I/O. The runnable column is the number of processes trying to something. These numbers combine to form the ‘load’ value on your server. Typically you want the load value to be one or less per CPU in your server.
The bi (bytes in) and bo (bytes out) column show disk I/O (including swapping memory to/from disk) on your server.
The us (user), sy (system) and id (idle) show the amount of CPU your server is using. The higher the idle value, the better.