[Techtalk] Swap away

James Sutherland jas88cam at gmail.com
Sun Jun 15 11:05:43 UTC 2008

On 15 Jun 2008, at 01:33, Akkana Peck wrote:

> Conor Daly writes:
>> you have 4Gb physical, you probably won't need swap at all.  OTOH,  
>> if you
>> have a laptop and you want to be able to hibernate, you need at least
>> 1xphysical.
> There's something I've never understood about swsusp. Suppose I
> have 256M physical and another 256M swap, and I'm using most of
> both. Now I try to hibernate. The hibernate code needs to write the
> contents of RAM 256M), plus the contents of swap (another 265M),
> a total of 512M, into a swap partition that's only 256M. How is that
> possible? Does swsusp use some kind of super compression so that
> it can fit 2*N into a 1*N space?

That isn't possible — compression might save some space, but you
can't guarantee 2:1 compression like that.

What you can do, however, is throw away a surprising amount of
the data in physical memory.

Some blocks will be free: completely unused. Obviously, no problem
for swsusp. Some more will be used for caching disk contents: just
throw these away when suspending.

Any read-only data from files (libraries, executables etc) is also
just a cached copy of disk contents: again, this can just be discarded
and re-read from the original source when needed.

Finally, there's data which only exists in RAM and/or the swap file.
Some data exists in both ("clean" pages), so the copy in RAM can
also be thrown away then re-read from swap as needed. The rest
("dirty") is all that needs to be written out to swap when you suspend.

As a result, as long as there's enough swap space free to write in
those "dirty" pages, suspending will work fine. You might be using,
say, 200M of your swap, with another 50M of RAM being "dirty",
at which point everything's fine.

Two interesting side effects here: first, Windows (and the old BIOS
suspend mechanism) uses a simpler but less efficient approach:
ignore swap, just write the whole of physical memory out to disk,
byte for byte. Slower, eats more disk space, but a bit easier to

Secondly, the handling of executable code means that even without
any swap partition active, the system can still swap by discarding
and re-reading executables. That's why disabling swap is no
panacea for disk thrashing, and can even make it worse by forcing
the kernel to swap out pages which are re-used frequently.

> I remember in the early days of swsusp I tried creating two swap
> partitions, using one for swap and the other for hibernate -- and
> swsusp wouldn't let me do that, and would only use a partition that
> was currently being used as swap.

That won't work, as you found: because swsusp relies on swapping
everything out, it can only swap to actual swap space. (It might also
struggle with multiple swap partitions, because


More information about the Techtalk mailing list