Image of the glider from the Game of Life by John Conway
Skip to content

How Much Swap?

I've been in the UNIX and GNU/Linux world since 1999. Back then, hard drives were barely passing double digits in GB, and RAM was PC100 speed at roughly 128 MB max. In fact, it wasn't uncommon for most systems to have 32 MB of RAM with an 8 GB hard drive. And we ran GNOME, which had barely released, and KDE on these machines!

However, when installing the operating system, is was a general rule of thumb that the size of your swap file should be 2x the amount of RAM. So for a 32 MB RAM system, this meant dedicating 64 MB of disk to swap. This wasn't a big deal back then. After all, hard drives were 8-10 GB in size. What's 64 MB?

Fast forward just a few years, and it wasn't long before this recommendation became unreasonable. After all, if you had 4 GB of RAM in your laptop (which I had in my Lenovo T61 for years) and a 100 GB hard drive, this meant dedicating 8 GB of hard drive space to swap- roughly 1/10 the drive size! So, the recommendation shifted a bit to 1.5x the amount of RAM, or 6 GB of swap in my T61.

Now look at to today with DDR3 RAM sizes and the popularity of SSDs. My server has 32 GB of RAM, and 2x60 GB SSD drives in a Linux software RAID mirror. So, given the recommendation of 2x the amount of RAM, this would mean 64 GB of swap on a 60 GB disk. Given the recommendation of 1.5x the amount of RAM, this would mean 48 GB of swap on a 60 GB disk. Something tells me we need to re-evaluate the recommendation.

When I started training for Guru Labs, I started re-evaluating the recommendation. In fact, I started debating if I even NEEDED swap. After all, swap is nothing more than a physical extension of the installed RAM on your system, it just resides on disk, which is slow. So, for my laptop, I didn't bother with it. Instead, I upgraded my RAM to 8 GB, which the board supported. I then started telling students my own personal recommendation on swap sizes:

Unless you have an application that has advanced swapping algorithms, and needs more swap, 1-2 GB of swap should be more than sufficient for most situations. Otherwise, install more RAM, and don't even bother with it at all.

Now, there are some caveats. If you would like to hibernate your system, you may need as much swap as you have RAM, seeing as though hibernation implementations typically flush all of the contents of RAM into the swap/paging file. This could be useful for laptops. Also, there are some software applications out there which can swap in and out very efficiently, leaving active RAM available for the running software. The Oracle database software comes to mind here.

Swap can be part of a healthy system, and having it installed isn't "a bad thing", unless the system is thrashing of course. But it also isn't required, which I think some people aren't aware. You don't NEED swap. It's just like the laundry basket in your house. Rather than leaving dirty clothes all over the house, you can put them neatly in one place, out of the way. But the laundry basket isn't required to do the laundry.

