Ceci est une ancienne révision du document !
Last time, we established a kind of sliding scale to determine whether or not a Swap partition is a) essential, b) useful, c) wasteful, or d) obsolete. Don't worry, this isn't an exam. However, it may be worth looking into the topic of Virtual Memory Management, to call it by its proper name, a little further. Ancient Linux hackers among you will have to excuse the Dr. Seuss-level distillation which follows (or else go write your own 300-page book on the subject).
Page by Page
Memory, as used by a Linux operating system, is divided into pages, which can themselves be categorized by usage:
• Kernel Pages are fixed in memory and never swapped. Some operating systems have pageable kernels, but Linux plays it 'safe'.
• Program or application memory pages are held as read-only in memory and are exact copies of the binary executable on disk.
• File-backed pages are those containing volatile values which haven't been written back out to disk. These might contain changes in memory that haven't been written out yet.
• Anonymous mappings are volatile pages which don't correspond to any file name; these contain the stack and heap variables of running tasks, created by a program requesting a memory allocation from the kernel.
The cached pages can be divided into 'clean' and 'dirty', which the kernel memory manager has to look after in the background.
• Clean means the data in memory is the same as on disk, or, the values are unchanged since they were instantiated. Clean pages can be discarded whenever the memory is required for something else as they match the original data available on disk.
• Dirty pages contain changed data since the last read or instantiation. These have to be 'evicted' - the changed values recorded to disk - before the memory can be flushed.
It's important to remember that Linux has no separate disk cache, unlike Unix- or DOS-based systems. It's designed to read-in and keep the most useful data from disk as cached pages. Also, the kernel doesn't usually modify the disk directly: changes are made to the files in memory, then 'flushed' after a disk-write by the memory manager. It's a data-security thing.
At times of peak workload, this is the operation which slows everything down, as the kernel runs out of empty and clean pages in the pool; it has to write dirty pages to disk before it can continue on with its prioritized tasks. File-backed pages are flushed by writing-back to their parent file on disk. Anonymous mappings, however, have no backing-file, these are stacks and arrays of works-in-progress. Where do they go? Into Swap!
If you don't have swap space, then anonymous mappings can't be flushed, they have to stay in memory until they're deleted. This reduces the size of the memory pool that the kernel can draw on to keep running all concurrent processes. It has to keep juggling file-backed pages, which can be even slower in operation; navigating directory trees and performing disk-writes by-the-book. It's a lot simpler and faster to blast out copies of flat pages to Swap. Remember, RAM is expensive, disk is cheap.
Partition or File?
The original criticism is that a whole swap partition is a waste, especially if you have only a small hard disk or solid state drive. You can use swap files, as do some other operating systems we won't mention, but remember swap files have to obey the rules alongside everything else on your journaling file system. A swap partition is ring-fenced for paging; no other files can reside there.
Kernel Panic
This is a recognized medical condition of mental anxiety, brought on in Linux users when they run the free or vmstat commands and discover how little free memory is reported. You can relax, this is quite normal.
The kernel memory manager tries to keep the available RAM filled with cached pages in expectation that the files will be used again and it won't have to go to disk to fetch them. This is intentional. Whole books have been written on the predictive algorithms used to predict what to cache, what to flush, when, and how much memory is sensible to leave in the empty pool. You can watch your free memory go up and down, if you have nothing better to do. Right after boot-time, there's a large pool, decreasing when you begin running application programs, then increasing again when a program exits and the data pages it held are released.
Swap On
To check the state of your swap partition, use:
swapon -s
which gave me the result shown below.
The Linux 2.6 kernel has a parameter called swappiness which you can use to tune the way Linux uses Swap. It is a number from 0 to 100: 0 means minimal swapping, keeping more pages resident in memory, even if they are idle. Going up the scale to 100 increases the rate of swapping in an attempt to maximize the amount of free memory available in the pool. The default value for swappiness is 60. You can alter it using root permissions and a parameter command such as :
echo 50 > /proc/sys/vm/swappiness
Any value set this way lasts until the next re-boot. To change it permanently, you need to edit the vm.swappiness parameter in the /etc/sysctl.conf file .
In adjusting swappiness, you are making a judgment call; it prejudices the kernel memory manager for-or-against swapping out to disk. Set swappiness too high, you risk your disk-writes going through the roof; meanwhile every concurrent process has to wait even longer in line for its data to be read back in from disk before it gets a slice of processing time.
Which brings us neatly back to the need for Swap. It's only there to support our marvelous multi-tasking, time-slicing operating system that we run on our lightning fast, parallel-processing multi-core CPUs. If you only do one thing at a time, you can happily ignore everything I just said. Before you do, try running the top command in a terminal during a busy session.