19

On a VMware ESX setup what is the difference of these options?:

  • a Linux VM with 1GB RAM and 1GB swap partition and the VM uses 1.5 GB ram
  • a Linux VM with 1GB RAM and no swap partition and the VM uses 1.5 GB ram

I mean, in both cases swap is being used;

  • in the first swap is done to the linux swap partition
  • in the second case, VMware will swap 512MB to the VMware storage pool.

So are there any point in giving Linux VM's a swap partition?

Sandra
  • 9,973
  • 37
  • 104
  • 160

6 Answers6

15

Ignoring the fact that people are dealing with OS specific reasons I have two reasons why it's a bad idea to not run with a swap partition/file.

  1. If you have 1.5 GB of RAM allocated to a VM with no space file/partition and it wants to use 1.5 GB + 1 MB it will report an out of memory error. With the swap space it will be able to swap data out of active memory and onto the disk.
  2. The guest OS does a much better job of memory management than the host. This is why technology like memory ballooning exists because the Host can make educated guesses on what memory isn't needed right now but the guest knows at a much more intelligent level (this keeps OS memory from being swapped out which could kill your performance).
Scott Keck-Warren
  • 1,670
  • 1
  • 14
  • 23
10

Yes. It is the Unix way.

Unix (even Linux) expects to be able to swap.
Bad Things happen when the system can't swap (either because it was misconfigured without a swap partition or because the swap space is full). In Linux one of these Bad Things is the Out Of Memory Killer, which will plunge a knife into the back of the program it thinks is using the most RAM (database servers are a favorite target).

voretaq7
  • 79,345
  • 17
  • 128
  • 213
4

What do you have /proc/sys/vm/overcommit_memory set to? From the kernel documentation:

0       -       Heuristic overcommit handling. Obvious overcommits of
                address space are refused. Used for a typical system. It
                ensures a seriously wild allocation fails while allowing
                overcommit to reduce swap usage.  root is allowed to
                allocate slightly more memory in this mode. This is the
                default.

1       -       Always overcommit. Appropriate for some scientific
                applications.

2       -       Don't overcommit. The total address space commit
                for the system is not permitted to exceed swap + a
                configurable percentage (default is 50) of physical RAM.
                Depending on the percentage you use, in most situations
                this means a process will not be killed while accessing
                pages but will receive errors on memory allocation as
                appropriate.

Thus if you are using 1 there is no difference. If you are using 2 and no linux swap file then no process will be able to allocate 512M of (virtual) memory. The outcome isn't clear for 0.

Edit: From http://utcc.utoronto.ca/~cks/space/blog/linux/LinuxVMOvercommit this is how 0 works:

Heuristic overcommit attempts to work out how much memory the system could give you if it reclaimed all the memory it could and no other process used more RAM than it currently is; if you are asking for more than this, your allocation is refused. In specific, the theoretical 'free memory' number is calculated by adding up free swap space, free RAM (less 1/32nd if you are not root), and all space used by the unified buffer cache and kernel data that is labeled as reclaimable (less some reserved pages).

So it uses swap in the calculation as well. In general I'd follow the RHEL recommendation of :

M = Amount of RAM in GB, and S = Amount of swap in GB, then
If M < 2
    S = M *2
Else
    S = M + 2
Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • 1
    `cat /proc/sys/vm/overcommit_memory` returns `0`. What do you recommend in terms of `overcommit_memory` value and swap/no swap? – Sandra Jan 18 '12 at 16:28
3

Swap partitions can be potentially faster, especially in situations where the root disk is nearly full and you can't create a swap file in one piece without fragmentation, let alone the overhead that might be created by the filesystem and, if applicable, things like LVM.

But the performance of both machines will suck anyway if you keep 33% of your memory requirements on the disk.

Sven
  • 97,248
  • 13
  • 177
  • 225
  • When you say "root disk". Do you mean on the ESX host, where ESX create its swap files? – Sandra Jan 18 '12 at 16:11
  • No, I mean the VM's system disk containing the root file system. After all, that's where swapping will occur in absence of a dedicated swap partition. – Sven Jan 18 '12 at 16:12
  • Note, this answer is debating between swap partition vs swap file. I thought the question was whether you needed swap at all. – user606723 Jan 18 '12 at 16:13
  • @user606723: If you don't use swap at all you will end up with an out of memory error if you try to allocate more memory than the VM has and the 1.5GB usage with 1GB physical situation cannot occur anyway. – Sven Jan 18 '12 at 16:15
  • @SvenW, Yes, but it's not a given that something like that is likely to happen. That's for the admin to decide. – user606723 Jan 18 '12 at 16:21
  • @SvenW I can't understand why that makes a difference. I mean, in the second case, VMware will just swap memory behind the VM's back, and I suppose if the VMware host have free physical memory it would use that instead of swapping to file? – Sandra Jan 18 '12 at 16:21
  • @Sandra, the difference is that in case(2), if you use more than 1GB, then you're stuck. The real question you seem to want to ask if it's better to have 1GBmem+1GBswap or 2GBmem that the VMhost will swap out on it's own. – user606723 Jan 18 '12 at 16:23
  • @sandra you're asking if swap on the HOST is necessary? – Bart Silverstrim Jan 18 '12 at 16:24
0

Please see the following link- https://help.ubuntu.com/community/SwapFaq

Basically, unless you need hibernate or using more memory than you're allocating to the VM, there is no significant advantage to a swap partition.

I haven't used a swap partition/file on any of my linux machines for years.

user606723
  • 544
  • 1
  • 4
  • 10
0

The obvious advantage for swap is that when your machine crashes you can still create a crash dump. Correct me if I'm wrong, but afaik this isn't possible without swap. This isn't VMWare specific of course but rather applies anywhere. I felt like it might be important to point out.

juwi
  • 573
  • 5
  • 14