Long time ago computers had just two states: turned on and turned off. We’ll skip this period 🙂
Before Mac OS X 10.4 Tiger (2005) there was just one option for sleep. It was a regular Sleep. This option is by default for desktops up to our days because desktops are connected to power constantly and for this reason there is no need for hibernation.
But for laptops this option was not good enough because it could run out of battery. In this case MacBook just turns off and loose it’s state.
In order to fix this issue new feature was introduced in Mac OS X 10.4 Tiger: Safe Sleep. Safe sleep means that MacBook save the state to hard drive. So there are two copies of your data: in RAM and at Disk. After Stand By period RAM is powered off. And even in case of power loss macOS just restores content of RAM using sleep image from disk. But in case MacBook have enough power, it wakes imminently which is much faster. This way Safe sleep takes the best from Sleep and Hibernate.
Sleep + Hibernate = Safe Sleep
But the amount of RAM in laptops is growing and it takes longer to safe everything to disk. At the same time it is difficult to find a laptop without SSD in our days. And as you may know, SSD is not designed to save/load gigabytes of RAM to sleep image. Because they wear out.
For this reason additional optional options were introduced both for memory management and hibernation in Mac OS X 10.9 Mavericks. And memory management became more complicated. In order to save on SSD writes/reads, macOS is trying to compress some pages and store compressed virtual memory pages to an another region in RAM. In case you need more memory, it finally gives up and write pages to swap file.
Note: if you have enough RAM, you can increase macOS performance by disabling swap and memory compression. It could give really good impact on older macs with slower CPUs because modern operating system consumes a lot of computation power in order to avoid SSD wearing. The bad news are that there is no way to disable memory compression without disabling swap. In the source file you can find that there are only three options available:
0x1 (VM_PAGER_DEFAULT) Default pager (without compressor and swap)
0x2 (VM_PAGER_COMPRESSOR_NO_SWAP) In-core compressor only
0x4 (VM_PAGER_COMPRESSOR_WITH_SWAP) In-core compressor + swap backend
Why so? Because during restore from hibernation loader should know if pages are compressed or not. And it knows because they are always compressed. And this is done in order to care about your SSD.
So as you can see, macOS is really stingy on SSD writes. And in case you want to hibernate it, you may expect that very little of virtual memory pages will be saved in swap file by that moment. And the more pages are saved to swap file, the less pages we need to save to sleep image.
For this reason two new options were introduced in macOS. These options tells pager to purge virtual memory pages to swap file. In case you are going to write everything to disk, it make much more sense to save them to swap file than to sleep image for two reasons:
– it allows to wake up faster due to smaller sleep image;
– in case MacBook is waked up by mistake (or by power nap) it will not take long time to hibernate back;
For sure all the pages will be saved to sleep image instead of swap file in case you are using the default pager (without compressor and swap).
Thus in macOS there are the following basic options for hibernate configuration:
0000 0000 (0) – classical Sleep. default for desktops
0000 0001 (1) – Hibernate
0000 0010 (2) – It works only in conjunction with (1) and do not power off RAM cips right after sleep image is saved to disk
0000 1000 (8) – save unused pages to swap file before hibernation
0001 0000 (16) – do the same more aggresively
These basic options are designed to be combined like that:
0000 0011 (3) – Safe Sleep
0001 1001 (25) – recommended for better battery life
You can try to set these options like that:
pmset -a hibernatemode 25
Actually only values 0, 3 and 25 make practical sense. All the others are needed for debugging only. Zero is for desktops, 3 and 25 are for laptops depending on your needs. 3 is for faster MacBook wake up, and 25 is for staying with charged battery longer.