13

I was wondering since I heard that swap is used when you're out or RAM and and swapping too much is bad, since I have a lot of RAM ( 8GB )....

what if I dont create a swap partition?

Also, do I need it for hibernation or it isn't a must?

3
  • you must create swap, i once tried not to create swap, couldn't proceed the installation
    – Lynob
    Sep 1, 2013 at 20:02
  • what happens when you install inside windows? I did that and it also seemed a bit slow for my rig compared to windows, but I didn't create the partition Sep 1, 2013 at 20:07
  • I never tried installing a Linux inside windows, I always create a separate partition
    – Lynob
    Sep 1, 2013 at 20:41

7 Answers 7

16

Modern operating systems require a swap space to make efficient use of RAM. Even if your system has plenty of RAM, wasting RAM results in a smaller buffer cache, which means increased disk I/O. So no matter how much RAM you have, you still want the system to use it efficiently. Using it efficiently means getting things out of RAM that are extremely unlikely to ever be accessed.

When you start up a typical system, a large number of services start up. Programs run initialization code and modify private memory mappings in the process. A number of these services will never run again. Many of them won't run for hours. Without swap, the OS has no choice but to keep the modified private memory mappings associated with those services in RAM forever. That's RAM that can never be used as disk cache.

So you want swap whether you need it or not.

4
  • 10
    "So you want swap whether you need it or not." -- Seems like you made the case that he needs swap whether he wants it or not.
    – sawdust
    Sep 2, 2013 at 0:50
  • 2
    Assume I have plenty of RAM and can afford wasting 2GB. Why would I need a swap file?
    – Tarik
    Aug 4, 2016 at 17:47
  • 1
    In that case, you don't need a swap file. But as I argued, you want one. Because whether or not you can afford to waste RAM, you shouldn't want to use RAM inefficiently when you don't have to. Aug 4, 2016 at 18:11
  • I'm running a CLI server with no swap and it runs perfectly. Running a teamspeak3 server, apache2, node.js I never see more than 500M of ram being used. So it doesn't bother me to use RAM "inefficiently" DISCLAIMER: The webserver function of this server is exclusively for development, so the load is always low anyway. And if I have a memory-leak during development I'd rather the OOM killer kill it as soon as I run out of ram than push all that nonsense to disk. Jun 9, 2017 at 14:43
15

I've been running a desktop system without any swap for a couple years, and it's been going great! There are a few behaviours that are different; some of these are advantageous and some might harm you. It all depends on what you're doing.

One major difference is how the system behaves when memory runs out:

If there is no swap partition, the OOM killer runs immediately. If you've got a program leaking memory, that's likely to be the one that gets killed. That happens and you recover the system nearly instantly.

If there is a swap partition, the kernel pushes the contents of memory into swap. Errant process can continue allocating memory. This keeps happening until swap runs out, which could be ten minutes to an hour if you have a big swap partition. While it's doing this, your system slows to a crawl. It becomes impossible to open a terminal and kill the process. I usually unplug the power cord when this happens.

So, since I'm going to lose my data anyways when the system OOMs, I prefer the former option to at least have a (high) chance of recovery.

2
  • 2
    This has been my experience too. Linux doesn't handle swap-thrashing well enough to allow you to kill the offending program yourself. It is more reliable to just have the OOM killer do it itself. Of course this is an awful design - a better one would be to freeze the offending process rather than kill it and provide a nice GUI to allow you to unfreeze or kill it, but... Linux.
    – Timmmm
    Apr 5, 2016 at 8:20
  • 4
    At last a response that makes sense and matches my own experience. I cannot comprehend the rationale behind "you must have a swap partition". Except if we want to hibernate the system.
    – Tarik
    Aug 4, 2016 at 17:49
5

Swap is required for hibernation, otherwise you can get by without it just fine.

3
  • Thus the size of the swap area should be a little over the size of the memory installed in your system.
    – mdpc
    Sep 1, 2013 at 21:16
  • 1
    @mdpc, not necessarily; it just has to be large enough to hold the used ram that can not be discarded ( i.e. data backed by a file on the disk ).
    – psusi
    Sep 1, 2013 at 21:24
  • and how would you estimate that on the creation? Wouldn't you so the proper planning and prepare for the worse case as partitioning is generally a pain to change after it is set?
    – mdpc
    Sep 1, 2013 at 22:07
3

It is a common misconception to think having a swap area negatively affects performance. What does severely affects performance is not having enough RAM. Swap area per se have no negative effect on performance unless you do not care about reliability. It can have a positive impact on performance even while you think you have enough RAM (and no RAM shortage is reported).

