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?

20 of 900 comments (clear)

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

  2. Forget the RAMx2 rule by jmorris42 · · Score: 4, Insightful

    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.

    --
    Democrat delenda est
  3. 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

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

  5. None may be a good option by thogard · · Score: 4, Insightful

    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.

  6. Re:What Has Changed? by Gewalt · · Score: 4, Insightful

    Why would I want that? If Windows crashes, I want it to restart and quickly, not waste my time dumping memory.

    --
    Modding Trolls +1 inciteful since 1999
  7. 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.
  8. Re:What Has Changed? by kyz · · Score: 4, Insightful

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

    --
    Does my bum look big in this?
  9. Re:What Has Changed? by the+unbeliever · · Score: 3, Insightful

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

    I have customers who insist on windows servers. When they crash, the customer wants to know *why* -- dump files are handy in this case.

    For a home user, however, I see your point.

  10. Re:What Has Changed? by kasperd · · Score: 4, Insightful

    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.

    --

    Do you care about the security of your wireless mouse?
  11. Need More Info... by Vrallis · · Score: 4, Insightful

    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.

  12. Re:What Has Changed? by Sj0 · · Score: 4, Insightful

    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.

    --
    It's been a long time.
  13. Re:What Has Changed? by PAjamian · · Score: 3, Insightful

    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.

    --
    Windows is a bonfire, Linux is the sun. Linux only looks smaller if you lack perspective.
  14. 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

  15. Re:What Has Changed? by kyz · · Score: 4, Insightful

    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"

    --
    Does my bum look big in this?
  16. 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

  17. Re:What Has Changed? by siride · · Score: 3, Insightful

    Yes, let's go back to the days of overlays and manual management of transferring data to and from disks and other devices. That was so much simpler.

  18. Re:What Has Changed? by Mr+Z · · Score: 4, Insightful

    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.

  19. Re:Here's how big by swilver · · Score: 3, Insightful

    This formula is ridiculous and makes no sense at all. To determine if you need swap you are far better off just figuring out if your system has enough RAM to run the programs you want to use on a daily basis. If you have that, then there's no reason to ever use swap (as that was the original reason people needed swap in the first place). As an added bonus, systems without swap cannot swap out programs in favor of increasing the disk cache, keeping everything snappy even after days of not using certain programs. So, here's my formula:

    1) Donot turn on swap.
    2) If there's ever any problem with memory, create a swap file (if you don't have one yet) and type swapon on a live system.

    You can see when you have a memory problem by applications getting killed when they try to allocate large chunks of memory or by keeping an eye on a memory monitor. If you see most memory allocated to stuff that isn't the disk cache, then you'll need swap.

  20. Re:Here's how big by swilver · · Score: 3, Insightful

    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.

    Unfortunately, these "aggressive" memory managers are rather stupid. They will happily swap out every running program to increase the disk cache even in situations where caching makes no sense. Caching only makes sense if the underlying media is a bottleneck for performing a given task. How much of a bottleneck is your hard disk however when you are downloading and uploading files? When you're watching a movie? When you're playing MP3's? Or even, when you are serving web pages (over a link slower than your hard disk)?

    In none of those situations will you get ANY benefit at all from disk caching... yet if I watch a 4 GB movie over a period of 2 hours, a lot of memory managers will decide that attempting to cache all of that data might be a good thing. Halfway through the movie, it will think that all those other running programs have been unused for an hour and can be safely swapped out in favor of caching more of that 4 GB file. The end result is that half your programs are swapped out after watching a movie, resulting in a sluggish system that is trashing all over the swap file to restore some sanity to it, and all for caching data that put NO STRESS on the underlying media in the first place.

    The same thing happens for idling systems left on over night, doing simple tasks like virus scanning, downloading files, rebuilding indexes, and so on. The end result is that a system feels sluggish the next day, for no tangible performance benefit.

    Ask yourself, if I have 4 GB of RAM, and 500 MB worth of applications running, effectively having 3.5 GB for disk caching. How useful is it to swap out that extra 0.5 GB worth of kernel/programs for even more disk caching? Is 4 GB of disk cache so much more valuable than 3.5 GB? I highly doubt it, so to prevent stupid memory managers from swapping out my favourite programs which I left running for a reason, I just turn off swap.