Linked by David Adams on Thu 20th Nov 2008 04:19 UTC
General Unix Linux and other Unix-like operating systems use the term "swap" to describe both the act of moving memory pages between RAM and disk. 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. Now, many admins (both Windows and Linux/UNIX) follow an old rule of thumb that your swap partition should be twice the size of your main system RAM. Let us say I’ve 32GB RAM, should I set swap space to 64 GB? Is 64 GB of swap space really required? How big should your Linux / UNIX swap space be?
Permalink for comment 337822
To read all comments associated with this story, please click here.
I have a better way...
by looncraz on Thu 20th Nov 2008 06:02 UTC
looncraz
Member since:
2005-07-24

The best way, IMHO, is to do need approximation.

For instance if you will not be doing anything different with your new 2GB of RAM than you did with your 512mb of RAM, and you had a 1.5GB swap space before, then you really don't need any swap space what-so-ever.

In fact, I have 512MB of RAM ( by choice ), and I keep a 750MB swap partition for Ubuntu. I only "hit" the swap partition when a piece of software is memory leaking. For me, using the swap is a no-no/fall-back scenario.

But let's say you will be using some intense applications ( video editing ). The program requires 2 GB of RAM, but recommends 4. You have 2GB of RAM, and can't fit any more in the system ( y'know, ya bought some machine instead of building your own, like ya shoulda ).

With that you have your needs: 4 GB of RAM, meaning RAM + SWAP >= 4 GB as a minimum.

Now, set aside 200MB for the operating system, Desktop, and various background utilities which may or may not end up needing to be swapped to disk.

Then, if you expect to be running other applications at the same time, see what their usages could be. Firefox, well just add another 250 MB, just to be safe.

You know for certain you will be running an apache_server for your intranet, so add 100-200MB if you need.

Continue on until you have exhausted the list of program you will be most likely running while the most memory intensive portion of the video program is running in lowest priority ( or max 'nice' ). Includes your media player, say 75-100 MB.

Now, your final "max" memory usage would be expected to be around 6GB in this case.

So you have a 4 GB partition/file for swap, preferably on another ( fast ) hard drive.

But let's say you have 16GB of RAM and the above is true. No, a 32GB partition would be stupid. In fact, any swap would be - just in case the kernel decided something was dormant and should be swapped to disk just in case real memory is needed - even though it never will be. No point in that at all ( this is what Windows did/does (Vista was suppose to fix it, but it crawls on my machine - even though everything else flies ).

So a real simple formula would be:

S = C(M - (R/C))

Where:
S is swap
M is max memory required
R is the actual RAM installed.
C is the extra comfort room you would prefer,
such 25%, which would be 1.25

So, if you need 3GB of RAM total, have 2GB of ram, and want to have room for at least another 512MB of load while still allowing the same amount of added comfort( 25%) then you can do the following:

1.25 * (3 - (2/1.25))
1.25 * ( 3 - 1.6 )
1.25 * 1.4
1.75

So you get a 1.75 GB partition with 2 GB of RAM to satisfy your needs, plus some comfort, giving you 3.75GB of RAM+Swap. Remember that comfort means to have some left over, even if you have gone right to the max extant of your comfort level.

Naturally, not many people want to calculate their work loads to this precision, but it is the "right" way. And, I would personally use a comfort level of 50% or maybe even 75%. Sometimes you just plumb forget about closing those programs on another workspace.

But if you want to be the safest, then you should just count the number of programs you could possibly run at once, and give them each room to run to their fullest addressable limits ( 4 GB per app on 32-bit, depending on OS as well ).

So if you have 60 programs, you will need 240 GB of RAM + swap.

To be somewhat smart: Just keep an eye on your swap usage ( if the OS allows it in a human readable manner ) and increase its size if you use too much or have problems with memory. Not so easy with a partition if you can't easily resize it.

So, seriously, your best bet is:

S = C(M - (R/C))

If you don't know what your loads will be, assume you need exactly 50% more than the RAM you have, so:

M = R*1.5

If you upgraded your RAM, don't even fret about resizing the swap file, there is no point unless you have errors or expect to be using more memory beyond what your upgrade provides, then use my formula.

--The loon