36

I have read this question, but it was asked in the context of servers not desktops or notebooks.

If a good amount of RAM is installed on a notebook, say 8 GB, do we still need a swap space?

32

There is a common misconception that having a swap space is negatively affecting system performance. This is incorrect. As long as you have enough RAM, having a swap area whatever its size doesn't hurt performance at all. What affects performance is being short in RAM and effectively using the swap space.

  1. If you have no swap space and happen to be out of RAM, the kernel will pick one or more processes which it thinks are good candidates (although you might give it some hints though) and kill them.

  2. If you have a swap space and are out of RAM, the kernel will pick less used memory pages and put them on the swap area to free RAM. This will slow down the system but your applications won't be affected otherwise.

I always prefer case 2. as I feel uncomfortable loosing parts or all of my work because the kernel thinks my applications are worth killing. Moreover, with the current size of an average disk being in the TB range, reserving a few percent for swap is usually not an issue.

  • Is your first point really true? The kernel will kill running processes to free up RAM? Why wouldn't the kernel just use disk space instead of killing live processes? – Donato May 10 '15 at 5:45
  • 4
    @Donato Yes, my first point is true. If there is no swap space configured (i.e. disk), the kernel is not given the choice to use disk space to free up RAM. – jlliagre May 10 '15 at 6:37
  • and what happens when I finish the swap space? Fall back in case 1? – Zac Jul 21 '16 at 9:43
  • @Zac Yes, unless you really disabled memory overcommitment in which case the first process failing to allocate memory will be hurt. Note that if you have a (significant) swap space area configured and happen to use most of it, you'll probably have serious performance issues in the first place. – jlliagre Jul 21 '16 at 10:05
  • 1
    @Zac If you exhaust your swap space, you are essentially in the same situation as having no swap space in the first place. One process or more will die. They might be chosen by the oom killer or just be at the wrong place and the wrong time. Normally, a properly monitored production system should have trigger an alert and then an admin reaction before swap exhaustion happen. – jlliagre Jul 28 '16 at 10:28
9

If you have a RAM of 3GB or higher, Ubuntu will automatically NOT USE the Swap space since it's more than enough for the OS.

Now do you really need a swap partition?

The answer in my opinion is yes. Because you don't know what will you be installing or doing in the future that might require the need of swap space. Also if you're planning to use hibernation, you might want to have some swap space. You actually don't have to have swap partition, but it is recommended in case you do use up that much memory in normal operation.

For more information on the need or not fro swap, see Ubuntu Swap Faq, or Swap Requirements

  • Do you have a link confirming your first statement? – jlliagre Feb 16 '15 at 7:24
  • @jlliagre Take a look at mygeekopinions.blogspot.com/2011/03/…, which leads to askubuntu.com/questions/23676/… – Mitch Feb 16 '15 at 7:53
  • 1
    The blog statement is misquoting the askunbutu reply. There is no evidence user4124 is an Ubuntu developer. "will (typically) not have a use" is quite different than "will automatically disable". Actually, Ubuntu is never disabling swap and will make use of it if it needs to, even if you have 8GB of RAM or more. – jlliagre Feb 17 '15 at 6:40
  • @jlliagre What that means is that the swap space is there, but not used. When I installed Xubuntu 14.04, it created a 16GB swap, and its never been used yet. see image – Mitch Feb 17 '15 at 7:23
  • 1
    Glad you corrected your reply. "Not use" is definitively not the same as "disable". Ubuntu doesn't disable the swap and will make use of it if ever needed, regardless of is size. – jlliagre Feb 17 '15 at 20:25
4

For the last 6 years or so I have used desktop systems with at least 8GB RAM. I don't remember a case when the system swapped out memory under normal usage - and I monitor resource usage quite often with htop. Not even in cases when I ran 4 virtual machines (servers) simultaneously for development purposes did swapping occur.

Having said that, if the applications you are running are memory intensive (eg video editing, image editing, large database table manipulation, etc) you may face a situation where your system might run out of memory. In that case it may be desirable to have a swap space. Think of it as a last resort safety net. And considering the multi-terrabyte capacity of today's systems, what's a couple of gigabytes in a swap partition worth? Better be safe than sorry :-)

2

Having swap space is always a good thing.

Here I'm quoting Michael Jang's RHCSA/RHCE Certification Study Guide

Linux Swap Space

Such space is used to extend the amount of effective RAM on a system, as virtual memory for currently running programs. But you can’t just buy extra RAM and eliminate swap space. Linux moves infrequently used programs and data to swap space even if you have gigabytes of RAM..

For systems of up to 2GB, the default swap space size is twice the amount of installed RAM. Above 2GB, it’s the amount of RAM + 2GB. But those are not “hard and fast” rules. Workstations with several GB of RAM frequently use very little swap space.. but it may be used more frequently on systems that aren’t rebooted for months at a time or have heavy demand from certain services.

  • 1
    "Linux moves infrequently used programs and data to swap space even if you have gigabytes of RAM". I assume here we are talking about code and data loaded in memory. In the case you have 8GB RAM and are just running regular apps, why on earth would any code or data page, be paged out to swap? – Tarik Aug 4 '16 at 17:41
1

It depends on what you are running on your system.

If you are creating content, it is probably a good idea to still have swap space allocated, so if you do run out of RAM, your processes just get slow instead of being killed and potentially losing work.

If your machine is just running programs, then you don't need or even want swap space. I design and build many kiosk-like display systems that fit this usage exactly. I'd rather a process die suddenly than be slow, because then I can detect that condition and correct it.

1

If you ever need swap or more swap space you can create swap files on your file system. Of course, they will not support hibernation but it is a reasonable approach for a laptop with SSD drive and a lot of RAM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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