Joined: 19 Apr 2002
Location: Dallas, Tx USA
|Posted: Wed Jun 18, 2003 3:21 am Post subject: [FAQ]SA: How much swap space do I need?
|First, let's be clear on what "swap space" is to begin with:
Swap space: Disk space used to increase the amount of virtual memory a system has.
Virtual memory: Real memory (also known as RAM) plus disk "memory" (swap space). It's the amount of memory the system thinks it has. (Whereas real memory is how much it really has.)
Swap space (and its clone-brother paging space(see note 1)) were invented very early in the history of computers for one simple reason. RAM was hideously expensive.
Anyone who has been around computers a long time (15, 20 years or more) can tell you horror stories of how much money they paid for what seems like tiny, even insignificant amounts of memory today.
RAM used to be one of the major roadblocks to having a fast, high-performance system, simply because only the filthy rich could afford enough of it!
Here's what it's used for:
The system starts getting short on memory. You've opened up more Web pages, you've started a major game -- something happened that it needs more memory for, and it hasn't got it.
It starts looking through all the memory, looking for stuff that hasn't been used for quite a while. (If you care, it organizes things into chunks of memory called "pages" (which is where the name "page space" came from) that are usually 4KB (4,096 bytes). If you're not designing a paging/swapping subsystem, don't worry about the details.)
It takes each page that it considers a reasonable candidate and writes it out to disk (the swap space), and then frees up that space so it can be given to whatever the new thing is that wants some RAM.
Think of it this way: Real RAM is like your living room. Most of the things in it you use fairly often (even if "using" is just looking at it). Swap space is like the garage or a back bedroom which you're using as an overgrown closet. You wouldn't want to put the television there, because going to get it every time you want to watch something would be too much hassle, but you might put the manual for that TV in there, because you're probably not going to use it but once every year or two. The extra effort to get it is worth not having it clutter up your living room.
You have the same kind of tradeoffs with swap space. Disk is roughly a million times slower than memory. If you write something out to disk, if and when you ever need it again, you're going to have to wait a loooonnng time to get it. That's still better than having your new program crash because you're out of memory.
The old rule of thumb was "twice real memory". If you had 32 MB of RAM, for example, you would have 64 MB of swap space.
A somewhat better rule, but one that required you know a lot about what your machine was going to run, was to figure out how much virtual memory your system needed (let's say 512 MB), and just subtract how much real memory you had. If you had 128 MB of real memory, you'd have 384 MB (512 - 128) of swap space. If you were rich, and had 256 MB of RAM, you'd only need 256 MB of swap space.
Ok, all that was true back in the days when RAM was outrageously expensive, and augmenting the tiny bit of RAM you could afford with disk was almost a necessity. What about now, when RAM is cheap (and getting cheaper by the day)?
The rule of "figure out how much virtual memory you need" rule still works. The difference is, there's seldom any reason to not make it ALL real memory. So really, for many people, the answer to "How much swap space do I need?" is "None. Buy more RAM if you need it."
If you want to sound like one of the old pros, you could always allocate twice as much swap as you have real memory, and mutter about how "Yeah, I know RAM is cheap, but I don't like taking chances..."
Finally, I know there are some people out there who are wailing "Quit explaining at me and just tell me how much to use! Please!". Take comfort, oh tortured soul, here's a table for you.
For each entry, the first amount is how much RAM you have, the second is how much swap space you should have.
16 MB or less. Subtract your ram from 32 MB and allocate that much (e.g 8 MB would be (32 - 8) 24 MB of swap. Do not try to run X. (Allocate enough to give you 64 MB of virtual if you want to run X, but you'll be sooorrryy!)
17-63 MB -- subtract RAM from 64 MB. You can try X, but no guarantees. Subtract RAM from 96 if you want to be certain X will run. The higher the ratio of swap to RAM, the more sluggish and jerky your system will be.
64-128 MB -- Now we're starting to get to decent-sized systems. The "twice real memory" rule works well in this range. If you have 128 MB of RAM, allocate 256 MB of swap.
129-1024 MB -- Unless you have a special need (or are running out of virtual memory), shade down from 2-to-1 at the low end to 1-to-1 on the high end. You might even consider using 256 MB across the whole range. In a sense, you're making your "swap space" more memory. (This is good, in case you had any doubts.)
1024 MB+ -- At this time (6/17/03), if you're buying a system with over 1 GB of RAM, you really ought to know what you're doing. In this range, you're either building a server-class machine for something like Oracle (where 1 GB of memory is like 32 MB for a personal machine), or you're just greedy. (That's not knocking greedy, by the way -- I've got 1GB on my larger desktop machine.) Either way, if you're using this to tell you how much swap you need, you either need a different career, or you have a sick, twisted sense of humor. (Like most of the rest of us.<g>)
At anything above 256 MB, you're not likely to need more swap space. If anything, you probably need less, because more and more of your total memory needs are being met with real memory. (That "256 MB" number is valid in June of 2003. As time marches on, what constitues "plenty for everyday purposes" is going to climb. Adjust to your own time zone.)
For the purists:
Paging space is technically different from swap space.
To page something out, you steal individual pages, but the process as a whole stays in memory. (The frequently-used parts of the application stay in memory, in other words, while the parts that haven't been used in a long time get paged out.
To swap something out, the kernel says "I'm hurting for memory, and you're a memory hog. You're out of here until I can afford you better."
Despite the name, to the best of my knowledge, Linux does only paging, not swapping.
For the non-purists: Don't worry about this, I just put it in because I knew somebody would hassle me about the difference if I didn't prove I knew it. It's not likely to be important to you any time soon.