Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

How Big Should My Swap Partition Be?

Journal written by theheadlessrabbit (1022587) and posted by timothy on Wed Oct 01, 2008 05:39 PM
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?

+ -
story

Related Stories

This discussion has been archived. No new comments can be posted.
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
Loading... please wait.
  • What Has Changed? (Score:5, Informative)

    by eldavojohn (898314) * <my/.username@@@gmail.com> on Wednesday October 01 2008, @05: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 2008, @05: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 RiotingPacifist (1228016) on Wednesday October 01 2008, @06:01PM (#25226229)

        consider using a swap file for your setup id recommend 256mb of swap, with 1.25gb of ram (apart from when i left wireshark running for toolong) ive not seen it creep above ~100mb for long.

          • Here's how big (Score:5, Interesting)

            by TheLink (130905) on Wednesday October 01 2008, @09:55PM (#25228491) Journal
            It does hurt to allocate a couple of gigs of swap.

            I use swap only to tell me that I'm low on RAM. Basically once the machine starts using swap and getting slightly slow- it means I'm low, then I can try to shut down stuff (without it behaving otherwise strangely, or dying abruptly).

            Here's how I suggest you figure out _roughly_ how much swap you need.

            1) Figure out the amount of Virtual Memory your programs and services _allocate_ without really _using_ - call this F. There are some programs that allocate hundreds of MB of memory but never use it. But note that there are some programs that allocate lots of memory and may use it :). If you have lots of RAM and are too lazy to guess, set F=0.

            2) Figure out your drive throughput for swap access (swap in + swap out)- this is often related to random access throughput - and for a typical hard drive it could be in the order of magnitude of 10MB/sec - call this M. Note that many flash drives have pathetic random write speeds of 4MB/sec (or even less!).

            3) Figure out the time you are willing to wait for stuff to swap in and out (e.g. time to get an ssh prompt- call this T.

            Swap = F + T * M.

            So for example, if you have programs that allocate a total of 100MB and never use it, and your drive swap throughput is 10MB/sec and the amount of time you're willing to wait is 15 seconds.

            Swap = 100MB + 15 sec * 10MB/sec = 250MB.

            As you can see allocating gigabytes can hurt - since it'll take days to swap in and out processes that are using gigabytes of swap. You'll run out of time before you run out of swap, and when that happens somebody will do a hard shutdown of the machine - and that means ALL processes will be abnormally terminated, rather than just one.

            Yes, there are cases where the offending program might not keep accessing all of that swap, but when a program misbehaves like that, you'd rather find out sooner rather than have to shutdown the whole computer (because it takes ages to respond).

            Running programs from swap is best reserved for those who wish to experience the 1950s drum memory days. If you want to do retrocomputing keep in mind that memory speeds are now much faster than disk speeds, whereas in the 1950s memory speed = drum speed, and most modern programs assume modern memory speeds.
            • Re:Here's how big (Score:5, Informative)

              by BitZtream (692029) on Thursday October 02 2008, @12:20AM (#25229465)

              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.

      • by orzetto (545509) on Wednesday October 01 2008, @06: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 Anonymous Coward on Wednesday October 01 2008, @07:15PM (#25227027)
          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
          • by Mr Z (6791) on Wednesday October 01 2008, @08:00PM (#25227551) Homepage Journal

            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

              • by Mr Z (6791) on Wednesday October 01 2008, @11:25PM (#25229123) Homepage Journal

                If the paging algorithm does its job well and the active working set stays stable in RAM, then the bulk of the writes to the swap file are for the dead weight inactive anonymous pages. Freeing up additional RAM for disk buffers could also prevent writes on other random files if they were short lived and deleted before ever getting written. This happens more often than you might think, and is one of the motivations (but not the only one) behind deferred allocation. (The other big one is multiple files opened for streaming writes in parallel.)

                So, like all things, it's a tradeoff. When you're on an SSD, if your working set fits in RAM and you don't really thrash, then by all means turn swap off. If you find yourself thrashing a little, do yourself a favor and make a small swapfile and see if that stabilizes things, since at least some of that additional activity will be writes that could go away if you had more RAM--may as well let the VM throw out some deadweight pages to make room for transient pages that might live and die in RAM. If you're oversubscribing your RAM such that you need a truly huge swapfile, consider getting more RAM, because you're likely punishing your SSD.

      • by kasperd (592156) on Wednesday October 01 2008, @06: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 TXG1112 (456055) on Wednesday October 01 2008, @07:31PM (#25227235) Homepage Journal

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

          • by timster (32400) on Wednesday October 01 2008, @08:56PM (#25228039)

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

            • by Mr Z (6791) on Wednesday October 01 2008, @08:25PM (#25227761) Homepage Journal

              It's base 2 when dealing with RAM and base 10 when dealing with disks. Pretty consistently. It's generally base 2 when dealing with throughput.

              Pop quiz:

              Throughput: How many bits per second peak can a 14.4kbps modem move? 1.544Mbps T1 line? 10Mbit Ethernet?

              Disks: How many bytes are on a 1.44MB floppy? A 2.88MB floppy? A 650MB CD-ROM?

              Answers:

              Throughput: 14,400. 1,544,000. 10,000,000. Hmmm... so much for base-2 throughput numbers. And yet, when you see the "kB/sec" rate in your browser download dialog, that is most likely in a 1024 byte/sec quantities.

              Disks: 1,473,560 (1440 * 1024, a mixture of base-10 and base-2), 2,949,120 (2880 * 1024, again a mixture), and 681,984,000 (purely base-2, derived from 333,000 sectors * 2048 bytes/sector / 1,048,576). And yet when you look at disk capacities from most computer software, it's reported as purely base-2 sizes.

              So, what's consistent about this again? RAM seems to be the only thing that gets it right most of the time, though I do remember seeing plenty of adverts for Commodore 64s that listed them with 65K of memory back in the day.

              And for the real brain bender: If we agree that bits should always use power-of-2 meanings and everything else should use power-of-10, what do we do when the two collide, such as when talking about areal density? (That's bits per square meter.)

              That said, whoever came up with the names gibibytes, mebibytes and kibibytes must have wanted us all to sound like we have a speech impediment or something, as the pronunciation for these sounds worse than baby talk. I'll stick to saying gigabytes, megabytes and kilobytes and their understood power-of-2 meanings where it makes sense, knowing full well that it has deep flaws. It's just an unfortunate circumstance, but most of the time it thankfully doesn't matter.

              --Joe

        • by piojo (995934) on Wednesday October 01 2008, @09:16PM (#25228193)

          Linux will use swap sometimes even if you don't fill up your RAM. It can swap out idle programs and use the recovered RAM for file caching which gives a performance boost to the file system.

          Conversely, if you have enough ram for file caching and running applications, then you will get a performance boost from disabling swap (because applications would be faster if they weren't ever swapped out).

    • by cytg.net (912690) on Wednesday October 01 2008, @06:07PM (#25226307)
      The real question is; is that 750GB drive really 20 times faster than the older 30GB one?
    • by Sj0 (472011) on Wednesday October 01 2008, @06:39PM (#25226621) Homepage Journal

      You present several arguements, but none of them are really very good.

      Honestly, why does the 2x RAM guideline make any sense? Why is it that when I upgrade my 1GB to 8GB, I suddenly need 16GB of swap space, even though my total RAM+SWAP was less than half of my current memory? That makes no sense. Why should I want to increase the amount of swap I'm using if I've never used half the RAM I've got in the life of the computer?

      How about we practice some Engineering? I know, it's slashdot, it's a tough thing to do, but bear with me.

      So you've got a computer, and you know what you do with it. Simply do what you'll do, and figure out the peak memory usage over a period of time. Add 50% or so to get a target memory value, and if your current memory exceeds that value and thus you've got more than enough to never have to hit swap, pick a small number like 256MB for your swap partition to satisfy applications which demand swap even when enough memory is available. If you don't have enough memory, then create a swap file to make up the memory shortfall.

      Seriously, some of the suggestions for swap are impractical. If you're using 24GB of memory in my hypothetical, and your hard drive can only transfer 16MB of memory at once, you're not using your computer anyway because it's too slow.

      • by RiotingPacifist (1228016) on Wednesday October 01 2008, @06:05PM (#25226283)

        i assume you dont hibernate, thats the only reason i have 1x my ram in swap. Although using something like "hibernate" instead of ubuntu's tool will happily compress it so i could really get away with about 60-70%

      • by setagllib (753300) on Wednesday October 01 2008, @06:17PM (#25226411)

        I hate it when that happens. A helpful popup told me I ran out of CPU cycles just a few days ago, and I had to order a whole bunch online. Cost thousands! Still waiting for them to arrive.

      • by -kyz (225372) on Wednesday October 01 2008, @06: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?)

          • by -kyz (225372) on Wednesday October 01 2008, @08:00PM (#25227549) Homepage

            Looking at my own OS X activity monitor:
            * 320MB free (i.e. in use as disk cache)
            * ~320MB wired
            * ~970MB active
            * ~400MB inactive
            * ~500MB swap used

            And it's not about applications launched later; it's about applications running now, and the files they're accessing now.

            What kind of OS would say "I could use more memory right now to give better disk caching... but fuck it, there's a service that hasn't been used in 6 weeks. Better let it keep that inactive program in memory and just keep reading the disk over and over again instead of caching it"

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

      • by kasperd (592156) on Wednesday October 01 2008, @06:47PM (#25226725) Homepage Journal

        what I do is get the OOM killer to take action and kill some processes sooner.

        Just killing processes more or less at random when the system runs low on memory is not a good idea. (I know it is not completely random, but there surely ain't any guarantee that it will make sane decisions). What you really want is for programs to get an out of memory error when trying to allocate memory, and then they can shut down as gracefully as possible. (Would be neat if the choice of who get the first ENOMEM was chosen by the heuristics that would otherwise kill the process, but I guess that has not been implemented). Guaranteeing that you will never need to kill a process because you are out of memory means the kernel will have to not commit to more than can be backed by memory and swap. However since actual usage tends to be somewhat lower than what is actually committed to, that would be a bit wasteful. This is the main reason why it makes sense to have a large swap partition that is mostly unused. Just so you have backing for the amount you need to commit to in order to optimally use the physically available memory.

        You typically wouldn't want to make use of most of that swap. So once any significant amount start getting used, you'd want to start giving ENOMEM errors. And that should help ensuring that the swap will only be used for a short time. There are a few pieces of data in virtual memory that are only used under very rare circumstances, and those it is nice to have on swap so they don't take up precious memory. So the aim is not to have zero swap in use, just some low number of pages that are really not needed in memory.

        Is there any kernel out there that gets all of this right? I don't know. But at least those I know about can be tweaked to do pretty well.

      • by rcw-home (122017) on Wednesday October 01 2008, @06:55PM (#25226829)

        I am thinking of reducing the amount of swap on my primary compute server

        For best performance, don't reduce your swap below the amount of RAM you have, unless you want to get rid of it entirely. The reason is that Linux 2.2.x and later will, when your disks are idle, preemptively copy your physical memory to swap - that way if you do run out of RAM, all Linux has to do is reuse that RAM for other things - your application's virtual memory has already been written out to disk. This can't work as well if the swap space isn't there for it.

        With 2.0.x and earlier, I would have recommended you pick the amount of virtual memory you think you need, subtract the amount of physical memory you have, and set up that much swap. With 2.2.x and later, I recommend you pick the amount of virtual memory you think you need, and set up that much swap.

        For what it's worth, Windows NT derivatives do the same thing.

        • by damn_registrars (1103043) on Thursday October 02 2008, @08:39AM (#25232131) Journal

          In that case, why can't I just let Windows XP or Vista manage the virtual memory size by itself? I don't see why I should need to establish a fixed size when Windows can manage it dynamically.

          I have yet to see a version of Microsoft windows that does not end up with a hopelessly fragmented swap file over time. And if you let windows dynamically use space for swap, you're just asking for an even more hopelessly fragmented drive as it starts grabbing space anywhere it can find some to expand the swap file.

          On my own windows installs I have brought some old-school unix (FreeBSD in particular) methodology to partitioning, and I make a partition just for swap (still 2x my total memory size). Of course in windows you still have to partition it, but I just don't write anything to it myself, and tell windows to only swap to that partition. Then my main partition doesn't end up as terribly fragmented.

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

    • Oh, nonsense (Score:4, Informative)

      by Anonymous Coward on Wednesday October 01 2008, @05:51PM (#25226097)

      2X RAM was the standard rule of thumb at Sun, for SunOS long before Windows was around.

      If anything, Microsoft ripped it off from Sun.

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

        by Obfuscant (592200) on Wednesday October 01 2008, @06: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.

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

          by Caduceus1 (178942) on Wednesday October 01 2008, @09:32PM (#25228317) Homepage

          Yes, I believe it was the BSD memory manager (possibly earlier, V7 maybe) that had the 2xRAM rule. Less and you could have issues - more was wasted disk space.

          BSD was the foundation for SunOS (pre-Solaris 2.X), Ultrix, etc. so they all inherited this requirement - and from there the "requirement" became gospel on other systems.

          I've actually never heard the 2xRAM in relation to Windows, but it certainly predates it. I was setting up systems with 2xRAM when Windows was a DOS app... :)

    • by pete-classic (75983) <hutnick@gmail.com> on Wednesday October 01 2008, @05: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

  • What Oracle Wants (Score:5, Informative)

    by stoolpigeon (454276) * <bittercode@gmail> on Wednesday October 01 2008, @05: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 2008, @05: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 2008, @05: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 2008, @05: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 2008, @05: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 2008, @05: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 2008, @05: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 Databass (254179) on Wednesday October 01 2008, @06:02PM (#25226257)

    Maybe we should be asking "should we even bother with swap files?". I took a class on that where we calculated the steps it takes to get the final memory address in a paged memory system. It was something like 36 steps per address! We had PDEs, PTEs, convert this, change that. I didn't grok all the steps, but I do know there were a lot of them. I know 36 steps per little itty bitty piece of memory is a lot, even if you are a very fast CPU, when you have to do this hundreds of millions of times.

    Back in the day, it made sense to convince your programs you had an extra 100 megs of RAM, because a lot of programs needed that and didn't have it in memory. Today, memory is more abundant than things we would really need it for at the non-industrial level. I don't personally have any non-industrial applications that will fill up 4 gigs of RAM. Even Vista + WoW won't take up all that.

    So, and my professor suggested this, maybe the ideal swap size is ZERO. What if your operating system just operated under the concept of "If you can't fit it in 4 gigs, tough. Just wait until memory is free. I'm not even going to bother to split memory into pages because I'm always going to use RAM, not a hard drive page. Case closed." We could save so much overhead and complexity if we just admit that we never need to pretend hard drive is RAM. With 4 gigs or more of RAM, why even have a glacial slow hard drive in the mix?

  • RAM-based hard drive (Score:4, Interesting)

    by suck_burners_rice (1258684) on Wednesday October 01 2008, @06:17PM (#25226403)
    I have 64 GB of RAM and my swap partition is the same size at 64 GB. I thought 128 GB is a little excessive. What I would really like is if someone would make up an IDE interface to RAM modules and build a large amount of such RAM into the form factor of a hard disk drive. Then, you could populate this RAM-based "hard drive" with the necessary data during startup, and use it for swap and for all of your system's various "temp" folders. This would make swapping (and temp stuff) extremely fast to access, and more importantly, it would eliminate the need to encrypt your swap and/or temp partitions, as the data would simply disappear when power is removed. So when the agents (including Agent Smith) come to bust down your door, all you do is pull the plug and voila! Your secrets are safe. :-)
  • by thogard (43403) on Wednesday October 01 2008, @06: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 2008, @06: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.

  • by Zoxed (676559) on Thursday October 02 2008, @02:32AM (#25230129) Homepage

    Why does everyone put their swap on a slow harddrive ? A Gentoo running mate of mine in the pub showed me how to map the swap file into RAM: runs much faster there.

    (Although suspend does not seem to work now :-(

    • Re:None (Score:4, Interesting)

      by compro01 (777531) on Wednesday October 01 2008, @05:54PM (#25226119)

      Delaying is largely the point as I see it. If you're out of ram and it's eating into the swap, things are going to slow to a crawl and you'll know something is wrong, so you can look for, find, and kill whatever is running amok before it consumes all and triggers a panic/BSOD/etc.

    • Re:None (Score:5, Informative)

      by hey! (33014) on Wednesday October 01 2008, @06: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 2008, @06: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.