What is a "Swap Area"?

Summary

The Swap Area is just fake RAM that lives on your hard drive. It is much slower than actual RAM, but is necessary in many cases to keep a computer running normally. A good rule of thumb is to have as much swap space as you do normal RAM.

Memory

Your computer has a couple different kinds of memory that we need to talk about. Your files and operating system are stored on your hard drive (commonly HDD). This is relatively slow and cheap, but is persistent between system shutdowns. Your computer also has RAM (Random Access Memory) which is much faster, more expensive, and loses its contents when your computer loses power. In order to run a program, it is first copied from your HDD to your RAM (which is much faster) so that it can execute in close to real time.

Swap

However, we should ask the question: what happens if you run out of RAM but want to open another program? The answer isn't good; your computer freezes until there is enough RAM to complete the operation (this is commonly called "thrashing"). Although you might only have a few Gigabytes of RAM, most computers have more HDD space than they know what to do with. So we can use some of that extra space as fake "RAM" in a pinch. That is what swap space is; emergency RAM that lives on your hard drive.


Swap lives on your hard drive

Now that we have some swap space on our hard drive, running out of RAM isn't as big of a deal. Now when we run out and wish to open another program, the operating system looks through all of the open programs currently in RAM, and chooses one that hasn't been used for a while. It then copies that program out of RAM and stores it in swap space, freeing up room for the new program. If in the future we want to access the "swapped out" program, the operating system simply copies it back to RAM, potentially swapping out other programs in the process.

Partition vs. File

When creating a swap space, you have two choices. You can make a separate partition on your hard drive, which is recommended in Ubuntu and necessary if you wish to hibernate your system. Or, you can create a Swap File (called the Pagefile in Windows) on your hard drive. Technically a swap partition will out-perform a swap file, but the difference will likely be negligible. Also note that if you have multiple linux distributions on the same hard drive, they can share a single swap partition (swap partitions do not have a file system).

Guidelines

If you are installing Ubuntu for the first time, you might as well make a swap partition. This is the default in the Ubuntu installer, and also my personal recommendation. On the other hand, if you do not have any swap space and would like to add some after installation, it may be much easier and faster to use a swap file.

In terms of size, if you have no idea how big to make it, a good baseline (and the default in the Ubuntu installer) is to have the same amount of swap as you do RAM. That being said, this is only a very general guideline, and depends on how you use your system. There is nothing wrong with having no swap space. If you start running out of RAM, just make your swap space larger.

See also

  • Ubuntu help - Official swap FAQ
  • AskUbuntu - how much swap you need
  • ServerFault - performance of partition vs. file
  • The comments for technical details on partition vs. file

From Community Help:

  • Swap space is the area on a hard disk which is part of the Virtual Memory of your machine, which is a combination of accessible physical memory (RAM) and the swap space. Swap space temporarily holds memory pages that are inactive.
  • Swap space is used when your system decides that it needs physical memory for active processes and there is insufficient unused physical memory available. If the system happens to need more memory resources or space, inactive pages in physical memory are then moved to the swap space therefore freeing up that physical memory for other uses.
  • Note that the access time for swap is slower therefore do not consider it to be a complete replacement for the physical memory.
  • Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

Also visit:-

  • wikipedia paging:

    With a swap partition one can choose where on the disk it resides and place it where the disk throughput is highest. The administrative flexibility of swap files can outweigh the other advantages of swap partitions. For example, a swap file can be placed on any drive, can be set to any desired size, and can be added or changed as needed. A swap partition, however, is not as flexible as a file, as it cannot be changed without using tools to resize it, generally outside the operating system that uses the swap partition.

    Linux supports using a virtually unlimited number of swapping devices, each of which can be assigned a priority. When the operating system needs to swap pages out of physical memory, it uses the highest-priority device with free space.

  • https://stackoverflow.com/q/4970421 (Difference between virtual memory & swap area)

  • Linux.com - swap space

    Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

  • Importance of Swap Partition


Hope this helps to understand and get basic knowledge about swap.