{ 12 } Comments

  1. Tim Gelter using Google Chrome 18.0.1025.166 on Android | October 18, 2012 at 8:57 am | Permalink

    https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/ch-swapspace.html

  2. Tim Gelter using Google Chrome 18.0.1025.166 on Android | October 18, 2012 at 8:59 am | Permalink

    I don't mean to disprove anything you are saying, just leaving a link to the reference documentation I always use in case it's useful to anyone.

  3. ssam using Firefox 16.0 on GNU/Linux | October 18, 2012 at 9:51 am | Permalink

    we have probably moved from an era when people commonly had not enough RAM, to one where most people do have enough RAM.

    for hibernating, you actually only need as much space as the RAM you are using. so if your typical workload uses 2GB of RAM, then you will need 2GB of swap to hibernate (even if you have 8GB of actual RAM).

    however if you have 2GB RAM and 2GB swap, and you are currently have 3GB of allocated RAM (not including disk caches), then you wont be able to hibernate.

    a sensible recommendation for a 'normal' user might be 2*RAM, unless thats bigger than 1% of hard disk, unless that's less than 2GB.

  4. Aaron Toponce using Google Chrome 21.0.1180.89 on GNU/Linux 64 bits | October 18, 2012 at 9:52 am | Permalink

    Tim- Thanks for the link. I still find the recommendation silly, IMO. If you need it, use it. If you don't need it, don't use it. Setting hard numbers larger than 1-2 GB is a waste of disk. Take a 96 GB RAM KVM hypervisor. I don't have a spare 98 GB of disk lying around that I can use for swap, and if managed correctly, I haven't seen KVM actively use more than 2 GB. These astronomical sizes blow my mind.

  5. Joseph Scott using Google Chrome 23.0.1271.26 on Mac OS | October 18, 2012 at 9:54 am | Permalink

    In additional to some special use cases you mentioned, the other thing I ran into was some Linux kernels that would do odd things if there was no swap set. Setting just a simple 1GB swap segment was sufficient to make it happy again. It has been a few years since I've tested that so newer kernels may deal with that setup in a more graceful way now.

  6. John Massaglia using Google Chrome 22.0.1229.94 on Windows 7 | October 18, 2012 at 10:42 am | Permalink

    Are there any guides for partition sizes for a desktop machine? I am in the habit of setting up /, /boot, /tmp, /var, and /home to be seperate volumes but I haven't seen any good guides for sizing these volumes for over a decade.

  7. Aaron Toponce using Google Chrome 21.0.1180.89 on GNU/Linux 64 bits | October 18, 2012 at 10:54 am | Permalink

    For a desktop machine, I have no need to contain data in separate partitions. It's not multiuser, and I don't dual boot, so I don't need to contain /boot and /home. I'm not running busy servers on my machine, so I don't need to contain logging in /var. I have yet to see /tmp use more than 2MB on average. So, for myself, on a desktop or laptop, I throw everything in /, and that's it. I don't need contained partitions personally. I don't use LVM either. One large encrypted / that has everything.

  8. Fitzcarraldo using Firefox 15.0.1 on Windows Vista | October 18, 2012 at 1:53 pm | Permalink

    Back in 2010 I did a little experiment to see what TuxOnIce does.

    Here were my RAM and swap sizes in bytes:

    # free -bt
    total used free shared buffers cached
    Mem: 4142669824 2399014912 1743654912 0 175312896 1153126400
    -/+ buffers/cache: 1070575616 3072094208
    Swap: 4293558272 0 4293558272
    Total: 8436228096 2399014912 6037213184

    My (2.6.36) kernel config was as follows w.r.t. LZO compression:

    $ cat /usr/src/linux/.config | grep LZO
    CONFIG_HAVE_KERNEL_LZO=y
    # CONFIG_KERNEL_LZO is not set <---- Kernel Compression Mode was selected to be LZMA rather than LZO
    CONFIG_RD_LZO=y
    CONFIG_JFFS2_LZO=y
    # CONFIG_JFFS2_CMODE_FAVOURLZO is not set
    CONFIG_UBIFS_FS_LZO=y
    CONFIG_SQUASHFS_LZO=y
    CONFIG_CRYPTO_LZO=m
    CONFIG_LZO_COMPRESS=m
    CONFIG_LZO_DECOMPRESS=y
    CONFIG_DECOMPRESS_LZO=y

    and TuxOnIce did not work when I tried to hibernate my laptop:

    Dec 29 12:01:06 meshedgedx kernel: [ 272.829994] TuxOnIce: Failed to initialise the lzo compression transform.
    Dec 29 12:01:06 meshedgedx kernel: [ 272.829995] Failed to initialise compression algorithm.
    Dec 29 12:01:06 meshedgedx kernel: [ 272.829996] Continuing without compressing the image.
    .
    .
    .
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908327] TuxOnIce debugging info:
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908330] - TuxOnIce core : 3.2-rc2
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908331] - Kernel Version : 2.6.36-sabayon
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908333] - Compiler vers. : 4.4
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908335] - Attempt number : 1
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908336] - Parameters : 32777 667648 0 0 0 5
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908338] - Overall expected compression percentage: 0.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908340] - Checksum method is 'md4'.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908344] 0 pages resaved in atomic copy.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908345] - Block I/O active.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908346] Used 0 pages from swap on /dev/sda5.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908347] - Max outstanding reads 1052. Max writes 20353.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908348] Memory_needed: 1024 x (4096 + 344 + 112) = 4661248 bytes.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908349] Free mem throttle point reached 0.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908350] - Swap Allocator enabled.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908351] Swap available for image: 1048232 pages.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908352] - File Allocator active.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908352] Storage available for image: 0 pages.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908353] - I/O speed: Write 54180 KB/s.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908354] - Extra pages : 5179 used/500.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908355] - Result : Hibernation was aborted.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908356] : Insufficient storage was available.
    Dec 29 12:01:06 meshedgedx kernel: [ 316.908357] : I/O errors were encountered.

    As you can see above, with LZO compression configured to be a module instead of in-kernel, TuxOnIce did not compress RAM, and the 4293558272 bytes of swap was insufficient for hibernation when I have 4142669824 bytes RAM (of which 2399014912 was in use). So I used the kernel configuration options specified in the Gentoo Wiki article on TuxOnIce, and rebuilt my kernel to perform in-kernel LZO compression:

    $ cat /usr/src/linux/.config | grep LZO
    CONFIG_HAVE_KERNEL_LZO=y
    # CONFIG_KERNEL_LZO is not set <---- I left Kernel Compression Mode as LZMA rather than LZO
    CONFIG_RD_LZO=y
    CONFIG_JFFS2_LZO=y
    # CONFIG_JFFS2_CMODE_FAVOURLZO is not set
    CONFIG_UBIFS_FS_LZO=y
    CONFIG_SQUASHFS_LZO=y
    CONFIG_CRYPTO_LZO=y
    CONFIG_LZO_COMPRESS=y
    CONFIG_LZO_DECOMPRESS=y
    CONFIG_DECOMPRESS_LZO=y

    and then the laptop hibernates successfully:

    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711101] TuxOnIce debugging info:
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711102] - TuxOnIce core : 3.2-rc2
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711103] - Kernel Version : 2.6.36-sabayon
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711103] - Compiler vers. : 4.4
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711104] - Attempt number : 1
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711104] - Parameters : 0 667648 0 0 0 5
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711105] - Overall expected compression percentage: 0.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711105] - Checksum method is 'md4'.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711106] 0 pages resaved in atomic copy.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711107] - Compressor is 'lzo'.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711107] Compressed 2180071424 bytes into 914681193 (58 percent compression).
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711108] - Block I/O active.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711108] Used 225238 pages from swap on /dev/sda5.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711109] - Max outstanding reads 1338. Max writes 10377.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711110] Memory_needed: 1024 x (4096 + 344 + 112) = 4661248 bytes.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711110] Free mem throttle point reached 0.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711111] - Swap Allocator enabled.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711112] Swap available for image: 1048232 pages.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711112] - File Allocator active.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711113] Storage available for image: 0 pages.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711113] - I/O speed: Write 130 MB/s, Read 153 MB/s.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711114] - Extra pages : 5214 used/500.
    Dec 29 15:22:45 meshedgedx kernel: [ 1205.711115] - Result : Succeeded.

    With in-kernel LZO compression enabled my hibernating laptop with 3950 KB RAM occupied 225238/1048232 pages of swap = 22 per cent of my laptop's 4094 KB swap. The amount of swap used for hibernation was 225238x4096/4142669824 = 23 per cent of my laptop's RAM.

    So, making the swap size the same as the RAM size is more than enough. There is certainly no need to make the swap size larger than the RAM size, and one could get away with making the swap size smaller than the RAM size.

  9. Pedro using Firefox 16.0 on GNU/Linux 64 bits | October 18, 2012 at 2:27 pm | Permalink

    Well, today is almost impossible to say how much swap a user must separate. I agree that for simple users, I mean, users that do not use the computer for heavy things, 1-2 GB of swap is more than enough
    for today's computers/laptops.

    However, some situations really needs more swap. I have a laptop with 8GB RAM (max possible) and 32 GB of swap and some times
    I have to use a usb flash drive to increse it.

    I work with numerical simulation and it is very demanding.

  10. einheit using Google Chrome 22.0.1229.94 on GNU/Linux | October 19, 2012 at 3:17 pm | Permalink

    I agree the old school rules about swap no longer apply. The real requirement for swap is the size of your working set minus the amount of physical memory, if the difference is > 0

    These days I tend to put a 4 GB swap partition on servers regardless of the amount of RAM. In the worst case, swap files could be added, but it's best to have enough RAM.

  11. analogtek using Firefox 16.0 on GNU/Linux | October 20, 2012 at 5:03 pm | Permalink

    This is a very good paper. But what of the "swappiness" factor and how it should be set.

  12. John using Firefox 16.0 on Windows XP | October 23, 2012 at 4:14 am | Permalink

    For desktops 1-2GB swap is fine, for laptops I do RAM + 10% to allow for hibernation and its overheads.

    As for general partitioning, I always strongly recommend a separate /home so that you can do a full OS reinstall without worrying about backup/restore of your user data, stops you filling / with user data and running out of system space, and it makes backing up just the home partition easier. I also do a separate /tmp as I have seen rouge processes fill up /tmp and I want to limit the damage that causes by not filling up /. For /tmp I set it to 5GB as DVD burning software tends to create temporary iso files in /tmp, but others may prefer to just reconfigure the burning software. Another reason for a separate /tmp is to make it a ram drive to improve speed if you have sufficient memory, but you can do that later without creating a partition first. That means you need to decide on a size for /, which I set to 30GB for my personal machines as I have a lot of dev files installed, but your average desktop probably only needs 10GB, or 20GB for future-proof.

Post a Comment

Your email is never published nor shared.

Switch to our mobile site