This question often pops up in discussion. The commonly-expressed rule of thumb is that the swap partition should be at least three times larger than the amount of RAM in the computer. "But", I hear you cry, "I have a four gigabytes of RAM in the computer - surely I could get along with virtually no swap partition at all?".

That theory is attractive, and I subscribed to it myself for a while - but then I figured out the fly in the ointment. When the Linux kernel runs out of memory (that is, uses up the total of both RAM and virtual memory in the swap partition), it sometimes finds itself backed into a corner it can't get out of. The kernel will attempt to free memory by killing some processes - sometimes it's the process that made the request for memory that caused the problem, or it might be some large process that's been idle for some time (which might be the X server if you're silly enough to run one on a server, or the sshd on a workstation). Either way, the result can be surprising.

And worst case, it still might not fix the problem, or there might be no killable processes - and now, with no memory to run error-handling code, the kernel can't handle the error, and the result is an ugly crash. (Linux is not alone in this behaviour - many if not most operating systems die an ugly death when memory leaks consume their swap space).

So the best strategy is to ensure that the system has plenty of free virtual memory. You can monitor this with the free command, as well as with various desktop utilities and server management programs. But the best way to deal with the problem on a desktop system is to provide plenty of swap space, because as the system progressively uses more and more of it, it will slow down to a crawl, providing the user with plenty of warning that all is not well. With only one-quarter of the program code & data you're using actually in memory, and three-quarters out on disk, the system has slowed down a lot. On a system with four gigabytes of RAM and a 1GB swap partition, the system will barely have started swapping when it hits the wall, and the user will hardly notice the slow-down before the crash.

So, stick by the swap partition being three times larger than the RAM - if you have a machine with four gigabytes of RAM, you can certainly afford twelve gigabytes of disk space, but you probably can't afford system crashes.