Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Upgrades Hardware

How Big Should My Swap Partition Be? 900

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?

This discussion has been archived. No new comments can be posted.

How Big Should My Swap Be?

Comments Filter:
  • by Britz ( 170620 ) on Wednesday October 01, 2008 @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, 2008 @06:49PM (#25226069)

    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 hpa ( 7948 ) on Wednesday October 01, 2008 @06:55PM (#25226133) Homepage

    I personally prefer to put /tmp on tmpfs, and combine with a large swap partition (much larger than 2x RAM). tmpfs is a lot faster than a regular filesystem *even if it has to hit disk*, simply because it doesn't have to care about consistency. If the machine goes down, the data disappears.

  • 0 MB (Score:3, Insightful)

    by timeOday ( 582209 ) on Wednesday October 01, 2008 @06:55PM (#25226139)
    I have run linux systems without swap for years without trouble, including the laptop I'm on right now.

    On a system with adequate RAM, the primary effect of swap is to make the system bog down before it crashes when a runaway process tries to allocate a huge amount of memory.

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

  • by cytg.net ( 912690 ) on Wednesday October 01, 2008 @07:07PM (#25226307)
    The real question is; is that 750GB drive really 20 times faster than the older 30GB one?
  • by thogard ( 43403 ) on Wednesday October 01, 2008 @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.

  • by Gewalt ( 1200451 ) on Wednesday October 01, 2008 @07:21PM (#25226465)

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

  • by eldavojohn ( 898314 ) * <eldavojohn@noSpAM.gmail.com> on Wednesday October 01, 2008 @07:27PM (#25226499) Journal

    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 TurboNed ( 1370389 ) on Wednesday October 01, 2008 @07:27PM (#25226505)
    But the question arises - how many people actually are going to make use of that memory dump? Considering that even most power users of Windows don't know what to do with a memory dump file, discarding it completely is the efficient and useful thing to do in most cases.
  • by kyz ( 225372 ) on Wednesday October 01, 2008 @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?)

  • by the unbeliever ( 201915 ) <chris+slashdot&atlgeek,com> on Wednesday October 01, 2008 @07:29PM (#25226535) Homepage

    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.

  • by kasperd ( 592156 ) on Wednesday October 01, 2008 @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.

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

    by Vrallis ( 33290 ) on Wednesday October 01, 2008 @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.

  • by FooBarWidget ( 556006 ) on Wednesday October 01, 2008 @07:38PM (#25226601)

    Yes. If an OS upgrade fails, then I can reinstall the OS (i.e. format the / partition) without having to move all my /home files to a backup drive first.

  • by Sj0 ( 472011 ) on Wednesday October 01, 2008 @07:39PM (#25226621) 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 PAjamian ( 679137 ) on Wednesday October 01, 2008 @07:54PM (#25226817)

    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.

  • by PAjamian ( 679137 ) on Wednesday October 01, 2008 @08:01PM (#25226897)

    I am thinking of reducing the amount of swap on my primary compute server -- the reason is simple: if the machine starts using appreciable amounts of swap, it becomes so slow, it is unusable. So, really, by reducing the swap, what I do is get the OOM killer to take action and kill some processes sooner. I may have an unusual situation that when my machine is out of memory, the cause is almost certainly due to a process that I want killed anyway.

    The problem with this is that there is no guarantee that the process(es) that the kernel kills are the ones that are causing the problem (basically put a process is killed when it requests a resource that is not available and that is not necessarily the process that is hogging the resource to begin with). A better approach is to limit the resources that a given process can use with utilities such as ulimit and similar so that it sees the resource as being unavailable sooner and is killed off instead of something else more critical.

  • by Anonymous Coward on Wednesday October 01, 2008 @08: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 lgw ( 121541 ) on Wednesday October 01, 2008 @08:36PM (#25227275) Journal

    I've run Windows with no swap file for many years, and never had any issue. I just don't see any need for a swap file on a desktop, and consumer OSs should really abandon the concept (there is apparantly some downside to not using a swap file in Windows, as it assumes in some places that it can allocate all physical memory and use the swap file to clean up).

    The kernel could be made more efficient, and *many* drivers made simpler, if the whole concept of "page fault" ceased to exist.

  • If its any help... (Score:2, Insightful)

    by segra ( 867730 ) on Wednesday October 01, 2008 @08:59PM (#25227539)
    Windows (XP/Vista atleast) can't be trusted with a swapfile, I noticed this the moment I put 2gig of ram in my system (years ago)... yet for some reason Windows still felt the need to page everything when only 500meg was in use. Now with 3gig and the problem hasn't changed, windows seemingly keeps as much ram free as possible, just incase it needs it... which it never does because its always paging everything :) Turn off your pagefile, reboot your pc and you'll notice an immediate difference in performance. as for FreeBSD, it doesn't appear to use it til its outta physical ram have a nice day
  • by kyz ( 225372 ) on Wednesday October 01, 2008 @09: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"

  • by Mr Z ( 6791 ) on Wednesday October 01, 2008 @09: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 siride ( 974284 ) on Wednesday October 01, 2008 @09:33PM (#25227851)

    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.

  • by Anonymous Coward on Wednesday October 01, 2008 @10:00PM (#25228071)

    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.

    Your post is being labeled "Informative." Do you have a reference for your statement that you need at least a 1xRAM swap file because Linux writes a complete copy of physical memory there?

    It doesn't really make sense for the OS to write to disk parts of physical memory holding read-only pages like the kernel, libc, your GNOME libraries, the Firefox executable, etc. These pages are never discarded (the Linux kernel isn't paged) or could just be reread from the original library or executable file already residing on disk.

  • by cookd ( 72933 ) <douglascook&juno,com> on Thursday October 02, 2008 @12:04AM (#25229009) Journal

    No. "Enough ram for file caching" is approximately infinite RAM, so the premise is flawed. If you did somehow have "enough" RAM that there was no noticable benefit from having more file cache, then adding a swap file would make no difference one way or another -- the system simply wouldn't ever swap anything out. (You might still see some of the swap file as "in use", but that's just the OS doing agressive optimization. The OS will copy data from RAM to disk during idle cycles. It does this just in case there's a sudden increase in memory requests. If data has already been sent to the swap file and the OS later decides to give the RAM to some other program, the other program doesn't have to wait for the RAM to be copied to disk since it's already there -- the memory just has to be zeroed out and it is ready to reassign to the new program. But if the data is needed before some other program needs the memory, the data is in RAM ready for use, and the copy on disk doesn't hurt anything.)

    In any case, it's generally best to let the OS pick what stays in RAM based on what is used most often. If you're using files more than apps, your file cache will grow at the expense of RAM reserved for apps. If you're using apps more than files, your file cache will shrink. Removing swap from the equation simply means that you've made it impossible for the OS to do the best job of optimizing your system's performance if it gets low on free RAM -- instead of keeping stuff in RAM based on what gets used most often, it has to keep stuff in RAM because it has nowhere else to put it.

  • by Mr Z ( 6791 ) on Thursday October 02, 2008 @12:25AM (#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 phtpht ( 1276828 ) on Thursday October 02, 2008 @01:24AM (#25229499)

    I have been this 'bold' for many years now. I configure zero swap on almost all modern computers that have >=2GB ram, knowing that there is no real reason for it. (Even in microsoft os despite countless 'are you REALLY sure?!?!' thingies.) And heck, one day I even stared at a photoshop message saying something about 'no swap found - are you nuts' - i mean, why should a userspace application care about swap? (I can understand that in truecrypt but photoshop?)

    Anyway, my reasoning behing why swap is useless (in Linux):
    1. I have enough RAM for everything these days.
    2. In the rare ocassion I need more memory I can always dd /dev/zero into as many swap file(s) as i desire and swapon, and dispose of them when I'm done.
    3. If I needed more RAM routinely, I'd buy it (and perhaps sacrifice one lunch :).
    4. Swapping out those mingettys for 100K of extra disk buffers is nice, but neglectable.
    5. When an application goes on a memory rampage, it gets OOMed without my hard drive going nuts first.

    Moreover, having no swap allows me to create diskless computers with everything on NFS. Also, I'm not sure how healthy it is to put swap on a SSD/flash (i know, vista boost, but we'll talk in 3 years when flash drives get massively boosted out of service).

    Finally, swap is really useful for one thing: hibernation. And that is only because it happens to be used as hibernation space. However, with tuxonice, you can as well use a separate file.

  • Re:Loopback Swap. (Score:3, Insightful)

    by peter ( 3389 ) on Thursday October 02, 2008 @02:16AM (#25229791) Homepage

    Why would you use loopback when Linux can use swap files directly. They don't have to be block devices.

    For dynamics swap creation, check out swapspace(8), or swapd(8). They have to fill a file with zeros right when the system is under memory pressure and swapping, though. I use swapspace(8), and I like it. A runaway process will make the system thrash even harder, but if something is growing slowly it's great.

    What would help them would be an API like XFS's reserve space ioctl (xfs_io resvspace ...), but that worked for making swap files. XFS reserved space is still marked a unwritten, so it reads as zero, not as the data that was on the disk before. (old xfs without unwritten extent support worked for this, but it's a nasty security hole to expose stale disk data to anyone. A new API could allow it only for root, or for someone who could read the blockdev the FS is on anyway.)

  • by Bert64 ( 520050 ) <bert AT slashdot DOT firenzee DOT com> on Thursday October 02, 2008 @06:44AM (#25230985) Homepage

    Enough ram for file caching = enough ram for all your apps plus a complete copy of all attached storage...
    I had a sun e45k with 14gb ram and a single 9gb drive attached, it was able to cache everything from the drive and still run all it's apps adequately.

  • Re:Here's how big (Score:4, Insightful)

    by swilver ( 617741 ) on Thursday October 02, 2008 @07:47AM (#25231223)

    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.

  • Re:Here's how big (Score:4, Insightful)

    by swilver ( 617741 ) on Thursday October 02, 2008 @08:02AM (#25231303)

    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.

  • by OneSmartFellow ( 716217 ) on Thursday October 02, 2008 @08:42AM (#25231585)
    Excuse my ignorance, but what's the point of putting swap into RAM. That seems completely self contradictory. The whole point of swap is to make up for the fact you don't actually have enough RAM.

    Does it really surprise you that suspend doesn't work. Where do you think suspend intends to store the current state of volatile memory ? It can't store it onto your file system (other than SWAP) for quite obvious reasons - unless it had its own partition it could use. (which is kind of wastefull when swap is supposed to be there)
  • by harry666t ( 1062422 ) <harry666t@DEBIANgmail.com minus distro> on Thursday October 02, 2008 @08:53AM (#25231685)

    Haha. But what happens if your OS absolutely has to swap a page out, and there's no swap file/partition? That's it - one of your apps will die a painful death. Here are your choices: let the SSD last (theoretically) a few months longer, or have your workflow disturbed every time a swapping should occur. A tip: starting the crashed app and getting your documents / web pages / whatever back will ALSO wear the SSD.

    And if you're extensively using much more than this 1GB on this small netbook, then you're probably using a wrong tool for the job anyway.

    OBTW, wear leveling anyone?

  • Why so Linux? (Score:3, Insightful)

    by MSTCrow5429 ( 642744 ) on Thursday October 02, 2008 @09:44AM (#25232181)
    Why are most of the answers about Linux? Do we know the uber-parent is running Linux? No. Should they have specified? Yes. However, as they didn't, each post that attempts to be useful should, at minimum, cover NT, Linux, and OS X.

"And remember: Evil will always prevail, because Good is dumb." -- Spaceballs

Working...