One common question is, “How much memory is being used by my applications and various server, user, and system processes?” Or, “How much memory is free right now?” If the memory used by the running processes is more than the available RAM, the processes are moved to swap. So an ancillary question is, “How much swap is being used?”

The free command answers all those questions. What’s more, a very useful option, –m , shows free memory in megabytes:

# free -m
             total       used       free     shared    buffers     cached
Mem:          1772       1654        117          0         18        618
-/+ buffers/cache:       1017        754
Swap:         1983       1065        918

The above output shows that the system has 1,772 MB of RAM, of which 1,654 MB is being used, leaving 117 MB of free memory.  The second line shows the buffers and cache size changes in the physical memory. The third line shows swap utilization.

To show the same in kilobytes and gigabytes, replace the -m option with -k or -g respectively. You can get down to byte level as well, using the –b option.

# free -b
             total       used       free     shared    buffers     cached
Mem:    1858129920 1724039168  134090752          0   18640896  643194880
-/+ buffers/cache: 1062203392  795926528
Swap:   2080366592 1116721152  963645440

The –t option shows the total at the bottom of the output (sum of physical memory and swap):

# free -m -t
             total       used       free     shared    buffers     cached
Mem:          1772       1644        127          0         16        613
-/+ buffers/cache:       1014        757
Swap:         1983       1065        918
Total:        3756       2709       1046

Although free does not show the percentages, we can extract and format specific parts of the output to show used memory as a percentage of the total only:

# free -m | grep Mem | awk ‘{print ($3 / $2)*100}’

This comes handy in shell scripts where the specific numbers are important. For instance, you may want to trigger an alert when the percentage of free memory falls below a certain threshold.

Similarly, to find the percentage of swap used, you can issue:

free -m | grep -i Swap | awk ‘{print ($3 / $2)*100}’
You can use free to watch the memory load exerted by an application. For instance, check the free memory before starting the backup application and then check it immediately after starting. The difference could be attributed to the consumption by the backup application.

Usage for Oracle Users
So, how can you use this command to manage the Linux server running your Oracle environment? One of the most common causes of performance issues is the lack of memory, causing the system to “swap” memory areas into the disk temporarily. Some degree of swapping is probably inevitable but a lot of swapping is indicative of lack of free memory.

Instead, you can use free to get the free memory information now and follow it up with the sar command (shown later) to check the historical trend of the memory and swap consumption. If the swap usage is temporary, it’s probably a one-time spike; but if it’s a pronounced over a period of time, you should take notice. There are a few obvious and possible suspects of chronic memory overloads:

A large SGA that is more that memory available
Very large allocation on PGA
Some process with bugs that leaks memory
For the first case, you should make sure SGA is less that available memory. A general rule of thumb is to use about 40 percent of the physical memory for SGA, but of course you should define that parameter based on your specific situation. In the second case, you should try to reduce the large buffer allocation in queries. In the third case you should use the ps command (described in an earlier installment of this series) to identify the specific process that might be leaking memory.

(Extracted from oracle technet notes author Arup Nanda)