Slashdot Mirror


Is Swap Necessary?

johnnyb writes "Kernel Trap has a great conversation on swap, whether it's necessary, why swapless systems might seem faster, and an overall discussion of swap issues in modern computing. This is often an issue for system administrators, and this is a great set of posts about the issue."

18 of 581 comments (clear)

  1. If You have enough RAM by Greyfox · · Score: 5, Funny

    You could make a big ramdisk and swap to that!

    --

    I'm trying to teach myself to set people on fire with my mind... Is it hot in here?

    1. Re:If You have enough RAM by whiteranger99x · · Score: 5, Funny

      Yeah, just remember to allocate twice the amount of ram that you have installed! :)

      --
      Join the TWIT army now!
    2. Re:If You have enough RAM by martin-boundary · · Score: 5, Interesting
      I think this is the most interesting issue hinted on the mailing list.

      There are two "theorems" quoted: The first says that no matter what, if you have a size X of RAM used by the OS, and you add a size Y swap disk, you get better OS performance than if you only had X RAM.

      The second "theorem" says: if you have X RAM + Y swap disk, then add Y RAM and use that instead as the swap disk, then you get *faster* performance.

      The naysaysers now say that the second statement is misleading. Why? Because with X+Y RAM and Z swap disk, you'd get better performance again.

      I think this betrays an underlying assumption which I'm not sure is true, namely: X+Y RAM managed by the OS any way it likes is always better managed then X RAM managed by the OS any way it likes and Y RAM reserved for swap operations.

      In fact, let us suppose that the OS memory management is not optimal, ie when the OS manages X+Y amount of RAM, it does so suboptimally. Then it is possible that a different memory management scheme, e.g. X RAM used normally + Y RAM used exclusively for swap, may turn out to better use the available total RAM.

      So the theoretical question is this: is Linux's memory management sufficiently optimal that with an ordinary set of applications running, it can always make better use of X+Y amount of RAM than if it always reserved Y for swap? Alternatively, under what kind of running application mix is it true that reserving Y amount for swap yields a better memory management algorithm than using X+Y fully?

  2. they need updated docs for todays ram amounts by Anonymous Coward · · Score: 5, Insightful

    All the docs on Linux and swap amounts to use are from the days of 386s and 4 megs of ram!

    I want to know how much swap I should REALLY be using for a system with 1 gig of ram.

    Same for some of the kernel compilation docs. Maybe on a 4 meg system compiling that extra option might cause slowness but on a 500 meg system does an extra 30k in the kernel matter?

    Can we get some docs that aren't from the mid 90s!

  3. swap rule! by Coneasfast · · Score: 5, Informative

    the rule is swap should be 1.5x your RAM! ;)

    actually MS followed this rule, in win2k, the default swap size is set to exactly 1.5x your ram, was 176 for my 128mb system, and 384 for my 256mb system, not sure about XP though, someone fill me in
    (yes, some great minds working at MS)

    --
    Marge, get me your address book, 4 beers, and my conversation hat.
    1. Re:swap rule! by Majix · · Score: 5, Interesting

      The "swap=2x RAM" thing is obsolete admin trivia that simply refuses the die. It comes from the days when physical RAM was mapped into swap to simplify the swapping algorithm. If you didn't have at least a 1:1 correspondence between RAM and swap performance would suffer immensly. Starting with Linux 2.4 and up this is simply no longer true, there is no benefit from using excessively large swap partitions. Same goes for Sun OS and the BSDs these days.

      Instead, the swap needed depends on the sort of usage pattern your machine has. If it's a desktop with 1-3GB of RAM, a swap partition of 1GB is completely adequate. Want the machine to swap as little as possible and utilize all the RAM, so turn down swappiness a bit to avoid Mozilla/Firefox from being paged out when you leave for 15 minutes.

      On a server you need a whole lot more swap, the more the better. Not because it's necessarily any faster, it might be slower in fact with a high swappiness setting the system decides you don't really need that 2GB DB in memory if it's been unused for a month. But when you do run out of memory in legitimate use, the shit will really hit the fan if there isn't enough swap to pick up the slack.

    2. Re:swap rule! by Majix · · Score: 5, Interesting

      I forgot to explain swappiness. This is a entry in proc, /proc/sys/vm/swappiness, that you can plug a numerical value between 0 and 100 into. The higher the number, the more eager Linux will be to swap out applications from RAM to disk. There's a lot of conflicting opinions on what values you should use. Kerneltrap had a good article on it recently.

      Personally I use a value of around 20 or less for desktop machines. This keeps Mozilla being paged out after a short while, that really shouldn't be happening on modern hardware. Too bad you can't achieve the same effect in Windows 2000. Some people swear that a swappiness of 0 is ideal for their desktops, your mileage may vary. It's fun to play with in any case, any changes you make take effect instantaneously.

    3. Re:swap rule! by Admiral+Burrito · · Score: 5, Informative
      so turn down swappiness a bit to avoid Mozilla/Firefox from being paged out when you leave for 15 minutes.

      I think there is a common misconception about how virtual memory works in modern OSs. That is that if the VM writes a page to swap space, you have to wait for it to read it off the disk before you can use it again. That is not necessarily so.

      VMs may write pages to swap and still leave the page in RAM. The swapping is a pre-emptive measure, so that if it really does need to free that memory for other purposes later on it doesn't have to to swap it out to disk, because it is already done. Modern VMs will write pages to swap during idle periods for exactly this reason. If you really do have enough physical RAM, then the VM will never need to repurpose that page of RAM, and it can use the page from RAM without reading back from swap.

      In your hypothetical example the VM may write pages from your browser's process to swap, but when you get back to your desk you shouldn't notice, because the pages will still be in RAM.

      The only reason to turn down swappiness is if you are a victim of that misconception, or if you want to avoid disk activity during idle periods (like, on a laptop where the hard disk is set to spin down after inactivity).

  4. You've some good points... by robslimo · · Score: 5, Interesting

    but today's production, heavily loaded system will still need the ability to swap to/from disk.

    Already, there are systems that minimize that need, set-top boxes, embedded systems in general. But each of those is seriously modified (kernel-wise, mostly) to achieve the responsiveness, the frugality of resource treatment that a general purpose desktop computer can't expect to enjoy.

    That doesn't mean that developers should stay in the same rut, assuming that hardware that confined system design in the '60s, '70s... '00s will perpetually assign similar constraints.

    IMO, desktops still need to swap... for now. but let's not paint ourselves into a performance corner.

    1. Re:You've some good points... by Goldberg's+Pants · · Score: 5, Interesting

      If you've just got a box sitting not doing much, in other words not serving pages, SQL or whatever, you can run with minimal ram. My laptop has 24 megs of ram. I did have a 100 meg swap partition, but needed the space for a particular huge DOS game I wanted installed, so nuked it and converted it to DOS. Booted Linux and checked the ram usage and most of the ram was used.

      However, when I ran a program, the amount of used ram DROPPED.

      Of course in an environment where the system gets hammered, it's all very well talking about how cheap ram, but so is hard disk space. Is it really worth not setting up a bunch of swap space? What if a rogue process munches it's way through the ram while you're away? Would it not be better to have swap space and have it so the system can run, albeit not very well, than just die on you?

      I don't know, I ain't a sys admin, but performance issues aside, I don't see why you should risk it. I'd rather have swap partitions on a hardcore system than not.

    2. Re:You've some good points... by Proud+like+a+god · · Score: 5, Insightful

      Surely if your system runs out of RAM it shouldn't die? The runaway process, sure, but the OS should be able to reclaim some RAM from that and manage to carry on, no?

  5. Re:IMHO by Trepalium · · Score: 5, Interesting
    The other side of this is that memory that is not being used is wasted. Getting unused memory out of RAM, and into swap, so that memory can be used for real work can improve performance. This isn't just about memory that your applications are using. It's also about memory that is being used as cache for the disks you're using.

    Maybe you have enough memory to run your program, but you don't have enough memory to keep enough directory structures into RAM, so you keep needing to read the disk. If there are unused pages in that program that were only used once during startup, for example, it makes sense to get them out or memory, so that memory can be used for disk caching instead.

    Now, you have to understand how Linux handles paging, too. Unmodified pages from executables that are running may be discarded by the kernel at any time, because it knows where to get them. They won't be thrown into swap because it's not necessary. On the other hand, if that particular page has been modified (and some are modified as they are loaded by ld.so, for example), then the page must be copied into swap before it's discarded.

    --
    I used up all my sick days, so I'm calling in dead.
  6. Re:IMHO by irokitt · · Score: 5, Informative

    Linux has two properties that make swap a good thing (TM).

    The first thing to remember is that, for many Linux users, they have a newer PC running Windows (or a Mac ;) and a less recent PC running a Linux distro. The RAM threshold is realistically around the 128-512 MB range. Those who are dual-booting on a brand new machine can use 1 GB, but the rest of us put up with less than that (I for one want to avoid MBR screwups and the hassle of communicating with NTFS, so I don't dual boot. I had a nasty GRUB incident, so I'm probably paranoid).

    Finally, every Linux user that has compiled a kernel knows that it can really tax a system. Gentoo users also know how strenuous a XFree86 or KDE/Gnome compile can be. Being able to work on another terminal while compiling is one of the most beautiful things about *nix, and to do that on anything with less than 512 MB or 1 GB or RAM you want to have some swap.

    And finally, while RAM is very cheap, so are hard drives, and how hard is it to squeeze a swap partition out of a hard drive? Can it really hurt that much to let the system use it?

    As for Windows, swap is absolutely required for a lot of the games that are out there. I've heard that Unreal-engine based games in particular make heavy use of swap filing.

    --
    If my answers frighten you, stop asking scary questions.
  7. Re:I just don't get it. by sprag · · Score: 5, Informative

    The potential speed increase isn't seen when comparing 1G RAM vs a 2G RAM system. Its comparing a 1G RAM system with a 1G RAM system with swap.

    The gist of it is: with swap you can put things that aren't being used (like mingetty, gdm, etc) into swap to free up space for things that are running now. Without swap you have to keep the little-used processes in memory and you don't have as much 'free' space to use for things like caches.

    Its also important to note that the kernel will swap out code segments regardless of whether or not you have a swap partition: they get swapped out to nowhere. When they need to be swapped back in, the executable file itself is read.

  8. The Kernel Can Take a Hint by EventHorizon · · Score: 5, Informative

    In the average case code and data _do_ tend to be accessed more than once. We would all be complaining a lot more if the kernel NEVER cached... remember the huge performance boost SMARTDRV made in DOS?

    So, frankly, the default kernel behavior is right.

    To fix the movie/updatedb/jumbo cp/etc issues see "man madvise" and check out MADV_DONTNEED. I am hoping applications will start using this syscall sooner, rather than later. The Linux VM can take a hint, and it's pretty easy to give it one.

  9. "Swapiness" by Compholio · · Score: 5, Informative

    If you've got kernel 2.6 you can change the "swapiness" to fit your needs/desires. People with lots of RAM could experiment by changing the swapiness value to 0 and report back with the results (be easier than installing a system without swap).

  10. Swap sucks. :) by MikeFM · · Score: 5, Insightful

    I've built many servers, embedded systems, and even desktop systems that don't use any swap at all. Many more I limit the amount of swap greatly. The overall responsiveness is much better if you don't use swap and I find system stability to be better. Really it doesn't matter what the systems are used for or how many apps are being ran.. it's just how much memory you're going to use compared to the amount of physical memory you can afford. You can run out of memory just as easily using swap as you can while limited to physical memory.. the main difference being that the recovery of the sitution is much worse in the case of using swap. Quite often the system starts to churn and then grinds to a halt. Without swap those tasks just die and everything else keeps running. Setting memory limits on tasks is a good way of ensuring which tasks are killed first but I'd like to see better control of this given to the admin.

    --
    At what price learning? At what cost wisdom? The price is a man's peace of mind, and the cost is his life.
  11. Re: Hardly ever use swap by Black+Parrot · · Score: 5, Funny


    > Pan is an awesome program, but seriously...when it can single handedly use > 1GB of RAM just stealing divx rips...

    Think of it as a sin tax.

    --
    Sheesh, evil *and* a jerk. -- Jade