52

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?

Zanna
  • 70,465
pram
  • 940

6 Answers6

48

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.

jlliagre
  • 5,833
  • 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 05:45
  • 7
    @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 06:37
  • and what happens when I finish the swap space? Fall back in case 1? – Zac Jul 21 '16 at 09:43
  • 1
    @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
    @jlliagre But in this case are we just delaying the problem? – Zac Jul 28 '16 at 07:06
  • @Zac What case and what problem? – jlliagre Jul 28 '16 at 09:09
  • @jlliagre Case: "when I finish the swap space"; problem: "the kernel will pick one or more processes and kill them" – Zac Jul 28 '16 at 09:42
  • 3
    @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
  • Then it seems like the more important action is to monitor when memory exceeds a threshold rather than relying on swap space (which is a bandaid on the problem of processes possibly randomly getting killed)? To me this sounds like a good argument to not use swap space (unless you really don't have memory for the programs you're running) – éclairevoyant Jul 03 '22 at 18:06
  • @éclairevoyant Not having swap space typically means wasting RAM. It is often pointless to keep in RAM the memory inactive programs have allocated but will never access again, even on desktops. RAM is better used as filesystem cache than to store dormant data. – jlliagre Jul 04 '22 at 09:13
  • Option 2 actually have made me lose entire works and servers. After disabling swap no more surprises. The perfect use case for swap is so rare that we would need a better scheduler able to realize when a memory abuse is not temporary. The odds are against having just a small and fast use of memory in excess. Normally if all the RAM was eaten, there is no reason to believe it will just stop around the corner. It is more likely to be indefinite, in size or duration. – dawid Jul 23 '22 at 02:57
14

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

Mitch
  • 107,631
  • Do you have a link confirming your first statement? – jlliagre Feb 16 '15 at 07:24
  • @jlliagre Take a look at http://mygeekopinions.blogspot.com/2011/03/do-you-really-need-swap-partition-if-so.html, which leads to http://askubuntu.com/questions/23676/change-swap-memory-size-after-adding-ram – Mitch Feb 16 '15 at 07:53
  • 3
    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 06: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 07: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
6

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 :-)

hmayag
  • 2,246
  • 5
  • 21
  • 23
4

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.

In my experience, most servers usually do not require that amount of swap space, though it never hurts to have more. For desktops, the above instructions make more sense, so to support hibernation and such.

Gayan Weerakutti
  • 3,770
  • 26
  • 38
  • 2
    "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 04 '16 at 17:41
  • 1
    @Tarik for one thing: to make more room in the block cache, so that programs which are running get better disk performance. – greggo Sep 20 '20 at 05:46
  • Always is a strong word – dawid Jul 23 '22 at 02:59
3

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.