Slashdot Mirror


How Big Should My Swap Partition Be?

For the last 10 years, I have been asking people more knowledgeable than I, "How big should my swap be?" and the answer has always been "Just set it to twice your RAM and forget about it." In the old days, it wasn't much to think about — 128 megs of RAM means 256 megs of swap. Now that I have 4 gigs of RAM in my laptop, I find myself wondering, "Is 8 gigs of swap really necessary?" How much swap does the average desktop user really need? Does the whole "twice your RAM" rule still apply? If so, for how much longer will it likely apply? Or will it always apply? Or have I been consistently misinformed over the last 10 years?

17 of 900 comments (clear)

  1. What Has Changed? by eldavojohn · · Score: 5, Informative

    'Is 8 gigs of swap really necessary?'

    With a 750GB hard drive selling under $100, what has changed?

    Yeah, your 256MB of space was trivial when you had a 30GB hard drive ... and 8GB of space is still trivial with a 750GB hard drive.

    That said, I'll forward you some common information on paging.

    Linux and other Unix-like operating systems use the term "swap" to describe both the act of moving memory pages between RAM and disk, and the region of a disk the pages are stored on. It is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions, although Red Hat recommends using a swap partition. The administrative flexibility of swap files outweighs that of partitions; since modern high capacity hard drives can remap physical sectors, no partition is guaranteed to be contiguous.

    I'm no expert but the short answer to this is to look at your swap partition as your extended virtual memory. By saying that your swap partition should be 2x your main memory is like saying that you will never use 3x of what your main memory is (in this case 12GB). While that rule of thumb is a good one, there may in fact be applications today in the graphics and processing world that require insane amounts of memory. While Firefox is probably never going to reach that critical mass (nor will most average programs) it's probable that a few years from now it will be common place. I know it's insane to think of but 'ought to be enough for anybody' is not the phrase you want to throw around in the digital information world.

    It's those days when I'm playing Warcraft through wine, listening to streaming radio through Amarok, have 20 windows open behind it, idling a LAMP server for my development projects, running a vent client, some form of news aggregater, pidgin & an e-mail client hooked up to several POP3/IMAP accounts that I am happy I erred on the side of a whole ton of swap space.

    --
    My work here is dung.
    1. Re:What Has Changed? by meringuoid · · Score: 5, Interesting
      Yeah, your 256MB of space was trivial when you had a 30GB hard drive ... and 8GB of space is still trivial with a 750GB hard drive.

      I have an Eee 901. It has 1GiB of RAM and 20GB of disk space. A swap partition on the 'twice your RAM' rule would be far from trivial.

      I decided to be bold and installed Hardy with no swap partition. It seems to work just fine so far; Firefox greys out for a few seconds sometimes while loading pages, which might have to do with my reckless configuration, but on the whole it's pretty snappy.

      As for my desktop PC, it has 4GiB of RAM. I followed the traditional rule when I installed on that. I don't think that swap partition has ever even been used.

      --
      Real Daleks don't climb stairs - they level the building.
    2. Re:What Has Changed? by cytg.net · · Score: 5, Insightful

      The real question is; is that 750GB drive really 20 times faster than the older 30GB one?

    3. Re:What Has Changed? by orzetto · · Score: 5, Informative

      I have an Eee 901 [...] I decided to be bold and installed Hardy with no swap partition.

      There are better reasons than boldness for not using swap on an Eee. They use solid-state drives (except some 1000-series models and the 904), which are faster than mechanical devices but can be rewritten fewer times. To make sure your drives last longer, do the following:

      1. Mount partitions with noatime, or relatime if you are using one of the very rare programs that use atime (mutt is the only one I know of);
      2. No swap partition, which would predictably have many more writes than the rest of the disk;
      3. Mount /tmp on tmpfs so that temporary files do not wear the disk.

      Sure, without swap and with tmpfs you will have less memory available, but I have an Eee 900A and I bought it as a presentation machine, possibly for some occasional work while travelling, not as a workhorse.

      --
      Victims of 9/11: <3000. Traffic in the US: >30,000/y
    4. Re:What Has Changed? by Anonymous Coward · · Score: 5, Funny

      "Some people actually want to debug something and find out *why* windows crashed."

      It crashed because they booted it. Next.

    5. Re:What Has Changed? by Anonymous Coward · · Score: 5, Insightful

      what on earth is the point? have you actually looked at how many rewrites the disks get, and how the wear levelling algorithms work? your Eee will be years old, and worthless, long before the disk runs out of write cycles

    6. Re:What Has Changed? by TXG1112 · · Score: 5, Informative

      Base 2. Storage numbers using base 10 are for disk manufacturers that are filthy liars.

      --
      I will not be pushed, filed, stamped, indexed, briefed, debriefed, or numbered. My life is my own.
    7. Re:What Has Changed? by Mr+Z · · Score: 5, Insightful

      Well, that doesn't mean it isn't swapping. If faced with memory pressure, the OS can throw away file backed pages instead, such as program executable pages, and then bring them in later. Those file backed pages will be scattered all around the partitions that hold your programs, though, not concentrated in the swap partition.

      It also means that buffered writes will need to be pushed to disk sooner too, which reduces your disk buffering for anything that writes a lot of data will impose more pain on your system.

      The bummer in all this is that you have nowhere to put anonymous pages. These are the pages associated with "malloc()" (or "new" if you prefer), as well as any other per-task writable structures such as the stack and global variables. These pages aren't backed by any file and could only go to swap. Without a swap file, they will always accumulate in RAM until unmapped, crowding out program pages and disk buffers. This includes pages that don't actually hold anything at the moment, but remain part of the process' malloc heap due to internal heap fragmentation.

      So, that's where the increasing thrashiness comes from on a swapless system. If you get under enough memory pressure from anonymous pages, then it's hard to keep enough program pages and disk buffers around to make real progress. And when you do need those other kinds of pages, they're spread all over the disk so you suffer from tons of seeking penalty, unless you're on an SSD.

      --Joe

    8. Re:What Has Changed? by timster · · Score: 5, Interesting

      Perhaps you'd like to tell us whether a GB is base 2 or base 10 then.

      You obviously aren't worth your SALT!

      Remember kids, it DEPENDS!

      Bandwidth? Base 10 -- always has been.
      ROM? Base 2 -- always has been -- and traditionally in bits, not bytes.
      RAM? Base 2, and bytes.
      Hard disk? Base 10 in the manufacturer's specs, base 2 in the OS display. Always has been that way, always will be.
      Floppies? Base 2 until you get to MB, where 1MB = 1000 base 2 KB (seriously).
      Clock speeds? Base 10, always has been, always will be.
      Flash? Who knows.

      Isn't it great that we have such an easy, convenient system that is focused around the needs of us humans, and not the needs of the computers (who don't care in the slightest).

      --
      I have seen the future, and it is inconvenient.
  2. Definitely not twice... by 42forty-two42 · · Score: 5, Informative

    The origin of the 'twice real RAM' came in the early days of windows, in which windows could not use any swap unless you had at least as much as real RAM. That's been gone for ages now - and you should actively avoid too much swap.

    If you allocate, say, 8G of swap for 4G of RAM, most of the time almost all of it will go unused. If it actually /is/ used, your machine has probably spent the past hour or so frantically swapping to try to accomidate this 12G request; ie, your system is completely unresponsive due to every program being mostly swapped out. The additional swap merely delays the out of memory event, and in the meantime you can't control the machine.

    Swap is still useful for holding data that's not part of the working set, in order to free memory for cache; but this shouldn't be very much RAM (256-512mb should be enough). It's also useful for software suspend on linux - if you have a laptop, make it a little bit larger than physical RAM. And always have /some/ - linux's memory manager doesn't like having none.

    1. Re:Definitely not twice... by pete-classic · · Score: 5, Insightful

      your system is completely unresponsive due to every program being mostly swapped out.

      Uh, report this to your vendor as a bug. No amount of swap space should cause your system's memory manager to make such lousy decisions.

      And, in fact, having an "unreasonable" amount of swap can actually pay off. If your system can swap out really stale memory to disk and use the RAM to cache stuff on disk that you might actually want, you're going to see a really big performance gain.

      -Peter

  3. For Suspend to Disk more than actual RAM by Britz · · Score: 5, Insightful

    Whatever you do, you need to remember to setup you swap partition to as large or bigger than your ram in order to be able to use the "suspend to disk" function in Linux. On older laptops suspend is sometimes handled by the bios. Then you need a special partition. But nowdays Linux just suspends to your swap. And if your memory was full ...

  4. It Depends, but at least as much as RAM by rcoyner · · Score: 5, Informative

    In the end, it depends. If you are running several memory intensive applications you're going to want more swap space. At the very least, you should have as much as your RAM because when you hibernate it takes all the pages in your RAM and puts it into the swap space before powering off your computer. I wrote about this a while ago: http://www.bytetrap.com/blog/2008/06/02/swap-space-linux/

  5. Re:separate partitions for / and /home by eldavojohn · · Score: 5, Insightful

    Is there any point to separate partitions for / and /home? I mean, if you were running different file systems on each of them I could see the point.

    I have gone through four different version of Linux on my laptop: mandrake/mandriva -> fedora -> knoppix -> ubuntu. Guess how many times I've thanked 8 lb 6 oz baby Jesus that I had the foresight to separate the two? All my data from my college days is still intact under /home.

    For this simple reason, I heavily recommend it.

    --
    My work here is dung.
  6. Re:Errr... check your math. by evilviper · · Score: 5, Funny

    8GB swap on a 120GB drive is 7%, not .07%. On a 200GB drive, it's 4%, not .04%, etc.

    It's "Verizon math"...

    Just my 0.02 cents worth.

    --
    Slashdot gets worse every day... Pipedot: News for nerds, without the corporate slant
  7. Re:With a caveat... by Gewalt · · Score: 5, Interesting

    Oh dear FSM, Please for the sake of everyones sanity, NEVER LET WINDOWS GROW THE SWAPFILE! besides the fact it it will fragment the pagefile, it will also completely lock up the computer for X amount of time... right when you need it most! ...it ALWAYS happens at a bad time.

    --
    Modding Trolls +1 inciteful since 1999
  8. Re:Here's how big by BitZtream · · Score: 5, Informative

    So I'm a FreeBSD guy rather than linux, but I'm going to assume that Linux also supports 'limits' that define the maximum a program can utilize before its denied access to more resources. You won't get a normal app on my FreeBSD boxes to use more than 256M of ram, they aren't allowed. There are 2 exceptions, the PostgreSQL server on one of the machines, and the bot that connects to that database. They both deal with large datasets on regular basis so they are allowed to use more ram. Now mind you, these machines are used for my personal development projects and they aren't really 'servers' in the sense that they see real load. My instances of apache don't NEED a lot of ram, some do.

    My point is that there are other protections in place that prevent an app from 'running away' and taking a properly configured machine down.

    Second, swap can be VERY useful even if you NEVER run out of ram. The OS can swap apps that have used memory but aren't actually doing anything with it out, and leave that memory available for file/disk caching, which can make performance FAR better than if you kept the idling apps in memory and had less available disk cache. Some apps avoid buffering things in memory because its both easier and most times more efficient to use the disk and let the OS manange the buffering. I've seen NT based OSes aggressively swap out things that aren't in use just so there is more memory available for disk cache, and it makes sense cause there is a lot of crap the kernel and other apps load up that is very RARELY needed, if ever.

    So while you can ( and did ) point out the potential pitfalls of using swap, your examples don't apply to any modern OS. I'm excluding Windows from that statement cause lets face it, its not exactly modern at the core. Modern kernels are FAR better at deciding what to swap than you are in almost every case, just like compilers can do a far better job of optimizing applications that most developers can. Yes some can do better, but its not likely you are, and certainly not the guy asking this question.

    In short, if you're going to try to get technical with why you wouldn't want to use swap, at least use examples problems that weren't solved years ago.

    And for reference, you configure your swap poorly if you do what you say.

    --
    Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager