You can set the value of /proc/sys/vm/swappiness
to control the ratio of segments of data swapped to the segments of data kept in memory. A value of 0 completely avoids swapping at all costs.
This can be done using either:
echo 0 > /proc/sys/vm/swappiness
sysctl -w vm.swappiness=0
- Storing that setting in
/etc/sysctl.conf
Generally, using just a little swap is not a bad thing. Free memory can be used for caching data read from disk, and the system can plan ahead for a sudden need of lots of memory by an application.
When too many programs are swapped however, there is a lot of disk related activity during every program switch which really makes everything slow down. Before something can be used, it needs to be loaded back into memory.
Disks reads are horribly slow compared to memory access, as it takes significantly longer for the data to arrive. The system has to schedule the read between the other read/write requests, the drive starts making attempts to find the right cylinder, and finally starts slowly delivering data.
Hence, I think your logic is flawed. Generally, you want to keep programs running in memory, while still keeping enough room for sudden growth. Do not use the swap too often to "write things to disk", because it is neither a backup nor a performance improvement.
Older computers contained less memory and suffered from swapping problems as a result. When many programs were open at once, the system would slow down and you could hear the disk reading and writing in order to the swap file.
That depends on the implementation of hibernation. Even if you restrict the question to Linux, the implementation has evolved over time.
First, consider that some of the RAM is used for disk caches. This doesn't need to be moved to the swap as it can be reloaded from the disk after the system resumes. On a system with a good cost/efficiency balance, it's typical for about half of RAM being allocated to caches. (See also Why use swap when there is more than enough RAM.) Under Linux, some early implementations would store all allocated memory into the swap, but the current implementation(s?) of hibernation skip disk caches.
Second, some systems compress memory as it's written to the swap, which can make the exact required amount of swap hard to predict. Some versions of Linux's hibernation support have supported compression; I don't know if current ones do.
What you can generally expect if there isn't enough swap space is that hibernation will fail: the system will try to store the (useful) contents of RAM into the swap, and as soon as it detects that there isn't enough space, the hibernation attempt is aborted (typically with an error message on the console and in the system logs). As far as I know, Linux has always behaved like this (not that there's really another sensible behavior).
Best Answer
The main disadvantage that I can see for using swap files as opposed to swap partitions is that it's not possible to have a swap partition get fragmented, whereas it's possible that, upon creation, a swap file on an extent filesystem can get split up on the disk, causing slower sequential access.