How Big Should My Swap Partition Be?
For the last 10 years, I have been asking people more knowledgeable than I, "How big should my swap be?" and the answer has always been "Just set it to twice your RAM and forget about it." In the old days, it wasn't much to think about — 128 megs of RAM means 256 megs of swap. Now that I have 4 gigs of RAM in my laptop, I find myself wondering, "Is 8 gigs of swap really necessary?" How much swap does the average desktop user really need? Does the whole "twice your RAM" rule still apply? If so, for how much longer will it likely apply? Or will it always apply? Or have I been consistently misinformed over the last 10 years?
'Is 8 gigs of swap really necessary?'
With a 750GB hard drive selling under $100, what has changed?
... and 8GB of space is still trivial with a 750GB hard drive.
Yeah, your 256MB of space was trivial when you had a 30GB hard drive
That said, I'll forward you some common information on paging.
Linux and other Unix-like operating systems use the term "swap" to describe both the act of moving memory pages between RAM and disk, and the region of a disk the pages are stored on. It is common to use a whole partition of a hard disk for swapping. However, with the 2.6 Linux kernel, swap files are just as fast as swap partitions, although Red Hat recommends using a swap partition. The administrative flexibility of swap files outweighs that of partitions; since modern high capacity hard drives can remap physical sectors, no partition is guaranteed to be contiguous.
I'm no expert but the short answer to this is to look at your swap partition as your extended virtual memory. By saying that your swap partition should be 2x your main memory is like saying that you will never use 3x of what your main memory is (in this case 12GB). While that rule of thumb is a good one, there may in fact be applications today in the graphics and processing world that require insane amounts of memory. While Firefox is probably never going to reach that critical mass (nor will most average programs) it's probable that a few years from now it will be common place. I know it's insane to think of but 'ought to be enough for anybody' is not the phrase you want to throw around in the digital information world.
It's those days when I'm playing Warcraft through wine, listening to streaming radio through Amarok, have 20 windows open behind it, idling a LAMP server for my development projects, running a vent client, some form of news aggregater, pidgin & an e-mail client hooked up to several POP3/IMAP accounts that I am happy I erred on the side of a whole ton of swap space.
My work here is dung.
The origin of the 'twice real RAM' came in the early days of windows, in which windows could not use any swap unless you had at least as much as real RAM. That's been gone for ages now - and you should actively avoid too much swap.
If you allocate, say, 8G of swap for 4G of RAM, most of the time almost all of it will go unused. If it actually /is/ used, your machine has probably spent the past hour or so frantically swapping to try to accomidate this 12G request; ie, your system is completely unresponsive due to every program being mostly swapped out. The additional swap merely delays the out of memory event, and in the meantime you can't control the machine.
Swap is still useful for holding data that's not part of the working set, in order to free memory for cache; but this shouldn't be very much RAM (256-512mb should be enough). It's also useful for software suspend on linux - if you have a laptop, make it a little bit larger than physical RAM. And always have /some/ - linux's memory manager doesn't like having none.
If you were running Oracle - here is what they recommend:
RAM -> Swap Space
1 GB - 2 GB -> 1.5 times the size of RAM
2 GB - 8 GB -> Equal to the size of RAM
more than 8GB -> 0.75 times the size of RAM
I don't know if this would carry across to general computing - it seems to me if it's enough for an Oracle RDBMS server, it ought to do it for most things.
It's hard to believe that's how Micronians are made. Why don't we see it right now by having you both kiss one another?
Reading through OpenBSD's FAQ:
"The 'b' partition of your root drive automatically becomes your system swap partition. Many people follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. This rule is nonsense. On a modern system, that's a LOT of swap, most people prefer that their systems never swap. You don't want your system to ever run out of RAM+swap, but you usually would rather have enough RAM in the system so it doesn't need to swap. If you are using a flash device for disk, you probably want no swap partition at all. Use what is appropriate for your needs. If you guess wrong, you can add another swap partition in /etc/fstab or swap to a file later."
HTH.
Just make a note of your virtual memory use every hour or so (or just whenever you remember) for a few days/weeks. Then just give yourself maybe 2-3 times the peak usage.
I imagine different people will need different amounts of swap space, so use a size that's right for you.
If you're debugging your kernel or are helping people to debug your kernel, and are generating crashdumps either manually or as a result of kernel panic, you need your swap to be twice as big as the memory so it all fits comfortably (You can probably get away with X times bigger, where 1X2, but 2 is a safe number).
To my understanding that's always been the reason for the rule of thumb about doubling the memory. If you can afford the disk, go for it, because you never know when you might hit a panic and need crashdumps. If you are in a live environment and are sure you will never, ever need or even want crash dumps, and the disk space is at a premium, you can size it based on need.
Another thing to keep in mind is that as you have more ram, you have more pages, and the whole point of swap is to get pages to disk as well in case you need to free up physical ram quickly.
-bugg
If you need more than like 512M of swap, you need more physical RAM.
Seriously though, maybe like 10% of your physical RAM; but if you reach a point where you need to access a lot of swap in a short time (say a graphics intensive program, or a Web browser that has 2 gigs relevant data in swap), your machine will slow to a crawl.
This is why I use like 128M swap and swapd on Ubuntu.
Support my political activism on Patreon.
In the end, it depends. If you are running several memory intensive applications you're going to want more swap space. At the very least, you should have as much as your RAM because when you hibernate it takes all the pages in your RAM and puts it into the swap space before powering off your computer. I wrote about this a while ago: http://www.bytetrap.com/blog/2008/06/02/swap-space-linux/
2X RAM was the standard rule of thumb at Sun, for SunOS long before Windows was around.
If anything, Microsoft ripped it off from Sun.
How big should my swap be?
It really depends on what you're planning on doing with the machine:
It really depends on what you're doing. A simple firewalling machine will never need to swap. Low trafic websites and mailservers will probably hardly ever need it.
Also, you can always add swap later if you resize another partition. It really isn't that much of an issue, so pick a value and adjust according to your needs.
Well, I do occasionally need more than 2GB of RAM, without there being a memory leak. I've been running GIS programs, an IDE, a couple of RDBMSs, and then I fire up the old compression program...
Which brings me to my point. The question "how much swap do I need" is probably meaningless, even for a given amount of memory. There are people who find 2GB with no swap fine, and others, like me, who probably could get by with 2GB of RAM and maybe 512MB of swap, and others who might need more.
I think the 2x RAM rule of thumb has one virtue: excepting certain exotic kinds of systems, it's fairly safe that anybody who finds themselves needing more than that is probably feeling a world of pain that can only be fixed by getting more RAM. On the other hand, in most cases 2x RAM amounts to a trivial amount of disk. Probably most people could get by with 25% of RAM, but the value of thinking about whether that is true for you is very likely less than the cost of the disk space.
Common sense applies. If you have some kind of scientific computing device with a gazillion bytes of RAM, your swap requirements might not be related to your maximum RAM requirements at all. If you're running some kind of operating system that launches a bunch of rarely used garbage, you probably ought to think about your swap. I had awful problems with Vista until I figured out the page file Windows created had something like eight thousand fragments. I was actually better off getting rid of the page file
Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
For built-in suspend this is true. TuxOnIce offers, among other things, suspend-to-file support which eliminates the need to keep gobs of swap around if you don't want to.
25% Funny, 25% Insightful, 25% Informative, 25% Troll
I have made hundreds of swap partitions for OS X, AU/X, Windows, Schmindows, and just about every flavor of Unix I came across.
I would advise...
For Windows, load Process Explorer, and look at the Commit Change Peak RAM. Nice...
Now load a browser, a word processor, and Acrobat. OR Load the game you want to play.
Make the partition the size of that peak RAM+10%.
Make the swap size, the larger of the system cache or the minimum peak commit change. ( There is a brilliant trick here, but Id have to kill you...)
System 1:
1024MB ram.
Peak is 70%.
Swap partition is : 1916Mb, 64K Clusters.
Swap file size is : 512~1668Mb
Swap file size on OS Partition is 2Mb.
( Someone warned me about this, and I actually listened. Sure has helped when imaging drives )
More Later...( It gets trickier for smaller ram values...) I am working on a 512MB system, a 384MB system, and a 256MB system.
The point of swap space isn't to kick in when you run out of physical memory. The point of swap space is to allow the kernel to make use the most efficient use of your RAM, by swapping out the contents of infrequently accessed memory pages, and putting that memory to better use, like caching frequently accessed disk blocks.
If you have no swap space at all, any memory pages that your processes are hardly using have to stick around in memory forever, even if you'd get better performance by swapping the contents out and using it for caching disk blocks. So seriously, you should add some swap space, at least as much as your RAM.
Are you adequate?
8GB swap on a 120GB drive is 7%, not .07%. On a 200GB drive, it's 4%, not .04%, etc.
SirWired
If you're running Windows, use perfmon and see what the usage is under heavy load and scale accordingly.
tip: /proc/sys/vm/vfs_cache_pressure'.
Lower the amount of RAM Linux uses by changing vfs_cache_pressure to > 100. This will make the kernel dedicate less RAM for caching dirents (directory entries) for quicker lookups. For instance, to cut the amount of directory caching in half, double the pressure by doing:
'echo 200 >
HTH
If I mod you up, it doesn't necessarily mean I agree with what you've said, sorry.
Guess how many times I've thanked 8 lb 6 oz baby Jesus that I had the foresight to separate the two?
My guess: At LEAST three. :-)
I have three partitions on my system:
Home stores all my stuff, /usr/local stores all the stuff I download and build from source, and / is the stuff the distribution I use (currently Slackware 12.1) gets to muck with.
When I want a new distro, I can nuke and pave / with impunity, and depending on the age of things in /usr/local, they may need to be recompiled, and that's about all I need. Every now and then, /home and /usr/local get moved to a new, bigger drive, which is a lengthy, but fairly painless process. I don't clean out; I can't justify spending hours figuring out what I can purge and what I can't when storage is so cheap. I just buy a bigger drive, and the old smaller one becomes the new /. If the old system drive fails, it's no biggie. The new one gets its critical files backed up. If I lose it, there will be some pain, but I keep the "If I lose these files, I'd rather just die" stuff burned to disk, copied to my virtual server 1000 miles away, and on my USB keychain drive.
Multiple partitions FTW.
Arguably RH is the authority on the subject... See their documentation here.
-m
http://www.invisik.com
They have this technology already
http://www.tomshardware.com/reviews/gigabyte,1111.html
Can't find the other product that actually fits in 5.25 inch drive bay, but it was 10x's the cost a few years ago.
I picked up 4 of these cards for $60 on ebay 3 years ago, and then sold on ebay for twice that each.
However these cards have battery backup, but this could be removed. =]
Oracle has very specific requirements/recommendations:
Our organization just bought 4 database servers with 32 Gb of RAM each. I personally setup and installed the servers. I told the DBA:
The DBA agreed with this, and we went with 8 Gb of swap. Haven't had any problems with the server or DB applications for more than 6 months. It is the most heavily utilized server in the entire organization.
For a laptop, I would set the swap to equal or more of the RAM, only if you want to suspend to swap. Depending on the applications, I would say at least half the amount of RAM to double the amount of RAM, within reason. If you have 8 Gb of RAM on a workstation, you probably do not need 16 Gb of swap for everyday use.
/^([Ss]ame [Bb]at (time, |channel.)){2}$/
Either your professor is an idiot, or you had no understanding what (s)he was talking about. /some/ swap is helpful because often you have data in RAM that you are not actually using, but is not file backed, so you can swap it out to disk and use that memory for something useful, like disk cache for your active set.
Swap is not the reason for paging. Memory fragmentation between programs is the reason for paging. Saying a program can't allocate memory because it has bumped into the next one, when there is a large amount on the other side is insanity. This is exactly like saying you can't write to a file because there is another file right after it on the hard drive. Paging has the nice side benefit of efficient swap (swapping out pages instead of entire processes).
Having
Also, all of those addressing steps are only needed on a full cache/page miss. If this is happening often, your system is thrashing. Think spending 10,000 times the time waiting for page loads than doing address look-ups, or more.
In any case, suspend to disk requires at least as much swap as you have RAM to store your suspend image.
Swapping requires virtual memory. The converse is false.
All this scary PDE, PTE and other TLB stuff is what happens when a virtual address is converted to a physical address. That has nothing to do with swapping or paging.
Now, you cannot seriously consider abandoning virtual memory and all that comes with it (inter-process protection, kernel protection from user-space errors amongst others), can you?
has a dynamically grown swap file currently at 64 MB.
I have 8 GB of RAM and never page out even when I run dozens of memory hungry apps (photoshop, nikon capture etc).
The general rule is if you are swapping pages out when running typical apps you use daily, get more RAM.
As the island of our knowledge grows, so does the shore of our ignorance.
Actually stuff stored in RAM isn't safe either. If you remember a couple of months back an article was posted were researchers were able to recover contents from RAM more than 10 minutes after power was shut off. The only way it's safe is if you write something to all locations in RAM. Additionally you wouldn't gain significant speed. On a server most of your stuff is already in RAM when the system is running. On a desktop you would be better off prefetching, 1 GiB RAM costs around 300 times what 1 GiB HD space costs (1 gig RAM $30, 1 gig HD space $0.05 more or less).
Separate /var too. If the system goes down, active partitions are at the highest risk of corruption. /home /var and /tmp are the only things that are active. /etc only changes when you reconfigure. Anything else should only change during software a upgrade. /tmp a tmpfs didn't you?
You did make
This is not a good idea. Paging enables all sorts of other behavior besides swapping, such as security and virtualized processes (the standard on all O/Ses). It also allows more esoteric things, like letting user applications use more of the address space, and paging hardware is really optimized, plus, compilers help make decisions to improve the use of the CPU's caching hardware. Paging is a good thing.
Yeah that would be a great idea.
And yes, those bad boys do raid together and they max out SATA transfer rates.
From the boot CD:
`mv /mnt/hd /mnt/hd/old'
Install, then mv your home directory back. Now you don't need the foresight from college days to realize you'll need 25 GiB for MP3s! Plus now if you have the disk space, you can keep the whole old FS and bring over conf files and binaries as you discover you need them, and even `chroot' into it if you like.
So I'm a FreeBSD guy rather than linux, but I'm going to assume that Linux also supports 'limits' that define the maximum a program can utilize before its denied access to more resources. You won't get a normal app on my FreeBSD boxes to use more than 256M of ram, they aren't allowed. There are 2 exceptions, the PostgreSQL server on one of the machines, and the bot that connects to that database. They both deal with large datasets on regular basis so they are allowed to use more ram. Now mind you, these machines are used for my personal development projects and they aren't really 'servers' in the sense that they see real load. My instances of apache don't NEED a lot of ram, some do.
My point is that there are other protections in place that prevent an app from 'running away' and taking a properly configured machine down.
Second, swap can be VERY useful even if you NEVER run out of ram. The OS can swap apps that have used memory but aren't actually doing anything with it out, and leave that memory available for file/disk caching, which can make performance FAR better than if you kept the idling apps in memory and had less available disk cache. Some apps avoid buffering things in memory because its both easier and most times more efficient to use the disk and let the OS manange the buffering. I've seen NT based OSes aggressively swap out things that aren't in use just so there is more memory available for disk cache, and it makes sense cause there is a lot of crap the kernel and other apps load up that is very RARELY needed, if ever.
So while you can ( and did ) point out the potential pitfalls of using swap, your examples don't apply to any modern OS. I'm excluding Windows from that statement cause lets face it, its not exactly modern at the core. Modern kernels are FAR better at deciding what to swap than you are in almost every case, just like compilers can do a far better job of optimizing applications that most developers can. Yes some can do better, but its not likely you are, and certainly not the guy asking this question.
In short, if you're going to try to get technical with why you wouldn't want to use swap, at least use examples problems that weren't solved years ago.
And for reference, you configure your swap poorly if you do what you say.
Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
Yes.
Just because an app can only use '32bit' addressing, doesn't mean the OS does. For instance my '32 bit' processor has '36 bits' of address space. So I have the capability in the processor to address 16 full 32 bit address spaces, even though the OS doesn't let any one app have more than 32bits worth of address space.
Unfortunately, I run XP which won't let me get even to 32bits for all apps combined, but thats another story.
Second, when you take 'virtual memory' into account, you can have a 'virtually' unlimited amount of applications that all think they have access to 32bits worth of address space. For instance say I have 3 apps that have memory leaks in them. These 3 apps for whatever reason must run for long periods of time without restarting. They really only use a gig each of active memory, but because they leak all the time, they consume a lot of wasted memory. This wasted memory will never be used again, so the OS can swap it out as needed and continue to let the buggy apps run until they can be patched, hopefully they will complete their long run cycle before exhausting swap as well.
A better question is, do you really want to deal with those situations? Most of us have no need to. I don't, I know that. And for many people, if they actively need to use 2gb of swap, your dealing with some process that is going to be so painfully slow that you'll probably be willing to plunk down the money for more ram than deal with waitting on the disk light to turn off sometime next May. My swap is set to 8gigs on my XP machine, not because I want/need the swap space, but because I use it as a buffer for free space. When I get low, I turn off swap and reboot and have an extra 8 gigs of space for porn until I can drive my lazy ass to the store to get more tissues and a bigger drive. Not the ideal solution, but it makes me cleanup old crap when I get low on space most of the time and provides an alternative that is just inconvient enough that I don't use it 99.999% of the time.
Persistent Volume manager for Kubernetes - https://github.com/dwimsey/openshift-pvmanager
8 gigs on a HDD will take 100-800 seconds to read off.
If you need that much, buy another 8 gigs of memory.
THAT'S why you don't double memory as swap.
1/2 to 1 Gig is plenty. If you start using a lot of it, get more memory.
says:
"The most important factors in determining swap space size are the requirements of the system's software applications. [...] If you are unable to determine swap space requirements from your application vendors, use the following general guidelines based on your system type to allocate swap space."
Workstation with about 4 Gbytes of physical memory: 1 Gbyte Swap
Mid-range server with about 8 Gbytes of physical memory: 2 Gbyte Swap
High-end server with about 16 to 128 Gbytes of physical memory: 4 Gbyte Swap
http://docs.sun.com/app/docs/doc/817-5093/fsswap-31050?l=en&a=view
afaict there is no need to bother with a loopback device on modern linux you can just mkswap and swapon the file directly.
note: i'm known as plugwash most places but i screwd up registering that here somehow in the past and now can't register
Each userspace process has its memory divided in to pages. These are typically 4KB. Each process has its own set of page tables, which map each of these pages to a real page in RAM. These entries are 32 bits, with 20 being used to indicate the address of the physical page. The remaining 12 are flags. One of these is the 'present' flag. If this is not set, then the operating system will receive an interrupt and have to set up the mapping correctly before it can proceed.
If you have a 32-bit OS with two processes, each using 2.5GB of their 4GB address space, then some of this will have to spill over to swap space. Each process will have (for example) 0.5GB of its RAM swapped out to disk. When it attempts to access this, it will be paused while the OS writes some pages that haven't been used recently out to disk and then loads the requested page back in.
A 32-bit OS just means that each process can only have 4GB of address space and that you can only have 4GB of total RAM. There are some slight fudges to get around this too - on anything more recent than a Pentium Pro, you have Page Address Extensions, which gives you 36 bits of physical address space, so you can address 64GB of RAM, but pointers are still 32 bits, so you still only get 4GB of virtual address space per process.
I am TheRaven on Soylent News
posix_fadvise and posix_madvise seem to be standard posixy way to give hints to the cache. madvise(..) and fadvise(..) also exist granted some of the kernels don't properly use the hints.