You've probably been there. 2 AM finishing your 4th cup of black coffee. The report is due tomorrow. One more spell-check… the system is slow and the disk light is buzzing. Suddenly, the main window dies. You see some text message about "Out of Memory Killer." The Linux kernel noticed you were low on memory and decided to nuke some programs for you. All gone. Even the big picture of Bruiser for the cover. Sigh.
No matter how much RAM memory you have, there's always room for more. Linux can play clever tricks to make your hard drive — or parts of it — look like slow memory for parts of programs you aren't using right away. That technique is known as swapping and you need a swap file or swap partition to make it work. But wouldn't it be nice if it could configure itself? Turns out, it can.
TLDR: Install swapspace and — usually with no configuration — it will add and remove swap files automatically as you need (and don't need) them.
One of the big decisions when setting up a new Linux system is if you want to have a swap partition and if so, how big should it be? Having a dedicated swap partition used to be faster than swapping to files, but either way, it is space you can't use for your programs, documents, videos, e-books, etc.
Instead of creating a static swap file, you can employ a program called swapspace to manage it all for you. This program is typically available from your distribution's package manager (apt, rpm, etc.).
Even though you could omit swap totally with swapspace, you still may want one normal swapfile to allow suspend and resume to work, especially on a laptop. On a machine that's always on, though, you could just leave off the swap file and swapspace can dynamically create swapfiles as needed.
Just as important, it can remove swap files that are no longer needed. Ideally, you wouldn't swap at all since swapping even to a fast solid state disk isn't very speedy. But if you are running out of memory, having swap will keep things running instead of crashing and that's usually a good thing.
If you want to read more about how swap works, I'll cover that in the last section. But meanwhile, let's get right to…
The Solution
You can set up a swapfile in the usual way or using dphys-swapfile if you like. If you want to do suspend and resume, the file should be as big as your RAM memory. However, you can get away with less or even none if you install swapspace.
In general, you don't need to change anything after you install the program. You may never notice it working, but if you run swapon -s
you might see a swap file like /var/lib/swapspace/1 or other similar files. These are the files that swapspace creates if it decides you are running out of memory.
There are a few options but you'll rarely use them. You can use -e
to attempt to erase all swapfiles the program has created or -f
to set a target for free memory. But usually the default are fine.
There is also an /etc/swapspace.conf file you can use to change things. For example, you can set the directory for swap files to a new location or the target free space here.
Again, you normally won't need to change any of this. The .conf file is all commented out and shows the defaults. The man page will shed light on the options, too, if you want to dig into it. Most of the time, though, it just works. If that's all you need to know, you can skip the next section.
What is Swap?
Your computer normally loads programs into RAM so they can execute. In the old days (think MSDOS) you loaded one program at a time and it used all the RAM. Newer operating systems can run more than one program at a time, so they all need a space in RAM.
A modern computer often has 2, 4, or even more gigabytes of RAM. But it only goes so far. You'll eventually run out, especially when the system uses some for other reasons.
Another feature of modern operating systems is that it provides a virtual reality environment to programs. Every program thinks it has a very large amount of memory starting at address zero. In reality, the computer only gives them pages of memory that they really use. The page is often as little as 4K of memory. So even though the program might think it has 3GB of memory, it might only be using a little bit at the start and a little bit at the end and only consume 10 or 20 pages of real memory.
However, there will come a time when the computer needs a page of memory and there aren't any. When that happens, the operating system can do two things. It can find some data that it knows will not change — maybe a font, for example — and drop those pages to free up space. Later, if the program needs that font, the computer can roll those pages back in from the disk.
That's fine if a file stays the same all the time, but what about data that changes? That's what a swapfile is for. The computer can write any pages that haven't been used lately to the swap and free those pages. Later, if a program needs that data, the swap system rolls the data back in, potentially evicting other old pages in the process.
Without swapspace, you'll have to guess how much swap you need and then that's how much you'll have all the time. You might use it all or you might use none of it, and it won't matter. Swapspace dynamically adds swap as you get low on memory and removes swap files as you get more free memory.
Even More
The most efficient way to swap is to not swap at all. If you have limited memory, you might consider ZSWAP which converts some of your RAM into a very fast compressed swap file. It might seem odd that eating up more memory could help performance, but it is so fast and with compression, you get more mileage out of the memory as swap even though it cuts into memory available for running programs.
ZSWAP is a good compliment to swapspace. The operating system uses fast compressed RAM first and then, if necessary, will automatically make swap files as you need.