How does swap space work?

By Adrian Cockcroft, Unix Insider |  Operating Systems

Q: How much swap space should I allocate, and what is the best
configuration for good performance? Why don't
vmstat and
sar agree on the quantity of swap space?

-- swapless in Shawano

First, I had better explain what "swap space" is used for.
Encompassing RAM and the disk space dedicated to it, swap space
holds the virtual memory of the system.
Every program you run occupies a certain amount of
virtual memory. Once all your virtual memory has been allocated to specific
applications you
cannot start new programs and currently running programs may fail if
they try to grow. Physical memory is what you know as the RAM of the
system. If you use up all the RAM, your system may run more slowly, but
you can still start more programs because the swap space absorbs excess
data. The physical memory contains the
current "working set" of virtual memory -- this means the parts of your
applications that are actually running on the CPU.

You usually configure enough RAM to run your main application, or
the parts of it that are active at the same time. You may have a window
system with lots of other applications iconized or dormant in the
background. These dormant applications gradually get their RAM stolen
from them, and they migrate to the swap space on disk. You will notice
that when you open up a long-forgotten window it takes a while to
respond, and you may hear the disk heads rattling as it is read back
into RAM.

The important thing to realize about swap space is that it is the
combined total size of every program running and dormant on the system
that matters. When a system runs out of swap space it can be very
difficult to recover. Sometimes you find that there is insufficient
swap space left to login as root or run the commands needed to kill the
errant process that is consuming all the swap space.

There are two possible situations to consider. If you are prepared
to keep track of your swap space, and administer it regularly, then you
can run with "just enough" swap space. If you don't want the
hassle and can spare some disk space in return for an easier life, then
you should run with "lots" of swap space.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness