Tuesday, May 26, 2009

Memory and Linux Swap Space

Memory and Linux Swap Space

August 5th, 2008 · by Xander Tan

Linux Virtual Memory

Operating systems divide memory (physical) into chunks of smaller memory called pages. Due to the limited size of memory, operating systems have to store these pages in memory and a secondary storage, i.e. hard disk. Swapping is the activity of transferring these pages in between memory and hard disk space. In Linux, this pre-configured hard disk space is called Linux swap space and the combined size of the memory and the swap space is the total amount of virtual memory.

When Linux runs multiple programs, of which total memory footprint is larger than the available memory, it uses this swapping mechanism. It temporarily stores chunks of memory into its swap space while loads the others into the memory and continuously swaps the chunks in the swap space with the ones in the memory. This means that all programs are loaded into the virtual memory and properly executed.

Linux Swap Space

Linux possesses two types of swap space, i.e. a swap partition and a swap file. A swap partition is an independent section of the hard disk used solely for swapping; no other files can reside there. While a swap file is a file that resides amongst system and data files. With the 2.6 Linux kernel, swap files are just as fast as swap partitions.

Linux virtually supports unlimited number of swap spaces, each of which can be assigned a priority. When it needs to swap pages out of memory, it uses the highest-priority free space; when it finds multiple spaces with the same priority, it uses them in a similar fashion as RAID0 (access them in parallel). In order to gain higher performance, priorities must be assigned effectively. For example, the fastest spaces possess the highest priority and swaps located on the same physical disk should not be used in parallel.

Advantages and Disadvantages

Advantages of swapping:

  1. It allows systems to execute multiple processes in virtual memory larger than available memory. Linux swaps pages in between memory and swap space. This virtually gives the operating systems a larger memory footprint than what it physically has.
  2. It increases the efficiency of memory utilization. Linux swaps out less used pages (in the memory) and allocates the freed memory to processes that need memory immediately. For example, Linux can swap out the pages (in the memory), which are used by a process during its initialization and then never used again, and allocate the freed memory for other processes.

Disadvantages of swapping:

  1. It requires a long time to perform swapping. Hard disk is much slower than memory; memory and hard disk speed is measured in nanoseconds and milliseconds respectively. This means accessing a hard disk is tens of thousands times slower than accessing physical memory; the more swapping occurs, the slower the system will be. In the case of trashing (excessive swapping), the system is struggling to free memory, only adding in more memory will help.

Memory and Swap Space Size

To see the amount of memory (physical) installed:

# grep MemTotal /proc/meminfo

The output will look something like this:

MemTotal:      1035776 kB

To see the amount of swap space configured:

# grep SwapTotal /proc/meminfo

The output will look something like this:

SwapTotal:      393552 kB

To see the utilization and availability of both memory and swap space:

# free

The output will look something like:

             total       used       free     shared    buffers     cached
Mem: 1035776 970104 65672 0 226544 618284
-/+ buffers/cache: 125276 910500
Swap: 393552 0 393552

To see more details about swap space configuration:

# swapon -s

The output will look something like this:

Filename                                Type            Size    Used    Priority
/dev/hda5 partition 393552 0 -1

The ‘Type’ field indicates that this is a partition swap space rather than a file. The ‘Filename’ shows that the configured partition is /dev/hda5. The ‘Size’ tells us the size of the partition in kilobytes. The ‘Used’ field shows that the swap space is currently unused. ‘Priority’ tells Linux which swap space to use first, of course, it doesn’t really matter since it has only one swap space.

Swap Space Optimization

Based on the discussion above regarding zero swap space utilization, Linux is able to run without swap space. Linux will keep going even though it runs out virtual memory, normally, its kernel OutOfMemory (OOM) will kick in and try to kill the “right process” and thereby freeing up memory. There are many guidelines recommending the size of swap space to be allocated for a certain memory size. For example:

RAM Swap Space
Between 1024 MB and 2048 MB 1.5 times the size of RAM
Between 2049 MB and 8192 MB Equal to the size of RAM
More than 8192 MB 0.75 times the size of RAM

And let us see the setting of my Linux:

             total       used       free     shared    buffers     cached
Mem: 1035776 970104 65672 0 226544 618284
-/+ buffers/cache: 125276 910500
Swap: 393552 0 393552

Memory (physical) is 1,035,776 kB (~1GB) and Swap Space is 393,552 kB (~400MB). In conclusion: hell yeah! The table can be a guide but it doesn’t have to be followed. Predict the Linux utilization and set the swap accordingly.

No comments:

Post a Comment