Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
7  
Does this question help? – muru Aug 9 '14 at 3:08
5  
Related: SwapFaq – Eliah Kagan Aug 9 '14 at 3:09
    
Have you consider any answer to accept? – Pandya Sep 3 '14 at 17:49
1  
@Akiva Could you be a little more specific? Personally I'm not sure exactly how a visual aid would help in this case. – TheSchwa Jan 23 at 17:29
1  
@Akiva Please see my updated answer and let me know what you think. – TheSchwa Feb 17 at 20:59
up vote 17 down vote accepted

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

share|improve this answer
    
You can either make a separate partition for your Swap Area (recommended) - why so? Works great for me. – jcora Aug 9 '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 9 '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 9 '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 9 '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 8:11

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.

  • http://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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.