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."
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.
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
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.
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.
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.
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.
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
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.
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
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.
This might not be such a funny comment considering that why they swap improves performance is
"well it is a magical property of swap space, because extra RAM doesn't allow you to replace unused memory with often used memory. The theory holds true no matter how much RAM you have. Swap can improve performance. It can be trivially demonstrated."
Wouldn't having a swap drive in ram improve the overall performance of having a swap drive and still keep the above true?
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.
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.
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.
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
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?
For most home users who can get 200+ GB of disk relatively inexpensively what's the big deal about giving up (say) 1GB for swap?
There's also the use that if for some reason a system panics (hey, it happens) you have a place for the kernel to dump to. This can be valuable in helping debug what happened with a backtrace.
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.
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.....).
/data/swap /data/swap
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
swapon
Opportunity knocks. Karma hunts you down.
..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.
They are not theorems, but conjectures. A theory and a conjecture are not the same thing. No one to date as posted a theory.
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?
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.
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
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.
Oolite: Elite-like game. For Mac, Linux and Windows
I don't know if Linux works this way, but...
UNIX kernels have assumed the availability of swap for nearly 35 years. You cannot remove this major architecutural feature without unintended side effects.
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?
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.
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!
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
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.
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.
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.
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!"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 :)
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!
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.
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.