8

I have a core 2 laptop with 4gb of ram. I use the machine for browsing, network stuff and simple programming. (I use this machine to host a small ftp and web server semi-occasionally.)

Would there be any benefit to having a swap partition? My understanding is that with that much memory, there would be no need to have a swap for my mediocre usage. In fact, the kernel will be forced to use my ram, which will be faster.

Is this correct?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Bill Gray
  • 1,295
  • 1
  • 11
  • 18
  • 1
    related question ... http://serverfault.com/questions/19007/how-big-should-be-the-swap-partition – tomjedrz Jun 09 '09 at 21:42
  • also related question http://serverfault.com/questions/5841/how-much-swap-space-on-a-high-memory-system – Avery Payne Jun 10 '09 at 06:15
  • Also related: http://serverfault.com/questions/352431/should-i-completely-turn-off-swap-for-linux-webserver/398224#398224 – chutz Nov 06 '12 at 20:47

8 Answers8

15

Do I need a swap partition?

You need a swap partition (not a swap file) to send your workstation to sleep.

How can I force the kernel not to use swap?

sysctl vm.swappiness=0

On another note: How is that related to server administration?

Martin M.
  • 6,428
  • 2
  • 24
  • 42
  • But as far as performance goes, it makes no difference? – Bill Gray Jun 09 '09 at 21:47
  • 2
    There might be a slight overhead when activating swap but nothing you could measure as long as you don't use the swap. No need to measure if you use it, since you can go off have a cup of coffee (or two). If you really start using it you'll notice :) – Martin M. Jun 09 '09 at 21:50
  • Sorry, I meant if I don't want to send my workstation to sleep, is there any benefit gained in having a swap? – Bill Gray Jun 09 '09 at 22:51
  • 2
    Not really, but given todays storage prices you'll probably want some swap anyway (just stay with the default of you distro install if you don't know how much). You'll save yourself some headache if at some point you are starting a huge SQL query (or something similiar) and run into the OOM killer just because of that. On the other hand for that purpose you could always simply use a swap file. – Martin M. Jun 10 '09 at 01:39
  • 2
    You mean hibernate, sleep is ACPI and doesn't touch the disk. – LapTop006 Jun 20 '09 at 10:47
  • I don't think setting swappiness to 0 forces the kernel to not swap, it just makes it less eager to. Swap (if available) will still be used when the memory pressure goes too high. – Jo Liss Jun 10 '11 at 18:29
  • 1
    You are right it won't to [quote LWN](http://lwn.net/Articles/83588/): `swap_tendency = mapped_ratio/2 + distress + vm_swappiness` - disabling swap is a lot easier by simply removing all swap space. I'm opinionated and unless you know what you're doing you should have a swap partition (I don't pretend to know so I have swap on all my machines set up, just in case) – Martin M. Jun 10 '11 at 18:35
4

There is benefit. Whether you need it is up for debate. At a minimum, swap gives you a safety net in case something happens that is unexpected. Your system will swap instead of starting to have programs crash or killed from OOM.

Kevin Kuphal
  • 9,064
  • 1
  • 34
  • 41
4

Suspend-to-disc also requires enough swap to save the contents of memory into (it might use compression depending on settings and version).

As it's a laptop, you may want to use suspend-to-disc.

MarkR
  • 2,898
  • 16
  • 13
2

No. If for some reason you later decide you have to have swap you can create a file and tell the OS to use that file on your file system for swap space.

Dustin
  • 199
  • 3
2

In fact, the kernel will be forced to use my ram, which will be faster. ?

Maybe, most likely no - modern operating systems are intelligent (well, sane) about using available RAM - even old ones are pretty good. On all the linux machines I've used, the swap count stays at 0 bytes until it's required - there's no reason the OS would use swap unless it's forced too..

The issue only arrises when you run out of physical memory..

  • Without any swap-space, the system tends to freeze for a few minutes, the OOM-killer (out-of-memory) kicks in and starts killing processes until there is enough free memory for the main bits of the system to function..
  • With swap-space, the application that's caused the swap'ing might slow down a bit (a good indication it's reaching the physical memory limitations), and.. nothing, it just carries on..
dbr
  • 1,538
  • 3
  • 16
  • 18
  • "there's no reason the OS would use swap unless it's forced too" Of course it does. It's called swapiness. OS simply swaps "dead" memory in anticipation of needing more disk cache. – kubanczyk Jan 03 '17 at 23:35
1

Think of an application's working set as comprising two parts: the data it is holding in its own address space and the data it is accessing from files. The first must be held in RAM or in swap for correctness, the second can be correctly retrieved from disk or fileserver and need only be cached in RAM for performance.

Apologies that I have no empirical evidence for this, but I have always assumed that there is a performance benefit from being able to page out data in the first category that's no longer part of the working set so that less data from the second category need be fetched from disk or network.

James
  • 607
  • 7
  • 14
0

In essentiality, what swap gives you is protection against burst memory consumption - if some application of yours decides to allocate too much memory for your system's good (memory consumption approaches total available memory), what you are going to get is termination of the said application or (best case) or termination of other applications (not good at all), either way, in 99% cases, the application will not get to do whatever it was going to do. With swap, there is a possibility, that the said application will do whatever it intended to do, release the memory and the normal operation will be resumed. With no swap there is no such possibility. Can you really not spare 4Gb of your hard drive for swap? We definitely DO have swap enabled on our systems even though they have at the very least 32Gb RAM.

Generally, what is your problem with swap? The only "real" downside I see is, that you are shaving some gigabytes off of your usable disk space, but thats all there is about it. Or am I missing something?

shylent
  • 792
  • 10
  • 22
0

I have an old server running with 4GB of RAM, and the swap on it is 4GB and a small amount of it gets used. Rarely, but it does get used. If you can't spare 4GB for the swap, I would upgrade the hard disk.

Edward_178118
  • 895
  • 4
  • 14
  • 30