A couple of people reached out to me with the same question after reading my recent article on Linux system hibernation: is it possible to hibernate into tmpfs instead of a swap partition? Some experienced Linux administrators might scoff at the idea, but let’s explore how these things work and interact, and what’s possible.
System hibernation copies everything out of volatile memory (RAM) and over to either a swap partition or swap file on a more permanent storage media. Your swap partition needs to be at least the same size as the memory you want to store on it. When hibernating, power to the system is cut after the contents of the system memory has been securely tucked away. The information in volatile memory is lost once the system is powered off.
It may be unappealing to set aside space on a small storage media for a swap partition. You want to preserve as much space as possible for your files and programs, after all. This setup works fine unless you exceed your system’s available volatile memory. At that point, the Linux kernel will begin terminating programs to free memory.
It may be tempting to use a temporary storage backend such as a temporary file system, or tmpfs, instead of a swap partition. The benefit of tmpfs is that you get fast performance by reducing slow I/O operations and that you don’t need to set aside any storage space for its contents. However, tmpfs itself lives in volatile memory either in a swap file or partition. The contents of a tmpfs itself need to be swapped to a swap partition to persist a system power-down.
Tmpfs is primarily useful only for temporary storage needs. As part of a file conversion process, or as a temporary place to store a working copy of a file for a program. System hibernation is most definitely a short-lived temporary need for storage space. However, tmpfs isn’t the right tool for the job.
You could theoretically create a swap file inside a tmpfs mount and have your volatile memory swap into itself. It would be completely pointless other than as an academic exercise.
If you don’t want to set aside space for a swap partition, then you can consider a swap file. It’s more convenient to shrink or grow a swap file than a swap partition. However, the list of caveats and limitations with regards to system hibernation is so long that I outright dismissed the idea of hibernating into a swap file in my earlier article.