MacBook’s Hibernation and Secure Virtual Memory
The MacBook that I bought a week ago had one nasty unexpected bit of behavior. Instead of going to sleep when the battery ran empty, it would just shutdown. Shutdown the hard way actually, so all unsaved work was lost. That is unacceptable behavior in my book.
But, thankfully, there is a solution…
Update: See my next post for some more precise explanation of the meaning of the pmset value for hibernatemode. It is nevertheless still recommended to solve he sudden shutdown behavior by disabling secure virtual memory
(One caveat though, this is based on just one successful forced sleep on empty battery. Others have reported success with it too though…)
My first thought was a badly calibrated battery, but subsequent tries to calibrate the battery failed because the calibration was undone by the hard shutdown. Suspecting that the battery somehow reported wrong values for the remaining battery life, I asked the Apple Service Centre for a replacement battery. They noted that this kind of problem was quite common with MacBooks, and that their solution was: “Plugin your adapter on time.” I did get a replacement battery, and was hoping furiously that this would fix it, as I sure would not be able to life with a solution like that.
Unfortunately, the new battery was not the solution. Again I experienced a hard shutdown when the battery had run out of juice. That was not what I had hoped for.
Of course I browsed the internet, and especially Apple’s Discussion forum to see if there were others suffering the same problem. A post by Psychonaut15 revived my hope of fixing the problem. As it turns out the hibernation mode set in the power management of my MacBook conflicted with the setting for secure virtual memory.
The default, at least since Mac OS X 10.5 (Leopard) is to have secure virtual memory enabled. You can check this in your Security preference pane. The default setting for the hibernation mode is however buried deeper within the system. To see it, type the following command in the Terminal:
pmset -g
It list amongst others your hibernatemode. The default is 3, or in bitwise notation 0011 on portables (0 or 0000 on desktops). You can take a look at the manual for pmset with this command:
man pmset
The manual explains the meaning of two of the four bits, but unfortunately fails to explain the other two bits. And one of those bits is very important to have set properly. (Note that the bits are counted from right to left starting at 0.)
To quote from the manual of pmset:
0001 (bit 0) enables hibernation; causes OS X to write memory state to
hibernation image at sleep time. On wake (without bit 1 set) OS X will
resume from the hibernation image. Bit 0 set (without bit 1 set) causes
OS X to write memory state and immediately hibernate at sleep time.
0010 (bit 1), in conjunction with bit 0, causes OS X to maintain system
state in memory and leave system power on until battery level drops below
a near empty threshold (This enables quicker wakeup from memory while
battery power is available). Upon nearly emptying the battery, OS X shuts
off all system power and hibernates; on wake the system will resume from
hibernation image, not from memory.
The next bit, bit 2, however turns out to need to be turned on when secure virtual memory is used. Psychonaut15 found that information in an old MacWorld article on hibernation from 2006. It eludes me why this is not reflected in the pmset man page.
So, as it turns out, the default settings for secure virtual memory and the hibernation mode conflict. The solution is to either turn off secure virtual memory, or to set the hibernatemode to 7 (0111 in bit notation).
When the setting for secure virtual memory is changed in the Security preference pane, bit 2 of the hibernatemode is not changed, so up to Mac OS X 10.5.2 at least you should be very wary of toggling this preference.
My recommendation is for all MacBook users to disable secure virtual memory (and reboot). Protecting my data from myself forgetting to plugin the power adapter on time is a lot more important than other users of my laptop (there are none) snooping through my virtual memory.
I filed this as a bug with Apple. I hope it will get included in the next bug fix release of Leopard. Until then, I hope the solution of disabling secure virtual memory can be of help to other MacBook owners.