Managing Swap Memory in RHEL

Managing swap memory is very important for any Linux administrator. Though it cannot be a replacement for actual physical memory (RAM), but it can certainly help increasing performance, if properly created and tuned.

Swap space is a generic term for disk storage used to increase the amount of apparent memory available on the system. Under Linux, swap space is used to implement paging, a process whereby memory pages (a page is 4096 bytes on Intel systems; this value can differ on other architectures) are written out to disk when physical memory is low or pages are inactive for a certain period of time (can be tuned) and read back into physical memory when needed. The process by which paging works is rather involved, but it is optimized for certain cases. The virtual memory subsystem under Linux allows memory pages to be shared between running programs.

For example, if you have multiple copies of gedit running simultaneously, there is only one copy of the gedit code actually in memory. Also, text pages (those pages containing program code, not data) are usually read-only, and therefore not written to disk when swapped out. Those pages are instead freed directly from main memory and read from the original executable file when they are accessed again.

Of course, swap space cannot completely make up for a lack of physical RAM. Disk access is much slower than RAM access, by several orders of magnitude. Therefore, swap is useful primarily as a means to run a number of programs simultaneously that would not otherwise fit into physical RAM; if you are switching between these programs rapidly you’ll notice a lag as pages are swapped to and from disk.

So while managing swap memory in Linux, remember, Linux supports swap space in two forms: as a separate disk partition or a file somewhere on your existing Linux filesystems. You can have up to 16 swap areas. 

A point to note there when managing swap memory is, a swap partition can give better performance than swap file, because the disk blocks are guaranteed to be contiguous. In the case of a swap file, however, the disk blocks may be scattered around the filesystem, which can be a serious performance hit in some cases. Many people use a swap file when they must add additional swap space temporarily–for example, if the system is thrashing because of lack of physical RAM and swap. Swap files are a good way to add swap on demand.