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

108 of 581 comments (clear)

  1. IMHO by rd4tech · · Score: 3, Interesting

    One can have 1GB of RAM for a fairly cheap price.
    I really doubt that majority of newest desktop PCs need to swap on the HD at all.

    The unused/used portions argument from the article isn't quite true. You don't have to swap every unused bit,
    if you have enough RAM, leave everything there. It's R-A-M. don't access parts you don't need.
    If you don't have them in the RAM, read them from the drive,
    don't waste time putting them where they mostly are in the first place.

    I'm willing to bet that people who need performance, don't often run 10 applications at the same time. If they do, they
    surely know what are they doing.

    IMHO the average user should get enough RAM and no swap, let the OS optimize things a bit.

    1. 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.
    2. 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.
    3. Re:IMHO by zoloto · · Score: 4, Informative

      My take on the swap -vs- no swap.
      I have no data, links or proof to back it up. Only the system responsiveness under two different setups.

      Setup A) 300mhz intel, 256mb ram, 6gb hdd, swap 512mb.
      Taking the swap out of that setup and the system crawled to a halt, expecially when using X. Re-enabling it and it ran just fine.

      Setup B) 2GHz Intel, 1gb ram, 40gb hdd, swap 2gb.
      Taking out the swap in that machine and the system ran fine. Even running Half-Life: Counter-Strike via WineX by transgaming. Re-enabling the swap, and I noticed a little performance increase, but I couldn't measure it b/c I didn't know how.

      Just my take on it. Swap is generally a good thing in older machines, while in newer systems it isn't a critical thing to have. HOWEVER, I did not run many tests with and w/o swap. just basic use, and Setup B's HL:CS test was only to make sure!

      YYMV, KBD.

      -zoloto

    4. Re: IMHO by Black+Parrot · · Score: 2, Insightful


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

      A third: Linux is a powerful and stable tool that makes it possible to run a dozen virtual desktops and stay logged on for a year at a time. So if you're a power user who leaves scores of applications open indefinitely as part of your ongoing work, kick some of them out to swap and leave them there until you get back on that project.

      I've added first one and then a second swap file, to quadruple the size of the swap partition I made when I installed my current system. My next system will have much more memory, but much more swap space as well. I'll just leave more and bigger programs open on more virtual desktops, and run less risk of The GIMP blowing up when I run a complex fu script on a big image.

      --
      Sheesh, evil *and* a jerk. -- Jade
    5. Re:IMHO by CAIMLAS · · Score: 4, Informative

      Kernel compiles do -not- tax the system. They utilize it. There are relatively few process threads used during a kernel build, substantial CPU use per process, a fair amount of disk reading, and a large amount of memory swapping about and modification.

      Its perfectly reasonable to expect a system to be responsive while compiling a large project. That's not a "taxed" system. A slashdotted machine, which spawns thousands of apache processes, is taxed, however.

      Except for your statement that for "anything less than 512M or 1G of RAM you want to have some swap"... You want to have swap all the time, unless there's a power-related (ie, laptop) reason to not have it. As the article says, it's trivially demonstrated that there is a performance increase by using swap, regardless of how much RAM. It is, however, imperative to have swap if the machine doesn't have much ram, otherwise you'll run into some nasty results after a while. :)

      I'd agree with your last two sentiments. :)

      --
      ~/ssh slashdot.org ssh: connect to host slashdot.org port 22: too many beers
    6. Re:IMHO by PacoTaco · · Score: 2, Insightful
      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.

      It shouldn't be, unless you have a low memory system and everything (including your swap) is on an older IDE disk that doesn't seek quickly. I often leave large builds running in the background on Windows, BSD and Linux systems with no noticeable impact on system responsiveness.

    7. Re:IMHO by Amorpheus_MMS · · Score: 3, Interesting

      Setup B) 2GHz Intel, 1gb ram, 40gb hdd, swap 2gb.
      Taking out the swap in that machine and the system ran fine. Even running Half-Life: Counter-Strike via WineX by transgaming.


      Do try that with Far Cry, I'm curious whether you'll notice a difference there. That recommends 1GB RAM, and swapping unused memory is certainly considered.

      Personally, I think games are the one reason why swap is still very useful. You either run your programs on your desktop, or a game - not both. Getting enough RAM to hold everything is wasted money.

    8. Re:IMHO by EsbenMoseHansen · · Score: 4, Informative
      That sounds seriusly broken to me. My linuxboxen never swap before the entire RAM is filled (with cache or otherwise). Even so, the swappiness stays quite low. Perhaps you show turn the
      /proc/sys/vm/swappiness
      knob down a bit? Or perhaps use this patch.
      --
      Religion is regarded by the common people as true, by the wise as false, and by rulers as useful.
    9. Re:IMHO by maraist · · Score: 2, Insightful

      I can't confirm the details, but it was my understanding that the Alan Cox fork in RedHat 9 had an implementation where the swap had to be at LEAST as big as main memory.. Theoretically the reason is that you preform pre-swapping.. The idea is that if you waited until the last second to do any swapping, then your most efficient choice would be to swap non-dirty pages.. but if instead during idle periods of IO you swap dirty pages, then when it comes time to swap, just about everything is fair game, and you can truely swap out the LRU pages with great efficiency.

      Thus you'd pre-swap once you got to like 60% full memory.

      More-over, as others' have said, unless you have as much RAM as the average hard-disk space used / day, then you are in a non-optimal operating environment, since your cache isn't as big as it should be.. cache flushing swapping.. They're almost identical in user-time experience (though aruably, re-reading a contiugous chunk of file-data is going to be faster than unswapping randomly positioned data; but how many files are contiguous these days?).

      Thus if you have a daemon with a LOT of setup code which is never used again after startup, then it makes sense to permanently swap this out to disk to free up space for the cache.

      --
      -Michael
    10. Re:IMHO by Jeff+DeMaagd · · Score: 2, Interesting

      I'm sick of the speculation. Maybe Linux has some key benefits that make swap useful on a machine that has more memory than it needs to operate. I'd like to see some evidence that those techniques actually make a jack of a difference or not.

      Is there anyone willing to take two identical machines and run a full Gentoo compile, with or without swap, with 256, 512 and 1028MB RAM installed, and time it? If swap really does make a difference, I think that sort of thing would help tell when swap is or is not useful in currently available systems. I'd love to do it but I simply don't have a good internet connection to do it.

  2. Swap is definitely necessary by betterthanducttape · · Score: 3, Insightful

    When I was running Linux on my 350 mHz Pentium II with 128MB RAM, you can dang well bet I wouldn't have made it without a swap partition. I probably would have gone back to Windows if swap hadn't existed.

    1. Re:Swap is definitely necessary by Anonymous Coward · · Score: 2, Interesting

      Not for everyone. I've got 1GB in my machine, and I don't think I've ever come near maxing it out. I've actually turned off the pagefile* in Windows and haven't had any problems other than Photoshop whining everytime I start it (even if it never uses more than 100MB of RAM, it still whines if there's no pagefile present).
      I don't use linux, so I can't say how well it'd work on my machine without swap, but I can't imagine it'd be any worse.

      * For the Windows-ignorant: a pagefile is the Windows equivelent of swap.

  3. 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 Wog · · Score: 2, Insightful

      I think what he's attempting to solve is the problem of some apps throwing a fit when they can't find a bunch of swap space, regardless of the 4 gigs of RAM installed...

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

    4. Re:If You have enough RAM by Listen+Up · · Score: 2, Interesting

      They are not theorems, but conjectures. A theory and a conjecture are not the same thing. No one to date as posted a theory.

    5. Re:If You have enough RAM by kasperd · · Score: 2, Insightful

      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?

      I don't think you will find an application where that is the case. But maybe if the Y amount you allocate for swap happened to be slower than the rest of the RAM, it could improve performance. If all of your RAM has the same speed, the VM really have to be f***ed up to not give you better performance when it can manage all of your RAM.

      --

      Do you care about the security of your wireless mouse?
    6. Re:If You have enough RAM by hobo2k · · Score: 3, Insightful
      I had to read that a couple times before I noticed the problem. You have the second theorem wrong. It should say: "X ram + Y swap is slower than (X + Y) ram with NO swap at all". Then, your question about managing X+Y ram wouldn't make sense because there is nothing to manage: either you run out of memory and apps die or you don't.

      Intelligent memory managment only affects performance if you have swap space. Swap space could be defined as storage which is slower than main memory. If all your storage is the same speed, memory management is trivial.

      Ironically, I have the same challenge for theorem #1 that you used for #2. #1 states that having swap is better than not having it. Clearly having swap increases the amount of allocations programs can make before their malloc's fail. But improve performance? That is only true if the OS can predict what data is needed for the future operations. If it predicts wrong, the usage of swap can degrade performance.

    7. Re:If You have enough RAM by kasperd · · Score: 2, Interesting

      True fragmentation of memory does not mean performance decrease. But fragmentation can still be a problem in some cases. For example if you need to allocate multiple pages at once, and only have scateret single pages available, the allocation would either fail, or you would have to free some memory. This is one of the reasons the task struct + stack allocation in Linux have finally been reduced from two to just one page (on x86 that is, I don't know about other architectures).

      Another possibility, that has been suggested for 2.7 is defragmentation of memory. Of course just because it has been suggested doesn't mean it is going to happen. Without defragmentation, what are your options to satisfy a larger allocation in case of fragmented memory? You'd have to free some memory, either by reducing the disk cache size, swapping out anonymous pages, or find some slabs that can be freed. But notice that with more memory there would be more possible choices for what to free, so it would have a better chance of picking something you won't need in the near future. If you used some ram for a ramdisk for swap, then you should not expect to use all of that ram. So effectively you are using less of your memory, which again could mean smaller chance of finding what you need in RAM. The failing allocation might as well be satisfied by removing a page from the page cache, which is certainly less disirable.

      Of the given suggestions defragmentation of memory is probably the only that shouldn't cause performance problems in specific corner cases. (Me thinking back on the good ol' days with MS DOS and AmigaOS where the only solution to memory fragmentation was a reboot).

      --

      Do you care about the security of your wireless mouse?
    8. Re:If You have enough RAM by ceswiedler · · Score: 3, Interesting

      Adding RAM always helps. No one ever says that swap is BETTER than RAM. Having X+Y RAM is better than X RAM + Y swap. However, having X+Y RAM plus Z swap is better yet.

      Sure, add more RAM. But swap will always be useful, because there's always some stuff which is better off on the disk, because it hasn't been used in forever, and until your RAM is larger than your HD, you'll get better milage out of that RAM if you use it as a cache.

    9. Re:If You have enough RAM by grmoc · · Score: 3, Insightful

      When you're DMAing large amounts of memory, memory fragmentation becomes an issue.

      This is why the 'bigPhysArea' patch to the kernel exists-- to create continuous bloakcs of memory which can be transferred without having to scatter/gather.

      Note that this is independant of memory -usage-.. this is an issue with the 'block size' if you will, of memory segmentation.

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

    1. Re:they need updated docs for todays ram amounts by rd4tech · · Score: 2, Insightful

      Start running bunch of applications and seeing what happens with the memory and the swap. The swap hardly gets used at all if you have 1GB RAM. On the other hand, on my old 486 with 32MB of RAM, swap was the main thing.. Sometimes several hundred MBs

    2. Re:they need updated docs for todays ram amounts by Eskarel · · Score: 2, Insightful
      Well that's really not a totally fair comparison. True, you may use far more resource intensive apps on your linux machine, but unless you're running some varation of wine(and even to a certain extent then) it's not likely many of those apps are games.

      A heavily used server is really not comparable to a game even if it seems like it uses more resources simply because it's far more likely to be well written than your average game. I've seen some seriously nasty memory leaks in popular games that I'd never see in something which was better designed be it for Windows or Linux.

      If you were comparing running the same app on windows vs linux then perhaps you could criticize the memory manager(which is honestly probably not as good), but you're not.

  5. Where many people miss the point... by NerveGas · · Score: 4, Insightful


    People like to claim that swap can always improve performance, by swapping out unused sections of memory, allowing for more memory to throw at apps or disk cache.

    Well, *most* apps won't just arbitrarily consume memory, so endless amounts of memory won't help. And disk cache gets you greatly diminishing returns.

    One of the machines I use has 3 gigs of memory. It will swap out unused programs, in an attempt to free up more memory. The joke is that it simply can't use all three gigs. After half a year of uptime, there's still over half a gig completely unused, because the apps don't take memory, and there's not that much to put in disk cache.

    Obviously, that's a pathological case. And there are pathological cases at the other extreme. But as memory prices keep dropping over the long run, swap does become less and less useful.

    steve

    --
    Oh, you're not stuck, you're just unable to let go of the onion rings.
    1. Re:Where many people miss the point... by Anonymous Coward · · Score: 2, Informative

      One of the machines I use has 3 gigs of memory. It will swap out unused programs, in an attempt to free up more memory. The joke is that it simply can't use all three gigs. After half a year of uptime, there's still over half a gig completely unused, because the apps don't take memory, and there's not that much to put in disk cache.

      Yes, it is swappnig because it is trying to free up "low memory", of which you have less that a gig. This is unfortunately an inevitable failure case of Intel's brain damaged (although practical) PAE schmeme.

      Were you to have no swap at all, you may have had to reclaim important kernel side caches (inode, dentry caches), or even had memory alloction failures in eg. the network stack (common).

    2. Re:Where many people miss the point... by torinth · · Score: 3, Insightful

      What about the basic situation of not setting a hard-to-describe limit on desktop users? Managing and disabling swap is great in controlled environments like servers and embedded systems, where the applications being run are limited and pre-determined.

      But on desktop systems, a user may want to use Word, Photoshop, Outlook, Internet Explorer, an anti-virus tool, 30 other system tray tasks and services, etc. Should this user sit there and add up the recommended RAM of each of every application she owns and use that as a guideline for buying? That seems a little over-complicated and wasteful. Most of the time, she won't be running every application, but she really should be able to when she wants to.

      The solution is to introduce a cheap storage tool to extend what's treated (by applications) as RAM--swap.

    3. Re:Where many people miss the point... by kasperd · · Score: 2, Interesting

      Yes, it is swappnig because it is trying to free up "low memory", of which you have less that a gig.

      Actually this sounds likely, but is it a good idea? Alternatively it could do a memcpy of your data from low memory to high memory. So now you have the choice between occupying the CPU to perform the memcpy, or occupying the disk controller to swap it out. But data that you could swap out is process memory, which you'd expect to be allocated from the high memory. So how do you actually reach a situation where process memory pages end up in low memory? You'd have to fill up the high memory first. Of course if you run a program that requires a lot of memory, which is all allocated from high memory, then other programs might get low memory. When the first program terminates you could have a lot of free high memory and other programs still taking up the low memory.

      --

      Do you care about the security of your wireless mouse?
    4. Re:Where many people miss the point... by Alioth · · Score: 4, Interesting

      The 2.6 kernel now has a swappiness setting in /proc where you can tell the kernel avoid swapping please (set it to zero) or swap like mad (set it to 100). Therefore you can tune your system to your specific needs. It'd be nice if they had a similar control for filesystem cache.

    5. Re:Where many people miss the point... by joib · · Score: 4, Informative


      It'd be nice if they had a similar control for filesystem cache.


      You're missing the point. That's exactly what the swappiness setting does, indirectly. If you avoid swapping (swappines = 0), the system has less memory left over for filesystem cache. OTOH, if you set swappiness=100, the system has a lot more memory to use for file cache.

      The system always tries to use all available memory, and that's a good thing. The question is whether to use extra memory for file cache or for keeping pages in memory.

  6. 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. Re:swap rule! by Tiro · · Score: 2, Informative
      You got me to look in /proc for said file, then I thought you tricked me! but it really does exist. Only it wasn't added until 2.6 & some of us still run 2.4

      I'd never used files in proc before so I had no idea if such a beast existed.

  7. no swap? by hawkeyeMI · · Score: 3, Interesting

    I ran linux without a swap file on 128 MB of memory a couple of years go. It was an accident, I didn't create a swap partition. I never had a problem (forutnately). Of course, I wasn't doing the heavy duty stuff I am now (scientific computation).

    --
    Error 404 - Sig Not Found
  8. Swap space not needed.... by kidgenius · · Score: 4, Funny

    If you've got 128MB of RAM you have plenty and therefore will have no need for swap space. I mean, isn't 640k enough for everyone?

    1. Re:Swap space not needed.... by Coneasfast · · Score: 4, Insightful

      isn't 640k enough for everyone?

      people constantly make this joke, but seriously, at the time BG said this, it was probably true.

      if today i say "1 gig ought to be enough for everyone" it is true, but in 10 years from now you will be laughing at this.

      he never claimed it would 'ALWAYS' be enough (unless there is more to this quote???)

      --
      Marge, get me your address book, 4 beers, and my conversation hat.
    2. Re:Swap space not needed.... by EvanED · · Score: 2, Insightful

      I doubt it was even that much a lack of vision. You *have* to make a limit somewhere, you can't make, for instance, addresses an infinite number of bits. Now, maybe they were too shortsighted in picking 640K (which actually doesn't make sense to me as it's not a power of 2, but I guess I don't know enough about the reasoning), that's something to pick. But eventually, yes, people would turn around and laugh at it.

    3. Re:Swap space not needed.... by pantherace · · Score: 2, Interesting
      As I recall (it hasn't been recently) The chips that were designed had the capability to address 1MB of RAM (8088 & 8086). IBM reserved the top 384KB or so for ROM & system calls. So the person was left with (1024KB-384KB) 640KB for using as actual RAM.

      That's part of the reason why the 4GB addressing limit matters, and really x86 is hurt badly performance wise if you have more than a GB or 2, even below the physical 4GB limit (which can be extended via Intel's extensions (this limit doesn't exist in native AMD64, or Intel's semi-copy of that.)): now x86 relies on paging & virtual memory spaces, with upper addresses reserved for libraries, & kernel calls. This mapping may take up a fair amount of space, and when manipulating large data sets (very large images, Databases & other stuff)... this becomes problematic, because of the virtual 4GB limit. The physical limit may not have been reached, but the virtual limit is. Doesn't mean more RAM isn't faster, but it does mean that there is a speed hit in some cases.

      If Bill Gates said it (he has denied it, but it's been around a LONG time), it may have even been something resigned possibly preferenced with an "Oh well, " or something like that.

  9. Yes, It's Necessary by rsmith-mac · · Score: 3, Insightful

    As long as users can eat up more memory than they have available, and as long as hard drive space is cheaper than RAM space, swap will always be necessary.

  10. Swap nessisary in low mem (128M) systems by strredwolf · · Score: 4, Informative

    Swap improving performance... yeah. On slow systems and low memory, every byte freed up helps. But not swapping in the first place is good too.

    I'm now expermineting with replacing various tools with smaller versions, such as dropbear, udhcp, tinylogin, and buzybox. I'm also slowly writing up a "exec and restart shell afterwards" utility called PivotShell.

    Hardware wize, I have swap on a CF drive. 32 megs so far, but if I can afford larger CF drives, I'll format 'em as swap and use them.

    Why all of this? 40 megs swaps to HD, and on a laptop, any HD access sucks battery power. When you're using Xfree (or even Kdrive) and Firefox, you're going to swap. Period.

    --

    --
    # Canmephians for a better Linux Kernel
    $Stalag99{"URL"}="http://stalag99.net";
    1. Re:Swap nessisary in low mem (128M) systems by BiggerIsBetter · · Score: 4, Informative

      Don't do that. CF has a limited number of write cycles (100,000 or so, typically), and swapping to it will kill it quite quickly.

      --
      Forget thrust, drag, lift and weight. Airplanes fly because of money.
  11. Re:On a laptop... yes, for the wrong reason by whiteranger99x · · Score: 2, Informative

    Yeah, because God forbid you should have to use the SAME amount of disk space as the ram you have installed to save a few seconds to bring the laptop up :P

    --
    Join the TWIT army now!
  12. Swap can save your ass by Julian+Morrison · · Score: 4, Interesting

    Sometimes, when a process goes haywire, it will start munching RAM. If important programs like, say, sshd or X, can't malloc when they need to, they'll die ignominiously. Swap gives you the chance to kill the rogue process before your OS goes kaput. Its slowness can actually help for this.

    1. Re:Swap can save your ass by anshil · · Score: 4, Informative

      Thats not true, if the linux kernel gets out of memory it takes the list of all processes, scores them with memory usage, runtime etc. and then simply kills the process of highest score. In your case your RAM munching app. would just be killed by the kernel.

      I know that pretty for sure, since I modified that part of the kernel once for an embedded system, where we explicitly didn't want it to kill any process, but have instead to reboot in such a case. Since nothing is worse than having a half-functional system with some processes missing....

      --

      --
      Karma 50, and all I got was this lousy T-Shirt.
    2. Re:Swap can save your ass by Alien+Being · · Score: 2, Insightful

      The parent, was talking about processes "munching RAM", not VM.

      On a system with no swap, all of VM would be exhausted very quickly by a runaway, at which point the behavior you're describing would kick in. But on a system *with* swap, IO waits act like a brake. In some cases it gives the admin time to stop the runaway train before it hits the wall (no more VM).

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

    3. Re:You've some good points... by krewemaynard · · Score: 4, Informative

      "...the OS should be able to reclaim some RAM from that and manage to carry on, no?"

      yeah, pretty much. i may be openly admitting my ignorance here, but i have a gentoo box with 256 MB DDR RAM. i set up a swap partition, and set up the entry in /etc/fstab, but when i ran the box, it never touched the swap. me: "great, this RAM ownz! and the 2.6 kernel ownz at memory management! it never uses swap!"

      then, stuff started dying on me during times of heavy system load. like, i'd be in Firefox and running emerge at the same time, and firefox would croak on me. or VMWare wouln't boot Knoppix. or OpenOffice would die. all this time i had something else going on on top of whatever memory-intensive program was dying. me: "wtf, mate?"

      it finally occurred to me that the box still wasn't swapping, and that might be a bad thing. so i tried to run "swapon /dev/hdb2" (my swap partition) and got errors. weird. then i realized what happened...

      ...i had forgotten to officially make the swap partition using mkswap. i'd been running this partition schem for about 2 months, and never realized it until about 2 days ago.

      all that to say, yes. the system DOES reclaim memory. by killing other stuff :)

      i'm a stoopid monkey sometimes :-D

      --
      I saw it on Slashdot, it must be true!
    4. Re:You've some good points... by PastaLover · · Score: 3, Informative

      The system only does that when it runs out of unused buffers though. This means that at the time a program has to be killed all of your ram is officially used by processes.

      I think the OP is probably right. I have 512 megs of ram and using my full suite of apps (let's say something like firefox, thunderbird, a seti process, xmms, irssi in an rxvt and maybe some other xterms etc) I'm using about 200 megs of ram (not counting unused pages). So there was no real need for me to have swap since it never, ever gets used (I've never actually tried to fill all the 500 megs up to the full). So on a desktop system, you rarely have enough bloat to actually need swap.

      That being said, I don't understand why a swapping system would be slower than a swapless system, except maybe on bootup when you have one extra partition to load.

    5. Re:You've some good points... by haggisman · · Score: 2, Interesting

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

      Not Windows XP for sure .. I edited an ISO image using 2 different editors and instead of the editors barfing, XP froze solid each time.

      Scotty

  14. Try this with linux by arvindn · · Score: 4, Interesting

    Notice how sluggish the system is after doing something disk-intensive like watching a movie. That's because the kernel is caching as much of the movie as possible to memory and swapping your running apps out. And kernel developers think this is a good thing, so it isn't going to change any time soon. IMHO for a desktop system this makes no sense, that's why I run my 1GB RAM machines with zero swap.

    1. Re:Try this with linux by Lumpy · · Score: 2, Interesting

      IMHO for a desktop system this makes no sense, that's why I run my 1GB RAM machines with zero swap.

      fine for you being a typical home user not doing much with your PC.

      now with me editing 4GB video clips, rendering a 2Gb CG clip or trying to process a large rotoscoping project in film-gimp 1GB of ram is consumed 3 minutes after I sit down at that machine.

      I have 2GB of ram + 4GB of swap and I can easily fill it all up using wither Blender, Film-Gimp or any of the other tools I use.

      and I'm betting that many others here that actually use their computers for real work instead of what they typical home user uses it for will also chime in.

      Basically, when you hit the top of your ram... hell will break loose on your machine... I can't afford to lose my work when I run out of ram, so I use swap to get more done.

      --
      Do not look at laser with remaining good eye.
  15. I just don't get it. by mcg1969 · · Score: 2, Interesting

    Seriously, I don't get it. How in the world can swap ever increase performance.

    Specifically, suppose I have one computer with 1GB of RAM and 1GB of swap, and another computer with 2GB of RAM and no swap. Under what circumstances will the first computer be any faster?

    Now I suppose if the swap is used for other things besides memory space then I could understand it. But then it seems like a simple solution would be to allocate a fraction of RAM for those things. In effect, create a swap partition on a RAM disk :)

    Seriously, I'd appreciate some education here, but make sure you answer my specific scenario above if you reply... thanks

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

  16. I'm curious how windows does it by Stevyn · · Score: 2, Interesting

    This may be slightly off topic...

    Running KDE 3.2.1 now, I notice it takes longer to open apps than it does in windows. Mozilla for example takes literally a few seconds longer to open each window than it did in windows. Another thing windows does is make it faster when you run an app right after you ran it then closed it. Say for example in windows I run mozilla, then close it, then open it. When it opens it the second time, it's almost instant. However in linux, it seems to take the same original amount of time to load it completely. I'm sure it has to do with an entirely different process of loading programs, but apps always seemed to open faster in windows than in linux, in my view.

    Then again, graphics used to be in the NT kernel and that's what made it appear fast, but lead to a lot of problems and crashes, so maybe the longer load time is worth the wait when compared to a reboot.

    1. Re: I'm curious how windows does it by Black+Parrot · · Score: 4, Interesting
      This may be slightly off topic...

      Running KDE 3.2.1 now, I notice it takes longer to open apps than it does in windows. Mozilla for example takes literally a few seconds longer to open each window than it did in windows. Another thing windows does is make it faster when you run an app right after you ran it then closed it. Say for example in windows I run mozilla, then close it, then open it. When it opens it the second time, it's almost instant. However in linux, it seems to take the same original amount of time to load it completely. I'm sure it has to do with an entirely different process of loading programs, but apps always seemed to open faster in windows than in linux, in my view.

      Then again, graphics used to be in the NT kernel and that's what made it appear fast, but lead to a lot of problems and crashes, so maybe the longer load time is worth the wait when compared to a reboot.
      Conventional wisdom is that Windows uses lots of hacks to make it "look" faster in the way you describe, without regard to the cost it imposes on other operations. I'm almost certain that XP keeps some applications in memory after you "exit" them. Sometimes I notice that something won't work after running certain big applications, suggesting that sufficient resources haven't been released. Also, sometimes a shutdown complains about an application that won't respond even after you've closed everything. I think they're hoaxoring people to think they got a fast system, when they're really just robbing Peter to pay Paul.

      --
      Sheesh, evil *and* a jerk. -- Jade
    2. Re:I'm curious how windows does it by dabraun · · Score: 2, Interesting

      One of the things that makes XP start apps very quickly is this:

      It watches applications startup and monitors what they read from the disk - it notes this in a log. During idle time it moves the sectors around on disk so that they will all be in the same place for the next time you start that app. When you start the app later it runs out and reads everything that it believes the app will want to read all at once. This pre-reading and disk order optimization makes XP start apps a heck of a lot quicker than previous versions of the OS did.

      It also does exactly the same thing for the boot process. There's even a tool you can download from MS's web site that will allow you to force the system to clear what it thinks about the boot process, reboot, and force the ordering to take place immediately rather than during idle time.

      David

  17. Re:On a laptop... yes, for the wrong reason by robbo · · Score: 2, Interesting

    It's all diskdrake's fault. I didn't choose my swap size, afair, although it's weird it's smaller than my RAM. Beyond that, acpi recommends you have swap about 30% larger than your RAM. While it would slow down the suspend, I don't see why acpi doesn't pipe /dev/mem through bzip first, or for that matter, why hibernate can't just dump to a file.

    --
    So long, and thanks for all the Phish
  18. gmail by maxbang · · Score: 3, Funny

    I use my gmail account as my swap partition. It's fully searchable and displays helpful advertisements every time I load fifty tabs in Firefox and OpenOffice goes idle. I don't know what I'd do without it. I'd probably be less of an unfunny jackass.

    --
    I also reply below your current threshold.
    1. Re:gmail by maxbang · · Score: 2, Funny

      Dude, that's the funniest thing I've read today. And I'd totally date you, but my IQ is four. I'm typing this with a toothbrush that's stapled to my head.

      --
      I also reply below your current threshold.
  19. okay, here's a challenge... by Doppler00 · · Score: 4, Insightful

    Does anyone out there want to run a series of benchmarks with a few standard applications to prove/disprove whether disabling swapping improves performance?

    I'm tired of just hearing antidotal evidence on this. Everyone has their stories about turning off swap files and improving performance, but in what cases? Are there some users this would harm?

    1. Re:okay, here's a challenge... by Trepalium · · Score: 2, Insightful

      I think it's simply a case of, 'there's no simple answer'. Even a benchmark would be difficult to do, because it would vary depending on workload. You might be able to handle a particular machine with no swap, but I would find it unusable. I'm not even sure how you would test this. What kind of performance would you test? Latency? I/O throughput? Integer instructions per second? If you turned off cache, your maximum latency and insn per second might increase, but your throughput may decrease.

      --
      I used up all my sick days, so I'm calling in dead.
    2. Re:okay, here's a challenge... by arvindn · · Score: 2, Insightful
      Unfortunately, that's very difficult, perhaps impossible.

      The users who are complaining about swap are saying that it decreases desktop responsiveness. Responsiveness is different from performance, and is frequently antithetical to it. It is inherently subjective and therefore hard to quantify.

  20. well one reason by discogravy · · Score: 2, Informative

    one reason you'd want swap on a system is to have someplace to dump/savecore information in the case of system crashes. Kind of hard to do with volatile memory.

  21. It's a choice... by Beolach · · Score: 4, Interesting

    As I RTFA & previous comments here, I was rather suprised at how argumentive people were getting over this. Some people are saying swap is an absolute necessity & a swapless system was a broken system, while other's said swap was an obsolete solution to a problem that no longer exists (expensive RAM). This seems odd to me, because as far as I can tell, the decision of whether & how much swap to use is based mostly on two things: specific situations (and thus there is no general answer to 'Is Swap Necessary?'), and opinion. And either way, with the Linux kernel today (and for quite a while now), I can choose for myself whether or not, and how much, swap I want to use. So if I am in a situation that I think requires swap, I can use it, and in a situation that I think would be hurt by having swap, I don't have to use it. So I don't see why there's so much hoolabaloo about this: nobody is forcing anyone to do it one way or the other. And if someone else thinks it should be done different from how I would do it, that's their decision, not mine.

    --
    Join moola.com, play games to earn money.
  22. swap deals with bloat by wotevah · · Score: 4, Insightful

    Most applications today have unnecessary or rarely used portions of code or data - bloat. These get swapped out first. Also there are various memory leaks here and there, which means the programs sometimes forget to release allocated memory they do not need any longer.

    Look at the size of your X server, or mozilla, or apache, or pretty much anything else and you will see over the course of a few weeks that it has grown beyond reasonable operation demands.

    The memory lost this way is never accessed from there on, but the system cannot release it without the program telling it to, so it does the next best thing and shoves it in the swap. Not a solution since eventually swap gets full, but since the leaks are slow to begin with, at least it prevents them from affecting system performance too early.

    1. Re:swap deals with bloat by spitzak · · Score: 2, Interesting

      Unfortunatley that bloat is also *fragmented*. Even a 4-byte structure that is still in use buried in a page will keep it swapped in. In my experience the only way app pages get swapped out is when the app is idle.

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

    1. Re:The Kernel Can Take a Hint by ndogg · · Score: 2, Informative

      linux-specific code
      Linux-specific code? Did you bother to look at the man page? Didn't think so...

      And I quoth from my man page:CONFORMING TO
      POSIX.1b (POSIX.4). POSIX 1003.1-2001 describes posix_madvise with constants POSIX_MADV_NORMAL, etc., with a behaviour close to that
      described here. There is a similar posix_fadvise for file access.

      --
      // file: mice.h
      #include "frickin_lasers.h"
    2. Re:The Kernel Can Take a Hint by swilver · · Score: 3, Insightful
      The default kernel behaviour is WRONG. The whole idea of memory is to put in stuff that will be likely to be accessed again. How likely is it that you will be watching a 1 GB movie again?

      Of course, the Kernel will have no idea about watching movies, but it stil can distinguish this "unimportant" data from data that do needs to be cached. The most important way to distinguish this data from data that does need caching is how fast it is needed in the first place.

      When I do a grep on the kernel tree, you'll find that your harddisk speed is bottle neck; it is worth caching this data as grepping from memory would enhance its performance.

      When I play a movie, the harddisk is not the bottleneck, in fact, NOTHING is a bottle neck, as my movie would be stuttering and unwatchable otherwise. This data is not worth caching (atleast not worth caching so much of it to the point of swapping out all else).

      This goes for most media streams, but also for interaction with the internet (downloads/uploads/p2p). There's no need to keep a 1 GB file cached when that file has "accumulated" cache space in the course of an hour or more (ie, slow I/O); if it was important enough to warrant caching, I'd think the harddisk would have been the bottleneck in the first place...

      --Swilver

    3. Re:The Kernel Can Take a Hint by Foolhardy · · Score: 2, Informative

      Don't blame XP's kernel. When you open a file in Windows that you plan on reading sequentially, you're supposed to use FILE_FLAG_SEQUENTIAL_SCAN. This is a big hint that you won't be seeking randomly or backwards, so only a little read-head caching should occur. By default, the cache manager thinks you will want to access the file randomly, so it stores as much as it can for your convenience. It is the program copying the file (probably explorer in this case) for not providing that hint.

      Actually, copying a file shouldn't be buffered at all; it should use an IO completion port and multiple (mabye 10) concurrent async IO operations to copy the file witn no buffering.

  24. Amiga by Jace+of+Fuse! · · Score: 4, Interesting

    In the 90's, I ran a 10 line BBS on an Amiga 4000 with 16 megs of Fast ram, 2 megs of Chip ram, and 0k for the swap file. :)

    I know, I know, the Amiga didn't HAVE virtual memory. Well actually it did if you had an 040 and installed a memory management program such as GigaMem, but so few people had a use for such a thing that it was practically unheard of.

    Oh, and before someone jumps in saying that I wasn't able to do anything else, that is totally NOT the case.

    Very often I was doing lots of stuff. The difference is developers were used to working within memory constraints, and now days developers are used to systems growing into the applications.

    --

    "Everything you know is wrong. (And stupid.)"

    Moderation Totals: Wrong=2, Stupid=3, Total=5.
  25. U R N Idiot by Graymalkin · · Score: 4, Informative

    I haven't seen a case where disabling swap actually increases performance. I have however seen lots of cases where disregard for logic involving swap space caused serious performance problems. The old 1.5x and 2x rules for swap space are outdated and even dangerous in today's systems with ooglebytes of memory.

    With less than 128MB of RAM you practically need 2x your physical memory worth of swap space. Running a full GUI environment, even a relatively lightweight one, needs quite a bit of system memory. With 64MB of RAM and a 128MB of swap space you'll be able to run a light GUI environment but have a crappy filesystem cache. The system will crawl but it won't get constant OOM errors if you're not overzealous with your app usage.

    The 2x RAM rule on a system with 512MB of physical RAM on the otherhand is excessive. With 1GB of swap space most of it will end up empty unless you're running programs needing huge amounts of allocated memory. With more than 512MB or more of physical memory on a single user workstation you're pretty unlikely to run into situations where active pages are swapped out to disk.

    I've seen the runaway process situation crop up on more than one system with excessive amounts of swap space. Since swap is so slow it can be troublesome to kill a process that is using so much memory that it ends up having active pages swapped to disk. The system ends up spending 99% of its time trying to handle the disk IO from the heavy swapping which can make the system totally unresponsive for local and remote users. Because the systems had way more swap space than was logical the offending processes never got OOM errors even though they were using up almost all of the system's resources.

    I've pretty much set 256MB as the upper limit for my systems with 256MB or more of physical memory. That is enough swap space to hold any dirty pages or unused processes but not so much that a runaway process is going to eat up all my disk IO for a couple of hours. Once a system hits the 256MB threshold I toss out the silly 2x RAM rules for something with a little more cognitive thought.

    --
    I'm a loner Dottie, a Rebel.
  26. "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).

  27. 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.
    1. Re:Swap sucks. :) by oolon · · Score: 2, Insightful

      There are good reasons for swap, for example when a program forks, You need spare ram for the complete process space, this space normally comes from swap, before being wiped out when a new command is execed. Another good thing to do with swap space is use tmpfs and use it from /tmp, that way if you have lots of memory /tmp will come from memory not disk, and if your stuck for space your use the swap space.

      James

  28. swap sucks with 2k & xp - disable it if possib by Festering+Leper · · Score: 3, Interesting

    there's a definite pattern with regard to swap in the windows world.

    for win'9x: use up ram until almost gone then start allocating swap space in anticipation of actually using it. should memory allocation still be increasing then actually use swap space. reverse the order when freeing memory.
    i had 384 megs ram at the time and as long as i used less than about 350 megs total the system wouldn't be in swap.

    for win 2k & xp: (when within physical ram limits) whatever amount of memory is requested, allocate between 60-80% to ram and the rest to the swapfile. even the disk cache partially goes to swap! i didn't believe it at first but all one has to do is look at the numbers in the task manager's memory/cpu window. at first i figured that all i'd need to do is throw in some more ram and the disk thrashing and absolute crawl would go away. i put in a gigabyte of ram (i never allocate more than 700 megs at most and the total system memory usage on bootup is 100 megs). even with the extra ram the problem stayed the same.

    turning off swap gives me consistent fast performance, and since the disk cache isn't swapped (partially) i get 2x the throughput i had with a swapfile on large file copy operations

    machine tested: duron 1.3ghz, 1 gig pc133 ram, 2x 80 gig wd800jb hdd.. os win2000 & winxp running newsbin which allocates disgusting amounts of ram in a large header grab (yeah i could have used a test program but why do that when newsbin is a real-world test for me). the os and applications are on different drives on their own ide chains

    with swapfile enabled (size=1.5x system ram).
    allocation time: unaffected, only the time to perform task reqested
    memory de-allocation time: (by either quitting app or selecting another group) 23 MINUTES of constant disk thrashing

    with swapfile DISabled
    allocation time: unaffected, only the time to perform task reqested
    memory de-allocation time: (by either quitting app or selecting another group) 2 seconds

    --
    if you want people to think you know what you are talking about, just put ".com" at the end of everything you say.com
  29. Re:What about Diskless Drives! by shione · · Score: 2, Funny

    Did anyone else read that as dickless drives? Man I think I need glasses.

  30. I always have 2G swap by menscher · · Score: 4, Interesting
    Why not more? Because that's the largest a swap partition can be. Why not less? Because disk is cheap. It has little to do with the amount of ram in the machine either, because it's easy to add more ram, but a bit harder to repartition for more swap.

    Here's a real-life example of why swap is useful. One machine I manage has a gig of ram. At the time of purchase, that seemed quite reasonable. But the users are working on a project that takes 2 gig of ram. So currently it's using a gig of the swap. Yes, that's bad, and I'll be adding a second gig to it in a few days (it's in the mail). But in the mean-time, that swap space is really handy. It means the users can get their work done! Think of the first 256M of swap as being for speed. If you're regularly using more than that, then it's time to order more ram. But it's nice to have the spare gig of ram for odd jobs, or while you're waiting to install it.

    I'm no expert, but I think a lot of these arguments could be resolved if people took advantage of the ulimit constraints. If you can limit how much a program can get out of control, then there's no longer a concern for a single user sending the server into swap hell. One of my current projects is to figure out reasonable limits.

    1. Re:I always have 2G swap by ananke · · Score: 3, Insightful

      "Why not more? Because that's the largest a swap partition can be"

      Just a side note: you can have multiple swap partitions. [not that you need them, but you can have them].

      --
      --- d'oh
  31. BillG and 640K by steveha · · Score: 4, Informative

    Bill Gates never made the infamous "640K... enough for anyone" comment. Not only have I never seen it documented anywhere, but he was asked about it and replied that he never said that.

    He didn't see the Internet coming -- he thought MSN should be like CompuServe, because that was the top info service (before the Internet became big). And I remember some wild comments he made about the truly amazing, throbbing power of the 286 chip. So he's not an amazing guru with awesome predictive powers. But people keep beating him up about this bogus quote, and I'm tired of it.

    steveha

    --
    lf(1): it's like ls(1) but sorts filenames by extension, tersely
  32. 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
  33. Reminds me of issues we had from Welchia by harikiri · · Score: 3, Insightful
    If I recall correctly, Welchia (the worm) looked for target hosts by ICMP scanning. On several of our cisco routers, the increased traffic resulted in them running out of memory, to such a point where you could not log into them.

    Apparently a new feature (mentioned by a network engineer workmate), is to have the IOS reserve a portion of memory for administrative tasks (like supporting the login process and configuration shell).

    A feature like this, that "reserves" a portion of RAM so that if something really fubars your system, you can still login to fix it - would be great for Linux/BSD.

    --
    Man watching 6 MSCE's around a sun box, looks alot like the opening scene's of 2001:space odyssey...
  34. Just FYI by slittle · · Score: 2, Interesting

    I've run my Linux systems without swap for years (since 2.2) without any problems. Of course, I make sure I have way more RAM than I am likely to need (the stuff is practically free these days; but OTOH, so is HDD space.....).

    Simply put, you need enough 'memory' to hold all the stuff you want to run, plus caches. For a given task, you might go for a system with 512MB RAM and a 512MB swap, and I'll just go for 1GB RAM and forget the swap. The only difference is that if/when your system comes up on its RAM limit, it's going to start slowing down. When it starts using a lot of swap, it's going to crawl. But it'll still run. Until you run out of both.

    Mine will run like blazes upto the 1024MB limit, then barf. No warnings like with swap.

    So if you want an early warning sign, use swap. If your needs are well known and won't push beyond the limits of your hardware, don't bother.

    You can always add a swap file later/only the fly as your needs change anyway.

    dd if=/dev/zero of=/data/swap bs=1M count=512
    mkswap /data/swap
    swapon /data/swap

    --
    Opportunity knocks. Karma hunts you down.
  35. One of the tricks.. by Sir+Pallas · · Score: 2, Interesting

    ..that I think is spiffy is using the partition I would normally use for /tmp as . Then, I mount a tmpfs of that size on /tmp. This makes a large performance improvement for anything that uses a lot of temp space, because everything /tmp would normally handle is done in RAM until RAM fills up, at which point we're back to using the disk.

  36. Swap ain't the issue by erikharrison · · Score: 3, Insightful

    For the kinds of complaints about Linux swap I've been seeing of late, it would be bogus to call swap the issue, really. People looking to eliminate swap entirely on desktop machines are cutting off the arm for the sake of a finger.

    The issue with swapping in a desktop system is that perception of system responsiveness is almost as important as real performance, and swapping in (actually, it's paging in, but that's semantics) causes high latecy. This is especially noticeable when returning to an idle machine. So we want to cut latency.

    People say "the kernel shouldn't swap unless it can't fit everything it needs in system memory." Duh! And it doesn't! It's swapping to increase the size of the file cache, a huge performance win. If the file cache gets too small (say, because this Wal-Mart PC only has 128 megs of RAM, and you've turned off swap, so Moz is eating it all) then you wind up with disk seeks for harddrive intensive applications, causing the same latency as swap.

    What's clear to me from these complaints is that the file cache isn't smart enough. People with lots of RAM want to cut down on all these disk reads - that's why they got gobs of RAM. (Ain't it funny that the same Linux heads who say that Linux makes a little machine fly also say that a desktop has no reason to have less than 512MB or 1GB of RAM). At the same time, smaller machines should still be supported, and even folks with gobs of RAM don't want to elimiate swap, otherwise disk bound apps suffer the same latency they're trying to eliminate.

    The Linux file cache seems too aggressive for most users. Ext2 loves a file cache like no other filesystem, and this probably influenced the design. If the file cache can be smarter about when to swap to grow itself, and when it should just be content to use up all available system memory, then lots of these latency issues can be fixed in a way which will scale across both hardware and multiple use environments.

  37. Re:We really need to repurpose the sticky bit... by cpghost · · Score: 2, Insightful

    Please don't touch the sticky bit semantics. They are still used on other Unix-like systems (though rarely) and having different meaning in Linux is just calling for trouble.

    A better way would be to use other file attributes. On FreeBSD you can use chflags(1) to set flags like arch, opaque, nodump, sappnd, schg, sunlnk, uappnd, uchg, uunlnk. It is IMHO always better to add more flags in a specific filesystem implementation, than to break backward compatibility without very good reason.

    --
    cpghost at Cordula's Web.
  38. Re:No more swap! by egomaniac · · Score: 2, Interesting

    Generational garbage collectors, such as the one used in the JVM, screw up swap. It seems like there is a conflict between what the OS is trying to do with swap and what the JVM is trying to do with GC. I would rather let the GC win in this so the application runs fast.

    You are absolutely correct that garbage collectors play hell with swap. It's pretty easy to understand why: to determine what is garbage and what is not, the garbage collector has to check every live object and see what they hold pointers to.

    Think about that one for a sec -- the garbage collector has to look at every single live object on the heap during every garbage collection pass. This means that any pages which were swapped out have to be fetched from disk, so you end up (usually) loading the entire heap back into memory during garbage collection.

    The aforementioned problem is true of all accurate garbage collectors. The other problem depends on the exact sort of garbage collector, but in general live objects are moved around in order to clean up holes in the heap (think of it like compacting a database). This can give you another "scan the entire heap" situation.

    The only real exception to this rule is that large data structures (such as the pixel data for an images) that do not contain pointers and thus do not have to be examined can remain swapped out if they aren't relocated during a particular garbage collection pass. The first page of the data structure must always be loaded no matter what, hence the "large" (really, multi-paged) disclaimer.

    An OS based on a GCed language such as Java will probably have to come up with some really innovative tricks for managing swap, or just do without.

    --
    ZFS: because love is never having to say fsck
  39. Swapping on other Unix systems by PhunkySchtuff · · Score: 2, Informative

    IMHO Swap is a good idea and here's why.
    I admin Solaris systems, and swap on Solaris is a fine thing indeed.
    You allocate a complete slice of a hard disk for swap, and you can then add and remove swap dynamically while the system is running. Need 1 GB more swap? Create an empty 1GB file, and add it as swap.
    What's more /tmp is mounted on swap. If you, say, have 1GB of swap space and chuck 512MB of stuff in /tmp, you've now got 512MB of swap left. Lots of Unix software dumps stuff in /tmp and, when there is available RAM, /tmp lives in RAM. This makes temp files very fast.
    Plus, the VM subsystem also deals with the file cache so on a Solaris system, you will see the amount of RAM used always around the 100% mark. No point in having RAM there unused, it costs too much. Use it as disk cache.
    In addition, when an application needs to be swapped out to disk, why bother writing to disk something that's already there - the application's code is marked as being paged out to disk and removed from RAM and when it's needed again, the code is fetched from the original binary that the application was loaded from.
    All in all, these kind of modifications to the VM subsystem mean that swap is good to have and can make systems faster with it than without.
    k:.

  40. Swap thrashing is a symptom, not a cause by majid · · Score: 4, Insightful

    A swapless system won't be faster for the same workload, usually the contrary, in fact, since lack of swap denies the system the opportunity to optimize RAM hit ratios. What a swapless system can do is force admission control on new processes in the system, thus enforcing a no-overcommit policy on RAM, and therefore increasing responsiveness at the expense of global throughput.

    Swap thrashing in a desktop environment is usually the sign of a workload that is too high for available memory, e.g. trying to run far too many apps simultaneously. No amount of OS smarts is going to compensate for overbooking RAM with too large a working set. The solution is to increase RAM or not run as many apps simultaneously.

    Swap thrashing in a server environment is usually the sign of improper server configuration. Naive administrators configure too many processes, thinking they will avoid a bottleneck if all server processes are busy, but all they achieve is turning RAM into the bottleneck rather than the server processes themselves. If you have a web server and configure Apache to have too many running processes, these processes will spend their time contending for RAM instead of doing useful work. Too many cooks spoil the broth. A swapless system would prevent excessive Apache processes from starting in the first place, thus alleviating the problem (at the expense of high error rates, which is probably not acceptable), but performance won't be anywhere as good as a system with swap and properly sized Apache process limits.

    Swap is not a panacea. It should not be used to protect against runaway processes (setrlimit is here for that). It is useful in absorbing sporadic spikes in traffic without causing denial of service, and to shunt away uselessly allocated virtual memory (ahem, memory leaks).

    As for the idea of putting swap on a RAMdisk, it is completely brain-dead (unless you have exotic memory arrangements such as NUMA) - the kernel is going to waste a lot of time copying memory from the active region to the ramdisk region and back. A straight swapless system will be preferable.

    There is no hard and fast rule for sizing swap, it depends on your workload, such as the average ratio of RSS to SIZE. The usual rule of thumb is between 1x and 2x main memory.

    1. Re:Swap thrashing is a symptom, not a cause by rfc1394 · · Score: 2, Interesting
      A swapless system won't be faster for the same workload, usually the contrary, in fact, since lack of swap denies the system the opportunity to optimize RAM hit ratios.
      Agreed. This is the real reason to have swap space, so you can run more applications than you have resources for. It also allows running applications to push ones that are not doing much out of the way while they are stalled (waiting for a resource) or otherwise not running (a process like, say, a database server that is sleeping until it gets a record or SQL request) and thus not use resources while not operating.
      As for the idea of putting swap on a RAMdisk, it is completely brain-dead (unless you have exotic memory arrangements such as NUMA) - the kernel is going to waste a lot of time copying memory from the active region to the ramdisk region and back. A straight swapless system will be preferable.
      On this I am going to have to disagree with you. If you have some swap, the system can move least-used pages out of the way as it runs out of primary memory or as it notices rarely-running processes that can be shunted off to release primary memory to processes that are running. Disabling swap altogether means that the system has to run out of resources, attempt to swap them out, discover it has no swap, then kill something or refuse to honor a request for more resources to make room. With swap, even if it's to a ramdisk, the system can remove processes at the high-water mark and if the hits aren't too high, it is conceivable that better performance might occur over a system that simply has no alternative but to hit the hard limit and run out of resources as opposed to crossing a soft limit and not ending up in a starvation condition.

      Under normal circumstances it would make sense that having all available memory would make more resources available than stealing some to make a virtual memory swap space, but as most operating sytems are designed to swap pages out as they are unneeded or when processes start to hit the high-water mark, the overhead of the swap manager running and being unable to do anything due to no swap at all just might be higher than the small amount of time needed to do some unnecessary copying of memory to swap out some rarely-used pages.

      Short of someone running a test on a machine with no swap at all vs. say a tiny amount of ram used as a ramdisk (say 5 meg on a 1 GB machine) it's probably an academic argument to say flat out that no swap will always provide better peformance than swap to ramdisk, especially if the kernel is designed to expect to be able to have swap around.

      If the kernel is designed to only swap out on resource shortages and not to optimize running processes as well, then swap to Ramdisk is a brain-dead operation. But I suspect the actual method of operation is a little more complicated than mere copy-on-resource-shortage, and thus it is conceivable that swap-to-ramdisk may provide better performance than no swap at all.

      Paul Robinson <Postmaster@paul.washington.dc.us>
      --
      The lessons of history teach us - if they teach us anything - that nobody learns the lessons that history teaches us.
  41. Reasons for swap... by emil · · Score: 4, Interesting

    I don't know if Linux works this way, but...

    1. The mmap() system call, which allows you to treat a file as an area of memory and manipulate it with pointers in C, oftentimes copies (portions of) the file into swap.
    2. Many systems, when you execute a binary obtained over NFS, will cache the binary in swap in hopes of preventing further transfers over the network.

    UNIX kernels have assumed the availability of swap for nearly 35 years. You cannot remove this major architecutural feature without unintended side effects.

  42. Swap Partitions by HeghmoH · · Score: 2, Interesting

    I haven't touched Linux for several years, although I used to do serious work on it.

    I take it from the tone of the discussion that Linux still uses separate swap partitions? Why? My main machine now runs OS X, which swaps into the filesystem, and that seems to work a lot better. The system can decide what it needs to use, and I don't have to make a decision. I recall that Linux supports swap to the filesystem, but it sounds like nobody actually uses this feature. I can somewhat understand a server using a swap partition, since the needs of a server would be more or less known in advance and I assume it's marginally faster, but I don't see any reason to use one on a desktop machine. Why is everybody still using dedicated swap partitions?

    --
    Mod down posts with a "Free Mac Mini/iPod" sig, they're spam!
    1. Re:Swap Partitions by Corydon76 · · Score: 4, Informative
      Because until relatively recently, Linux used a very slow filesystem by default: ext2fs. This is, for example, why ext2 filesystems are always mounted asynchronously, as attempting to wait for each disk operation to sync would slow down the system dramatically. You'd have to be concerned with swap not going to disk immediately (just moving into disk cache, waiting to be synced to disk, which can take up to 30 seconds!).

      Now with more advanced filesystems that can be mounted synchronously, using a swapfile is less of a problem -- it certainly could be done, but you still get a performance hit by having to manage a filesystem entry, rather than swapping to raw disk.

      BTW, a number of databases use raw disk for exactly this reason -- to avoid the performance hit of managing a filesystem. And yes, it will make a difference to the overall performance of your database.

  43. Slow I/O should not be cached indefinitely by swilver · · Score: 2, Interesting
    I have a Windows XP and a Linux box (2.6.4 kernel), both with 1 GB and both running without swap. The reason for this is simple; when I have my systems running a while doing nothing but serving files (slow downloads, or simply watching a big 2 hour movie), the machines will both be totally unresponsive when I get back to get some real work done; literally everyhing needs to be swapped back in because the machines use like 800 MB of it for cache buffers.

    Both OS's have filled their RAM with completely useless cached files (part of a 1 GB+ AVI for example, that I will most likely not be watching again for several months), swapping out all the programs I have running.

    Both OS's really need to learn how to deal with Slow I/O. If I/O is only being done at a rate that is a fraction of my harddisk speed (say 300-400 kB/sec), which occurs for stuff like watching a movie, playing music, serving an upload over DSL, then this data is really not worth caching for longer than a few minutes. Even if I do need it again, it will probably again be at just 300-400 kB/sec, something a harddisk can take care of quite comfortably.

    --Swilver

  44. Photoshop...etc by Thaidog · · Score: 2, Interesting

    Any machine that deals with large files will still need swap space... Photoshop when dealing with large image files...etc

    --

    ||| I still can't believe Parkay's not butter.

  45. nocache directive by Stephen+Samuel · · Score: 4, Interesting
    One of the errors that I see is Linux doesn't handle the read-once case very well.

    Once in a while I'll do something like 'grep -r "oops" /big/filetree'. The fact of the matter is that I'm probably only reading any of that data ONCE, and it's not going to all fit in memory anyways, so I don't even gain anything if I run the grep a second time.

    In a situation like that, I'd like to have some sort of 'nocache' directive that says 'Don't waste the cache with this'.

    Something else that might help would be to have some sort of 'minprog' directive which would tell the swapper that a certain amount of space is reserved for 'program' data (i.e. code (including shared libs) and data), -- and that that memory shouldn't be swapped out in favour of something otherwise being read from disk. I think that this might avoid the situation that I sometimes run into of a large program (mozilla/gimp) being unresponsive after I do some other disk-intensive task (like the aformentioned recursive grep).
    Things like the OS enforcing things like the RSS rlimit hints would also help. (I hadn't previously realized that it didn't).

    --
    Free Software: Like love, it grows best when given away.
  46. My experience by jmichaelg · · Score: 4, Interesting
    My first job as a sysadmin was on a Burroughs 7700. My employer sent me to a week long class on tuning the os to help the company deliver a turnkey app that met some performance specs. Didn't matter what I did to the working set/swap settings - the thing was pig slow. The older guys in the class who had admin experience on IBM 370's were constantly complaining that the Burroughs OS was doing a worse job deciding how to allocate RAM than they could and it was making them look bad because the boxes wouldn't deliver the throughput they had had with supposedly inferior IBM hardware. As you can imagine, it was a very contentious class.

    My boss started worrying that we weren't going to be able to deliver what the company had contracted to deliver. He was the antithesis of a PHB and so he sat down and in a few hours wrote a small driver to emulate the overall task the project had to accomplish. No detail, just broad brush emulation. He was able to demonstrate with a few lines of code that nothing we could do would hit the delivery spec. Burroughs responded by doubling the amount of RAM on the box as well as installing RAM that was twice as fast as what they had initially delivered. The combination enabled us to turn off swapping and deliver a working product.

    Fast forward to 2004 and I'm working on Excel spreadsheets that have 60-70 sheets in a workbook. Saving the book is a bitch - 15-20 second wait after I hit ctrl-S. Every so often, Excel just goes away as it performs a prophylactic background save just in case Excel dies. 15-20 second pauses because the software has become so bloated that saving a 2-3 meg document is an excuse to flog the poor drive into a seek frenzy. The drive, which was about 4 years old, finally gave up the ghost. Its replacement has an 8 meg cache separate from the 512meg Windows manages - that "little" 8 meg junk of RAM belongs to hard drive alone. Night and day performance difference. The Excel swap frenzies that were induced by a simple ctrl-s are gone. 3 meg documents save in under a second - just what you'd expect from a drive that has a transfer speed in excess of 60 mbytes/sec.

    My sense is that swap has always been a kludge. It's an attempt to squeeze more data into a machine that has only so much space. The working set graphs look pretty but they seldom describe what is happening day to day. Trading 2 nanosecond response for a 5 millisecond seek is seldom going to be a good trade. Bottom line from that OS class 35 years ago? Keep your working set size less than your physical memory and your machine will remain responsive. Just what the old IBM Geezers were saying in the first place.

  47. segregation Re:No more swap! by WolfWithoutAClause · · Score: 3, Interesting
    The aforementioned problem is true of all accurate garbage collectors.

    Whilst that's strictly true, some modern languages use generational garbage collectors that segregate objects in memory according to age. Only when an age group gets full do they sweep through an age group, and move any surviving objects up to the next age group.

    This heuristic works exceptionally well, and runs fantastically quickly, and triggers significant swapping hardly ever.

    There are some circumstances where it runs slowly, but in the worst case the performance is similar to simply doing a full garbage collection. These situations are pretty rare; objects generally segregate very well into young/old or young/middle aged/old categories- the vast majority of objects die very young.

    Sad isn't it.

    --

    -WolfWithoutAClause

    "Gravity is only a theory, not a fact!"
  48. Use a swapfile instead of a partition by MarcQuadra · · Score: 3, Informative

    Use a swapfile instead of a partition. 2.6 kernels cache the location of the file, so there's no performance hit for swap files compared to swap partitions. I'll give a quick HOWTO:

    1. decide how much you want (you can change it later, I have 128MB on all my boxes with over 512MB RAM). The example uses 128MB

    2. #dd bs=1M count=128 if=/dev/zero of=/var/swap
    3. mkswap /var/swap
    4. edit /etc/fstab for: /var/swap none swap sw 0 0
    5. swapon -a
    6. There is no step six!

    But the best way to know how much swap you need is to peek at #top every now and then, or #cat /proc/meminfo and see how much you're using when you've got the system strained, use twice that amount, not less than 128MB.

    --
    "Sometimes, I think Trent just needs a cup of hot chocolate and a blankie." -Tori Amos on Nine Inch Nails
  49. hmm by mAineAc · · Score: 2, Interesting

    After reading this I thought to look at my laptop. And what to my surprise I didn't have any swap. It turns out a couple of weeks ago I was playing with swsusp and it had corrupted my swap space and it wasn't loading. I never noticed but after the fact I realized some of the stuff that was going on was because of this. I tried openening a large pdf file and it was taking forever to load and seemed to almost lock up my system. Same thing happened when compiling a few programs and other things similar. I guess with 196 MB of Ram you want to have some swap :)

  50. Re:No more swap! by Unknown+Lamer · · Score: 2, Interesting

    A generational garbage collector does not always have to check every single live object. It allocates objects in different generations (e.g. a generation may be every 5M of memory allocated by the GC) and the newest generation is scanned first and older generations are only scanned if memory cannot be found in the nearer generations.

    This (mostly) alleviates the problem with straining swap because the GC is mostly scanning recently allocated memory that is probably still resident.

    --

    HAL 7000, fewer features than the HAL 9000, but just as homicidal!
  51. Re:Before you complain at linux... by BigBuckHunter · · Score: 2, Informative

    http://msdn.microsoft.com/library/default.asp?url= /library/en-us/dngenlib/html/msdn_ntvmm.asp
    Make sure to remove the space when you cut n paste

    BBH