19

So I noticed this question has not been asked directly;

What is this "Swap Area" people keep referring to when it comes to installing Ubuntu?

Pandya
  • 35,771
  • 44
  • 128
  • 188
Anon
  • 12,063
  • 23
  • 70
  • 124

3 Answers3

31

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

TheSchwa
  • 3,820
  • You can either make a separate partition for your Swap Area (recommended) - why so? Works great for me. – jcora Aug 09 '14 at 10:46
  • The default installer on Ubuntu 14.04 in fact creates a 16GB swap partition if you have 16GB RAM. So your recommendation is already the default now. – jmiserez Aug 09 '14 at 13:54
  • 1
    @jco In the case that the file is not fragmented, it will have identical performance to a swap partition because the kernel bypasses the file system in recent versions. If you create the swap file at installation this is likely the case, but not guaranteed. If you change the swap file at a later date, it will likely fragment. Therefore it is easier to make a swap partition which is guaranteed to be contiguous on disk. Also, a partition is recommended if you want to try and hibernate. – TheSchwa Aug 09 '14 at 17:30
  • @JeremieMiserez Thanks, I didn't realize that as I always partition manually when installing Ubuntu. Updated wording in answer to reflect that. – TheSchwa Aug 09 '14 at 17:31
  • @TheSchwa Thanks for the info. I have an encrypted swap file that I created after installation. I chose it because it seemed much more practical, because I didn't want to clock up my precious SSD space with 20GB that would mostly go unused, and because I would be getting more RAM and planned on increasing the swap size. Is there a way to make sure it doesn't fragment? – jcora Aug 11 '14 at 08:11
  • @jco Actually if the swap file is on an SSD fragmentation doesn't matter. On a HDD if a file is fragmented (broken up into several pieces and scattered around the disk) the read head has to physically move to get to all the pieces which slows down performance. On a SSD fragmentation shouldn't be an issue, as every memory cell has the same random access time. – TheSchwa Aug 11 '14 at 08:29
  • @TheSchwa Well, at the moment it's on the SSD. I thought ext4 was not supposed to fragment significantly? – jcora Aug 11 '14 at 08:34
  • @jco In general you're correct, but the possibility of fragmentation increases the larger the file, more full the disk, and more writes that have been done. Since the swap file is at least a few GB it's much more likely to fragment than other files. You can mitigate this by creating the swap file at installation time (disk almost empty). Fragmentation would also be more likely if you make the swap file bigger after using the system for a while (disk has fewer contiguous runs). Honestly (just instinct here, no evidence) I doubt you'll have any significant problems with a swap file on a HDD. – TheSchwa Aug 11 '14 at 08:44
4

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.

Pandya
  • 35,771
  • 44
  • 128
  • 188
0

"Swap Area" is the same as the pagefile.sys used for paging in the Windows NT family.

  • -1 for assuming that OP knows what a page file is in Windows. Also, not all pages end up in swap space when they are flushed out of main memory. The two are related but there are other paging mechanisms than swapping. – David Foerster Aug 06 '17 at 11:22