How large of a swap partition is needed to hibernate?
It's hard to predict: your swap needs to accommodate
- what's already in the swap when you hibernate
- what's in RAM when you hibernate, but only the part that's not disk cache or buffers; the RAM image is compressed if you use
uswsusp
- a few kernel data structures
So you can fail to hibernate even if your swap is bigger than your RAM, because most of your swap is already in use. And convertly you can successfully hibernate with a swap that's smaller than RAM, if you're not using much of either at the time.
If you want to avoid a fixed-size swap partition because your disk is very small, you can go for a swap file instead. It's easier to resize, and hibernation to a swap file is supposed to be supported (not sure if it currrently works out of the box, that's come and gone between Ubuntu releases; IIRC it did in 9.10 but it had to be set up in 10.10).
This question continues to surface in common queries when sizing swap to set up hibernate. The original answer is correct in that it could vary incredibly based on your workload, but I don't feel like that's a complete answer.
The complete answer is that you can tell how much you need based on your specific workload, then you can size appropriately.
The basic tool is the kernel messages during hibernate. You want to see how much memory was used with this command:
sudo cat /var/log/syslog | grep 'PM:'|grep kbytes
You should get output like:
Aug 14 11:03:20 mike-XPS-15-9570 kernel: [17594.823584] PM: Allocated 11809876 kbytes in 2.65 seconds (4456.55 MB/s)
Aug 14 11:03:20 mike-XPS-15-9570 kernel: [17605.365657] PM: Wrote 11787284 kbytes in 9.09 seconds (1296.73 MB/s)
Then the strategy is to run your maximum load, and trigger a hibernate, see what swap space was required, possibly add a margin of safety, then make your swap space that size.