Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

[ Create a new account ]

How Big Should My Swap Partition Be?

Journal written by theheadlessrabbit (1022587) and posted by timothy on Wednesday October 01, @06:39PM
from the wait-until-ram-is-infinite-then-double-it dept.

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?

hardware upgrades linux realmendontuseswap noswap
askslashdot upgrades
story

Related Stories

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • What Has Changed? (Score:5, Informative)

    by eldavojohn (898314) * <my/.username@@@gmail.com> on Wednesday October 01, @06:39PM (#25225907) Homepage Journal

    'Is 8 gigs of swap really necessary?'

    With a 750GB [newegg.com] 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 [wikipedia.org].

    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.

    • by meringuoid (568297) on Wednesday October 01, @06:49PM (#25226063)
      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.

      • by orzetto (545509) on Wednesday October 01, @07:25PM (#25226491)

        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 [ubuntu.com]:

        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.

      • by kasperd (592156) on Wednesday October 01, @07:31PM (#25226553) Homepage Journal

        I have an Eee 901. It has 1GiB of RAM and 20GB of disk space.

        I suppose that is not really a disk but rather flash storage. Swapping to flash is not the best idea as it could cause the flash to last shorter than it should. So I'd say this is probably one of those cases where no swap is the correct configuration.

    • by cytg.net (912690) on Wednesday October 01, @07:07PM (#25226307)
      The real question is; is that 750GB drive really 20 times faster than the older 30GB one?
      • by -kyz (225372) on Wednesday October 01, @07:28PM (#25226515) Homepage

        Swap space does improve performance. I have a lot of services loaded, ready for someone to use them, but they are rarely used. FTP server, file server, music server, web server, and so on. Most people have at least one little-used process running.

        With no swap, these never-running programs actively consume RAM and reduce the amount of RAM available to running programs and even disk cache.

        With swap, these sleepy daemons are paged out and not loaded again unless someone needs them. I get my RAM back for something I'm doing now.

        Yes, I could pare down my system so it doesn't load things unless absolutely needed, but why should I have to do that manually when I could just leave them running and have them consume zero RAM?

        As to "how big should swap be?", I prefer the Mac OS X solution - all free space on your drive is swap. Nothing is reserved, and you can make swap go away by completely filling your drive (but you wouldn't do that, would you?)

        • 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.

  • 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.

    • by pete-classic (75983) <hutnick@gmail.com> on Wednesday October 01, @06:55PM (#25226145) Homepage Journal

      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

      • Re:Oh, nonsense (Score:5, Interesting)

        by Obfuscant (592200) on Wednesday October 01, @07:27PM (#25226501)
        Here's my understanding of the "rule".

        In early Unixes (SunOS, e.g.), the memory manager was dumb and preallocated swap space sufficient to swap your entire process out if it became necessary, and it really did want contiguous. Running out of swap was common, even if it was really never used, and the "rule" to avoid that problem was 2xRAM. Further, if you had two swap partitions, or a partition and a file, your process stayed in whatever swap it started in and did not split across both. You could be out of swap space and still have a completely empty swap file.

        Memory managers have gotten smarter, mapping smarter, and now swap is only used when it really is necessary. Pages that are not dirty don't get swapped, they get reloaded from the disk they came from. Pages that are swapped are often used soon enough that they never leave the RAM buffers.

        Yesterday, I had a user come to me saying he was getting an "out of memory" error from Matlab. Matlab is notorious for not garbage collecting when it needs to. His Matlab process had 800Mb of resident memory, even though he said he had just 300Mb of data. The kicker? Somehow, over the last couple of years, the swap file I had created to extend the 512Mb swap partition had gotten lost. Dunno where it went, just not there. He had 512Mb of swap, and most of that wasn't being used. Never noticed it until yesterday. His 2Gb of RAM was sufficient for what he was doing.

        It's a case of people who learned early just doing what they know works, telling youngsters the "rule" so they do the same thing.

  • What Oracle Wants (Score:5, Informative)

    by stoolpigeon (454276) * <bittercode@gmail> on Wednesday October 01, @06:43PM (#25225967) Homepage Journal

    If you were running Oracle - here is what they recommend:
        RAM -> Swap Space

        1 GB - 2 GB -> 1.5 times the size of RAM
        2 GB - 8 GB -> Equal to the size of RAM
        more than 8GB -> 0.75 times the size of RAM

    I don't know if this would carry across to general computing - it seems to me if it's enough for an Oracle RDBMS server, it ought to do it for most things.

  • by Vexler (127353) on Wednesday October 01, @06:45PM (#25226003) Journal

    Reading through OpenBSD's FAQ:

    "The 'b' partition of your root drive automatically becomes your system swap partition. Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that's a LOT of swap, most people prefer that their systems never swap. You don't want your system to ever run out of RAM+swap, but you usually would rather have enough RAM in the system so it doesn't need to swap. If you are using a flash device for disk, you probably want no swap partition at all. Use what is appropriate for your needs. If you guess wrong, you can add another swap partition in /etc/fstab or swap to a file later."

    HTH.

  • Just test? (Score:5, Informative)

    by rasteri (634956) on Wednesday October 01, @06:45PM (#25226007) Journal
    Just make a note of your virtual memory use every hour or so (or just whenever you remember) for a few days/weeks. Then just give yourself maybe 2-3 times the peak usage.

    I imagine different people will need different amounts of swap space, so use a size that's right for you.
  • by bugg (65930) * on Wednesday October 01, @06:46PM (#25226019) Homepage

    If you're debugging your kernel or are helping people to debug your kernel, and are generating crashdumps either manually or as a result of kernel panic, you need your swap to be twice as big as the memory so it all fits comfortably (You can probably get away with X times bigger, where 1X2, but 2 is a safe number).

    To my understanding that's always been the reason for the rule of thumb about doubling the memory. If you can afford the disk, go for it, because you never know when you might hit a panic and need crashdumps. If you are in a live environment and are sure you will never, ever need or even want crash dumps, and the disk space is at a premium, you can size it based on need.

    Another thing to keep in mind is that as you have more ram, you have more pages, and the whole point of swap is to get pages to disk as well in case you need to free up physical ram quickly.

  • by Britz (170620) on Wednesday October 01, @06:48PM (#25226041)

    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 ...

  • by jmorris42 (1458) * <jmorris@@@beau...org> on Wednesday October 01, @06:49PM (#25226069) Homepage

    Forget the RAM X 2 rule. Capacity of drives are way up, base RAM load is way up. Drive transfer speed isn't up very much. Doesn't really matter how much ram you have, long before you get a Gig of swap utilized the system is going to be trashing to the point of being unusable under any but lab conditions.

    Running with no swap can cause some problems, because it does help if the system can push out blocks of memory that aren't backed by a file and also haven't been used for awhile. Still on an all flash system with an adequate amount of RAM running without swap is probably the right move. On a machine with a spinning disc give it a 1GB swap and forget it.

    The exception being in cases where the a system is doing suspend to disc into the swap. I don't have any Linux machines that will do suspend to disc so don't ask me about any details.

  • by rcoyner (1376393) on Wednesday October 01, @06:50PM (#25226077) Homepage
    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/ [bytetrap.com]
  • by thogard (43403) on Wednesday October 01, @07:19PM (#25226421) Homepage

    I've been setting up machines with no swap partitions for a few years. Swap partitions have a bad habit if collecting secure info you may have assumed was just in ram. All modern operating systems allow to you use a file or other blank space as swap means you don't need a dedicated partition. There is also the issues that if your starting to swap, where does it end? If your swapping on a machine with 4 or 8 gig of ram, will an extra gig help fix what ever is broken or just make the machine very slow until it gets around to telling the runaway program that there is no more memory. In the case of no swap, that tends to happen much faster. The only reason I see for swap partitions is that the OS will need a place to dump debug info if it crashes and the swap partition has traditionally been used for that.

  • Need More Info... (Score:5, Insightful)

    by Vrallis (33290) on Wednesday October 01, @07:35PM (#25226577) Homepage

    I also agree that the old "2 x RAM" standard is outdated.

    If you are a typical desktop user--browsing, email, games, etc, you will likely never swap. If you happen to edit photos a lot then you'll use a bit more. In these cases doing 4GB swap for 4GB RAM should be more than sufficient, and even then overkill.

    If you are a serous 'power' desktop user, heavy graphics / video editing or similar heavy-duty tasks, you will likely have significantly more RAM. If you ever did swap things would become so slow your productivity would be severely hampered.

    Were you talking about a server I'd say the same thing. Your swap space on an active server (thinking database or application server) is really just there to keep you operational should some process go haywire, long enough for you to fix it. If you are regularly swapping on a server then you need to upgrade your RAM or adjust your software on it.

    • Re:None (Score:5, Informative)

      by hey! (33014) on Wednesday October 01, @07:00PM (#25226211) Homepage Journal

      Well, I do occasionally need more than 2GB of RAM, without there being a memory leak. I've been running GIS programs, an IDE, a couple of RDBMSs, and then I fire up the old compression program...

      Which brings me to my point. The question "how much swap do I need" is probably meaningless, even for a given amount of memory. There are people who find 2GB with no swap fine, and others, like me, who probably could get by with 2GB of RAM and maybe 512MB of swap, and others who might need more.

      I think the 2x RAM rule of thumb has one virtue: excepting certain exotic kinds of systems, it's fairly safe that anybody who finds themselves needing more than that is probably feeling a world of pain that can only be fixed by getting more RAM. On the other hand, in most cases 2x RAM amounts to a trivial amount of disk. Probably most people could get by with 25% of RAM, but the value of thinking about whether that is true for you is very likely less than the cost of the disk space.

      Common sense applies. If you have some kind of scientific computing device with a gazillion bytes of RAM, your swap requirements might not be related to your maximum RAM requirements at all. If you're running some kind of operating system that launches a bunch of rarely used garbage, you probably ought to think about your swap. I had awful problems with Vista until I figured out the page file Windows created had something like eight thousand fragments. I was actually better off getting rid of the page file

        • Re:With a caveat... (Score:5, Interesting)

          by Gewalt (1200451) on Wednesday October 01, @07:30PM (#25226539)

          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.