Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
Linux Software

Tuning Linux VM swapping 324

Lank writes "Kernel developers started discussing the pros and cons of swapping to disk on the Linux Kernel mailing list. KernelTrap has coverage of the story on their homepage. Andrew Morton comments, 'My point is that decreasing the tendency of the kernel to swap stuff out is wrong. You really don't want hundreds of megabytes of BloatyApp's untouched memory floating about in the machine. Get it out on the disk, use the memory for something useful.' Personally, I just try to keep my memory usage below the physical memory in my machine, but I guess that's not always possible..."
This discussion has been archived. No new comments can be posted.

Tuning Linux VM swapping

Comments Filter:
  • by Trifthen ( 40989 ) on Friday April 30, 2004 @09:37AM (#9017866) Homepage

    Ah yes. It's all the fault of bloaty apps. Apps like database daemons and high-traffic httpd daemons. We've turned swapping off on our servers because we were sick of seeing almost a GB of cache/buffer memory, while it was swapping 500MB of shit to disk. Want a bloaty app? How about the linux Kernel? I love the thing, but Jesus Tapdancing Christ it would rather swap our starting DB process to disk, than free up the fucking buffers and cache. Is there something wrong with wanting it to give precedence to not swapping?

  • by redelm ( 54142 ) on Friday April 30, 2004 @09:38AM (#9017893) Homepage
    Ever since I've had a 32 MB machine (1997), I've not bothered to even set up a swap partition. On the rare occasions when I need swap, I'll create a swapfile. Sure it's slower, but swap is already hugely slow.

    With read-only & demand code-page loading and copy-on-write even bloatware really doesn't eat memory. And bloatware has to be frequently restarted to recover the memory it leaks.

    Sure, there are some jobs that needs swap -- lots of seldom used memory pages.

    But not mine. I prefer to save myself the complexity and performance headaches.

  • by Anonymous Coward on Friday April 30, 2004 @09:39AM (#9017898)
    Yeah there is nothing I love more than coming to an idle X console session on box I haven't touched in a while and watching it grind itself into oblivion because everything has been paged out.
  • VM you say? (Score:3, Insightful)

    by freeze128 ( 544774 ) on Friday April 30, 2004 @09:40AM (#9017902)
    At what point does VM stop meaning Virtual Machine and start meaning Virtual Memory.

    Or is it just the Virtual "M"?
  • by cloudmaster ( 10662 ) on Friday April 30, 2004 @09:47AM (#9017968) Homepage Journal
    Disk corruption is handled seperately. If you don't trust your drive controller, then you should spend some time fixing up the disk writing susbsytem. Disk writes should be guaranteed if they say they're succesful, except in the case of a power failure. At that point, swap integrity doesn't matter anyway. :)
  • by Anonymous Coward on Friday April 30, 2004 @10:01AM (#9018094)
    /proc/sys/vm/swappiness
  • Many people don't realize how smart modern page caches are designed to speed their system. Linux, MacOS X, Win2K+, etc. all boast aggressive page caches that make loading applications from disk more efficient.

    Without a swap file, the kernel has no place to stick memory segments that are rarely used. They stay in resident memory la-la land until the process is terminated. Those segments add up over time and erode the memory available to the page cache.

    Page caches are wonderful. When you load an application (like Firefox [mozilla.org]), you're not just getting the web browser. You're firing up a large chain of shared objects/DLLs that support the widgets, I/O, and components of the application. All of these components must be read into memory anyhow for program operation, so the kernel tends to just leave it in there for future use (the page cache).

    When you shutdown Firefox, you're also releasing the necessity of those libraries (provided nothing else is using them). Those libraries also remove themselves from memory. If you load another application (like Thunderbird [mozilla.org]) that uses the same type of libraries, the kernel will not have to go to disk in order to fetch those libraries. It will instead opt for the page cache contents.

    Turning off the swap file in the historic era of VM infancy was the best way to remove the hard drive bottleneck from system. The operating systems of yester-year did not have good page cache schemes that took advantage of all that unused memory. It is a little different now.

    Applications are so modularized that they are broken up into a billions of smaller libraries so that code can be shared. This increases memory efficiency by keeping a shared library resident for multiple processes. These libraries are frequently accessed, more often than many people realized. Getting THOSE into memory is better than making sure my 500+ Linux applications stay resident.
    $ cat /proc/meminfo
    total: used: free: shared: buffers: cached:
    Mem: 1055653888 1036296192 19357696 0 70488064 892309504
    Swap: 542367744 235892736 306475008
    Notice that on a web server with 1GB of RAM the Linux kernel is still putting things out to swap. These processes that stay asleep for long periods of time do not need to waste the memory that page cache is currently using (892309504 bytes or 753.7MB). What would be stored in that 753.7MB of memory? The database that drives the website (instead of having to seek the disk). The entire web page hierarchy used to display pages on the web site. All the scripts that are used to display dynamic content on the web site (etc. etc.)

    Now, if we subtracted from the page cache the amount of memory that was stored in the swap file, we would have over 200MB less that we could keep cached in memory. That could be an entire database that the kernel would then waste needless CPU cycles to fetch from disk.

    The only advantage to turning off a swap file on these modern machines would be for a machine that runs only a select few applications, and not having a lot of processes in the background doing things.
  • by julesh ( 229690 ) on Friday April 30, 2004 @10:09AM (#9018165)
    True, but in most real applications the latency of the disk is the real cost of using the system -- you don't tend to do sustained transfers from swap to CPU. Most memory accesses use a small amount from a single page and then skip to using another. Modern programs tend to have very bad locality of reference.

  • Re:Other reasons (Score:3, Insightful)

    by Just Some Guy ( 3352 ) <kirk+slashdot@strauser.com> on Friday April 30, 2004 @10:33AM (#9018459) Homepage Journal
    At least, that's the rationale I've read behind OS X's strategy of swapping things out long before all physical memory is used (and of keeping a pool of zeroed memory pages ready to fulfill most requests).

    That's what FreeBSD's been doing for years, and for a long time kernel hackers spoke in awe of the much-vaunted FreeBSD VMM. Now that Linux has implemented a similar strategy, everyone's freaking out like it's some new ego trip that noone's ever tried before.

    The "new" system is what other OSes have been doing for years, and it makes sense. Sometimes it's better to learn why such big changes are made than to gripe on Slashdot that things aren't the way they used to be.

  • by trezor ( 555230 ) on Friday April 30, 2004 @10:37AM (#9018501) Homepage

    Whatever swapping scheme is used in Windows, I do not know, and I don't care what it's called either.

    What I can't despice, is the fact that I got >300MB free physical memory, and 20MB of the kernel is still swapped. Result? Do this, do that (any minor thing) and you have to wait for it to swap in.

    In the end, I have never ever seen a Windows-system without a partially swapped kernel, even with tons of free RAM available.

    This is just plain stupid, or is there some sort of "smart" explanation for this?

    I, for once, would hate having to turn off virtual memory, just to have the system kernel loaded at all times.... And GOD BE DAMNED if Linux takes the stame stupid design-decision.

  • Swapping back in. (Score:4, Insightful)

    by AlecC ( 512609 ) <aleccawley@gmail.com> on Friday April 30, 2004 @10:42AM (#9018548)
    I ffel that there should be some tunable propensity for applications to swap back in. Generally speaking, disk cache is most effective over a pretty short timescale - seconds or a few minutes. It is vey effective with a multi-pass compiler to cache the output of one pass so it can be read in by the next. But this sort of thing has a relatively narrow window.

    So way you want to do is:
    • Apps which haven't been used for a time get swapped out.
    • Cached blocks decay with time, decaying faster if the system idles a lot (presumably the big jobs have stopped), slower if the system is very busy (more likely there is something to re-use cache)
    • As cache blocks decay out, BloatyApp is gradually sucked back in. In Gui Environments, the Window Manager flags the pressure to return as proportional to (say) the number of pixels of visible screen it occupies. Of course, having swapped out once, if it never restarts, you can throw it out second time if you need your cache back.

    So if the guy goes to leaving a big make running, it gradually pushed the big apps out while it runs. But if the big make completes, the apps start crawling slowly back in. If it hasn't finished when he comes back from lunch, he probably wants it to carry on running the make: since the CPU is at 100% load, he is probably not surprised it is sluggish.
  • by chrysalis ( 50680 ) on Friday April 30, 2004 @11:03AM (#9018784) Homepage
    http://00f.net/item/14/
    describe why swapping is _good_.

  • Re:God no... (Score:1, Insightful)

    by Anonymous Coward on Friday April 30, 2004 @11:14AM (#9018908)
    One of the best methods I've heard of for swapping stuff out is to be aggressive about it but not complete the process; that is, the VM manager can write hundreds of pages to disk but not remove them from memory. Then if another application suddenly needs a lot of memory, it can very quickly free up the memory without the delay imposed by swapping it out. Or if the "swapped out" application needs to use its memory, the pages on disk can be quickly marked as invalid and pages in memory used instead.
  • by gkelman ( 665809 ) on Friday April 30, 2004 @11:26AM (#9019012) Homepage Journal
    Yeah there is nothing I love more than coming to an idle X console session on box I haven't touched in a while and watching it grind itself into oblivion because everything has been paged out.

    What's wrong with that? The machine shouldn't sit and be ready for every possibility. If you've not logged in in X hours, it shouldn't expect you to suddenly log in. It grinds for a minute or so, and then it's fast again cos it's got what you want to use in RAM. Which is the whole point of swap, stuff you're not using gets paged out.
  • Linux does (can) do this: see tmpfs [ibm.com].
  • Keep two copies (Score:3, Insightful)

    by kasperd ( 592156 ) on Friday April 30, 2004 @11:42AM (#9019184) Homepage Journal
    Swapping out data before you need the free RAM would be a great idea if you kept two copies. One copy on disk and one copy in RAM. In fact it would be fine if the system swapped out 90% or more of the process memory this way. There will now be three different cases to think about.
    1. The process needs to read the page - no problem, one copy is in RAM just read it, and keep both copies.
    2. The process needs to write the page - no problem, you can modify the copy in RAM and discard the copy on disk. Notice that discarding the copy on disk doesn't require any disk access, as the list of swap allocations will typically be in RAM (it is much smaller than the swapspace).
    3. You actually need memory - no problem, discard a not recently used RAM page, you still have a copy on disk.
    The only problem is, that you need to make the page readonly, so you can trap the write and discard the on disk copy. In other words don't do this for pages that are frequently changed. But usually you don't have many pages that are frequently changed, and you certainly don't want to swap out those you have. And should you occationally happen to swap out one, it is not really a major problem. It will cost you a pagefault, but no disk I/O. And a pagefault is compared to a disk I/O. A system that behaves like I have described here would use a lot more space than Linux typically does, but still it should be faster. I wonder why this isn't done more often, it is not like the idea hasn't been known for years.

    Another problem that many have noticed, and that isn't easy to deal with, is heavy diskaccess causing the cache to grow and stuff getting swapped out. Yes even some Linux versions suffer from this problem. A Red Hat 9 system I had running for months was really slow in the morning, because all the programs had been swapped out while cron jobs where running during the night. But you never know when it is a good idea to swap the stuff out and when it is not. When the disk access is going on, the process page might not have been used for hours. But still you might want it to be kept in RAM. File pages that have been accessed just once shouldn't be kept in cache for long time. But of course you shouldn't remove them unless the memory was needed for something else. Removing the pages too early is also bad, because you wouldn't notice, that this was really a page that was going to be accessed frequently. Some people are fanatic, and don't want process pages to ever get swapped out to make room for cache. That isn't a good idea either. You can really have process pages that may not be needed even once, do you want such a page to be kept in ram for months just in case? And notice how disabling swap is not going to solve the problem. You still have to think about memory mapped files, that in many ways must be treated like anonymous mappings.
  • Re:God no... (Score:3, Insightful)

    by Fjord ( 99230 ) on Friday April 30, 2004 @11:43AM (#9019196) Homepage Journal
    The thig I hate about 2000 is that it seems to hold on to this disk cache RAM for dear life. Eventually, if you hibernate/unhibernate, or just don't turn off the computer, you swap for everything. It's a real pain in my assass well. I've disabled virtual memory on my windows machine at home (at work, I run too much to do so), just because it's not really needed and makes it much faster.
  • by ChaosDiscord ( 4913 ) on Friday April 30, 2004 @11:57AM (#9019374) Homepage Journal
    Personally, I just try to keep my memory usage below the physical memory in my machine, but I guess that's not always possible...

    I've seen a number of posts echoing this point, overlooking one of the key reasons for swapping. It's not just because you're out of memeory for applications, it's because sometimes there are better things to be doing with your memory. Mainstream operating systems use otherwise unused memory to cache disk access, dramatically speading things up. If you've got an process that hasn't been run for a a while it may actually be more efficient to swap it to disk. This frees up memory to cache data that may be being hit quite frequently. inetd hasn't been needed for a while? Swap it out so that your disk cache is larger, benefitting your heavily used web server.

    To be fair, when to make that trade off is very tricky and will never work perfectly 100% of the time. Inevitably you'll occasionally be burned by a bad decision. But there are real benefits. The real question is not how to turn it off, the question is how to improve it and perhaps how to allow users to tune it for their needs.

  • Re:God no... (Score:3, Insightful)

    by Darth Daver ( 193621 ) on Friday April 30, 2004 @01:01PM (#9020105)
    I have been using Windows for 12 years (and Linux for 10), and this is the first time I have heard of the obscure registry hacks you just listed. Besides, I thought Windows users argue they should not have to find, learn or research anything at all. It should just work, right?

    When I just searched for '/proc linux vm swap' in Google, /proc/sys/vm/swappiness was in the fourth hit from the top. There, that wasn't so hard, was it?

    I can tell you one thing. I would rather poke around the /proc filesystem than wander through the Windows registry any day.

    By the way, if you are not shutting your XP system down often, you must not be rebooting for the security patches, and that can be a problem for everyone. (Yes, I am aware that Microsoft has gone to a monthly release cycle now while simultaneously claiming to release within hours versus the weeks they claim FOSS takes.)
  • Re:The big issue (Score:2, Insightful)

    by 10101001 10101001 ( 732688 ) on Friday April 30, 2004 @02:16PM (#9020959) Journal
    You're right that having duplicate functions in different shared libraries means opposing apps real use of ram is larger than if they both used the same shared library or their shared libraries used a common lower library, but I think you're overlooking another point. Whenever there's a page of a library or a process' code that is going to be swapped out, it can simply be dumped. That means unoften used sections of different shared libraries aren't actually in memory. Also, because the libraries are already on the HD there's no swapout that occurs (though obviously swapin can occur). So the wasted bloat of most libraries matters little. And the often used stuff, which it does take up more ram than if it were unified is still a very miniscule section of ram usage (even at 8MB for qt and 2MB for gtk+ (plus another 1MB for glib and gdk) which cover all gtk+ and qt apps, you're using a amazingly small percentage of any modern system's ram (less than 9% of 128MB); add all the shared libs and you're still not using more than around 32MB which while large provides a massive amount of services for all sorts of apps which would otherwise be much larger). So, unification is good, but it's not nearly the worry that you seem to believe it is.
  • by shaitand ( 626655 ) * on Friday April 30, 2004 @06:44PM (#9023669) Journal
    Sorry but this is not a complex equation and I think these guys are getting wrapped up in too many details and missing the big picture.

    The harddrive is really, really, really, really fscking slow. In comparison Ram is really really really fast. As a result, you want to interact with the hard drive as little as you possibly can, and interact with ram instead as much as you possibly can (the only thing which beats that is interacting with only the cpu registers and avoiding ram and harddrive altogether).

    As is, linux doesn't even begin touching the disk until there is only enough ram left to turn on VM. Now this has a negative impact when that limit is reached because there is overhead turning it on.. this impact is negligable and tweakable since you can wait and see if you hitting the limit, add more memory, see again and reevaluate until you simply aren't swapping. This is a good thing.

    One of the worst things windows does is swap constantly. In fact beyond a certain point (read enough ram to run an XP desktop) the system swaps MORE if you have more ram. You boot the system with all uneeded services turned off and no startup processes and all the eyecandy turned off. And you've got 4gb of ram in the system, guess what, it's already using VM.

    Maybe VM management itself could be tweaked more, but it certainly shouldn't be used unless it absolutely has to (and if you don't have enough ram and it has to all the time then it's not like you suffer that performance hit more than once).

    The only exception to this I've found is a linux desktop running kde or gnome with about 256mb of ram, at that point the numbers seem to work out just about right(or wrong I should say) and the system is constantly turning VM on and off, encountering the performance hit again and again and again, with pretty much every operation you perform.

Intel CPUs are not defective, they just act that way. -- Henry Spencer

Working...