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?
Thread beginning with 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
Member since:

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))

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

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

Reply Score: 3

RE: I have a better way...
by parentaladvisory on Thu 20th Nov 2008 07:05 in reply to "I have a better way..."
parentaladvisory Member since:

Hmm, I can agree with your reasonings, but how is it with applikations that "requires" a pagefile(windows terminology)? _If_ you had 32GB of RAM and you use an app that is not going to install or whatever if you do not have a swapspace, what you recomend then?

Reply Parent Score: 1

RE[2]: I have a better way...
by BiPolar on Thu 20th Nov 2008 11:21 in reply to "RE: I have a better way..."
BiPolar Member since:

_If_ you had 32GB of RAM and you use an app that is not going to install or whatever if you do not have a swapspace, what you recomend then?

File a bug report about that silly app and/or search for an alternative program.

Reply Parent Score: 2

RE[2]: I have a better way...
by looncraz on Thu 20th Nov 2008 22:18 in reply to "RE: I have a better way..."
looncraz Member since:

Well, if you ABSOLUTELY NEED that poorly written program, then you will need to satisfy its "needs." You will need to experiment or research to find out how much swap space for which the thing is checking.

Regardless, I agree with BiPolar completely, file a bug report or use a different program, if possible.

--The loon

Reply Parent Score: 2

RE[2]: I have a better way...
by B. Janssen on Sun 23rd Nov 2008 11:35 in reply to "RE: I have a better way..."
B. Janssen Member since:

Just wirte a little script (read the manual pages of dd, mkswap, swapon) that adds a swapfile to a partition of your choice, e. g. /home or /opt, for the application and cleans up after you exit the application (check swapoff and rm).

This will increase your application's start-up time significantly if the swapfile is very huge, i. e. hundreds of GB, but satisfy the app's requirements. After you have done that, send the script and a bug report to the programmers of the app and tell them that a userspace application has no business poking its nose into memory management ;)

Reply Parent Score: 2