There are essentially three cases to consider:

  • 1: There is enough RAM for internal kernel needs, for all your applications to have their working set of pages on RAM and still have "free" RAM for the buffer cache to store most of the file system hot data.

  • 2: same as the above except you have not enough free RAM for the buffer cache to be fully efficient.

  • 3: There is not enough RAM to even store your applications used pages.

In case 1, which should be the norm for a production machine, having a swap area or not doesn't change anything (at least on Linux based OSes and other OSes that over-commit memory).

In case 2, having a swap area might improve the overall performance by allowing the system to page out very infrequently used pages and then allow the buffer cache to better play its role.

In case 3, having a swap area allows application to keep on running at the expense of a performance degradation due to pagination. On the other hand, missing a swap area (or a large enough one) will make applications crash randomly. Additionally, depending on OS settings, the OOM killer might also decide to kill the most memory hungry applications if there is a strong RAM demand without even giving them a chance to save their (your) critical data.

This latter case is one with which you have to make a choice. There are certainly some use cases where having processes being killed is the preferred option like systems where response time is crucial and where losing a transaction / process is no big deal.

However, I tend to believe in most situations, it is better to be able to let a chance for the user/administrator to notice the RAM shortage and act accordingly without risking losing data.

2

If you don't want to use a swap partition, you can use a swap file: a filesystem image stored on a regular file and used as your swap space.

The following articles explain how to do it in detail:

8
  • what is the difference between a swap file and a swap partition? and what would happen if I have none? Sep 1, 2013 at 20:33
  • speed of access....
    – mdpc
    Sep 1, 2013 at 20:38
  • 2
    If used YES it is quite significant. If you use a partition, it is simple addressing, if you use a file, you have to do all the filesystem overhead to use a swap file and you CANNOT guarantee where on the disk the segments will reside.
    – mdpc
    Sep 1, 2013 at 20:46
  • 2
    @mdpc File system overhead is negligible starting from kernel 2.6. Metadata, cache and file system code is bypassed. Raw disk blocks are simply directly accessed.
    – jlliagre
    Sep 1, 2013 at 22:22
  • 1
    @mdpc A swap file shouldn't be significantly fragmented if at all, unless you create it lately. Moreover, swap access is rarely sequential but mostly random due to how demand paging works so fragmentation shouldn't be that a problem.
    – jlliagre
    Sep 1, 2013 at 22:50
1

Linux swap seems to have quite a hard requirement as soon as there is memory pressure. The symptom is that if almost all RAM is used by active processes, the system stalls hard and the harddisk is running wild.

Why?

Pages without a backing file (usually from dynamic memory allocation) can't be evicted from RAM when there is no swap. The kernel resorts to pages with a backing file even if they are actually needed soon again, i. e. trashing even without swap!

For more details about this subtle problem see this blog post.

Conclusion: Always have a swap.

And for the size as a rough guideline use something between 4 and 8 GB, even for systems with a lot of RAM, assuming you don't hibernate. For more details see Red Hat's documentation about swap space.

0

It's a bit dangerous but you CAN run without a swap space. But if you finally exceed the amount of memory you have your system will nearly immediately crash without notice.

What swap gives you basically is an extended but far slower for your memory. If you exceed you will start swapping, it will really take the life out of your system....HOWEVER by killing off the errant processes you can still save your system.

Some programs insist on allocating (but not using) swap space, some of these are database programs if I recall. Again, by not having swap space you might limit the programs that may be run.

Finally, look SWAP is CHEAP in the fact that you do not have to backup the area. (I'm hoping that you are one of these fine outstanding sysadmins who backup their systems).

So create it.

My rule of thumb used to be 2*memory-size.....but now adays I am ok in many cases with 1*memory-size but generally use 1.5*(memory-size). You don't have to create this great of an amount, but I would...especially if there is any production type of work you are doing.

Oh yes, and for hibernation, swap is REQUIRED to hold a total image of memory. And thus the size if you are considering h8ibernation should be AT LEAST (1*memory-size)+100MB..the 100 MB is for possible overhead needed in the process.

3
  • so... for some programs you kind of need a swap area for them to run? I'm not worried of running out of ram with 8GB (though not impossible) Sep 1, 2013 at 20:53
  • The 2x memory size rule is old and outdated. But having some swap is not a bad idea. It does not use too much disk space, so you will not miss the space used; it is not usually used if you have enough RAM, and it allows you to dump core to your swap.
    – Hennes
    Sep 1, 2013 at 21:10
  • @Hennes -- read my note..."used to be"
    – mdpc
    Sep 1, 2013 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .