Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > All about Safe Sleep (some interesting facts)

All about Safe Sleep (some interesting facts)
Thread Tools
Tomchu
Mac Elite
Join Date: Sep 2005
Status: Offline
Reply With Quote
Jun 13, 2006, 03:38 PM
 
I remember there being some discussion here about Safe Sleep and how it works. The other day I played around with it, and discovered some interesting things.

To introduce the feature to those of you who are unfamiliar with it, Safe Sleep is much like Windows' "hibernate", in that it saves the state of your RAM to the hard drive so that power to the system can be cut completely. When you turn it back on, the state is restored, and your system is as you left it. Unlike Windows, OS X only presents a single "sleep" option -- ie. you don't get to choose whether you simply want to keep the RAM powered up, or whether you want to save it and completely power off. This is where it gets interesting.

What many don't know, and what I didn't know, is that the saving-RAM-to-the-hard-drive is done <b>every time</b> you put your Mac to sleep. In essence, there are two "copies" of your RAM's contents present when you put the system to sleep. The first is, of course, what's already in the RAM -- this remains powered up. The second copy is on the hard drive. This explains why Safe Sleep-enabled Macs experience a delay when going to sleep, but not when waking up again -- going to sleep requires writing RAM out to the hard drive, while waking up doesn't read it back -- unless there was a loss of power.

This would, of course, make sense. In an ideal situation, you put the Mac to sleep, it stays alive, and then you wake it up at some point. In a non-ideal situation (one where either the battery dies, or it is removed), the RAM contents are lost from RAM, but there is another copy in permanent storage on the hard drive. When you turn your Mac back on after power loss, these contents are read back, and you are back to where you were.

There's another interesting fact that I discovered along with this, and it shows just how sneaky/creative Apple can be. When I first got my MBP, it had a stock 512 MB of RAM in it. I was worried that after I upgraded to 2 GB, it would take forever and a half to go to sleep, as that would require writing out 2 GB of data to the disk.

Imagine how surprised (and curious) I was when I discovered that the sleep delay was more or less the same! Then I thought about the issue, and ran an experiment.

Memory management in OS X (and many other Unices) calls for generally four kinds of memory -- wired, active, inactive, and free. I'll go into a quick explanation now:

Wired memory is memory that absolutely CANNOT be swapped out to disk. This is usually something like the kernel, or the VM system (you don't want to swap the VM system out to disk, or you won't be able to get it back), or perhaps any kind of security-sensitive data.

Active memory is memory that has been accessed in the last N units of time (calculated via a LRU algorithm). Inactive memory is memory that has not been accessed in the last N units of time via the same algorithm, and it is memory that qualifies for swapping out if necessary.

Free memory is memory that is not being used at all, and can be consumed by any task.

Inactive memory is what you want to pay attention to. Most of your inactive memory is going to be disk cache. Have you ever started an app on your Mac after rebooting, closed it, and then started it again? You'll notice that the second time around the app starts much faster. That's disk caching at work. OS X simply "remembers" what you read off the disk so that in case you ever need to access it again, it can be read back from the much-faster RAM instead of the hard drive.

This disk cache is constantly being updated -- old files are forgotten, new files are placed in it, and so on. This is why after your Mac has been used for a while, you'll see very little "free" memory. Free memory is wasted memory, as all of your free memory can go towards the disk cache to improve loading/reading times of oft-accessed files. When an app actually needs that memory, OS X releases some disk cache and gives the app what it wants. In any case, the disk cache is not critical -- it can easily be wiped. OS X will simply have to re-read any files that you want.

This is exactly what Safe Sleep does in order to cut down on sleep delay times. It writes out wired, active, and important inactive memory to the /var/vm/sleepimage file, but it <b>leaves out the disk cache</b>. Try it for yourself if you have a MB/MBP -- open Activity Monitor, open a terminal window, find a rather large file (let's say 75% of the size of your RAM), and then grep it for a random piece of junk. Do it a few times so that OS X "remembers" the file in its disk cache.

Put the system to sleep. Wait until the light starts pulsating. Remove power plug, and then the battery. Plug everything back in, and revive the system. What just happened to your inactive and free memory counts? ;-)

Sneaky!
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Jun 14, 2006, 01:42 PM
 
Very clever I wonder if the sleepimage file is based on swapfiles, since they already have a painstakingly tuned system for writing memory pages to disk...
     
Simon
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Jun 14, 2006, 03:54 PM
 
I was always suspecting OS X doesn't write out the entire RAM content to the HDD because it just happens too fast.

If you assume notebook HDDs have a max write speed of ~50MB/s (my MBP's 7200 RPM SATA drive isn't actually even quite that fast) and you further assume you have 2GB of RAM, it would require up to 41s for that to be all written out onto the drive. But in real life the delay for me is certainly well below 20s, so we are talking about less than 1000MB which can actually be written to disk in the time frame given. I guessed it's because only critical memory chunks (wired, active) are actually written to disk. The rest can be neglected. And your checks seem to indicate that exactly that is happening.

Nice analysis btw.
     
Horsepoo!!!
Banned
Join Date: Jun 2003
Status: Offline
Reply With Quote
Jun 14, 2006, 04:52 PM
 
I've been asking Apple to add this functionality to *all* computers. Sure, power outtages during sleep don't happen (less so than the battery on a laptop dying during sleep)...but it happened to me twice. I woke up one morning to find that my computer was rebooting and that I had lost some unsaved documents (yeah, yeah, save your documents). Mind you, I didn't lose much but it would have been nice to push the power button and be back where I left off.
     
schalliol
Mac Elite
Join Date: Oct 1999
Location: Carmel, IN, USA
Status: Offline
Reply With Quote
Jun 14, 2006, 05:10 PM
 
I wonder though, a couple times I have put my machine to sleep and it wakes up as if I shut it down. I suppose it's possible that it was an isolated incident. Also, a co-worker's 17" MBP sometimes doesn't go to sleep after the lid is closed....
iMac Late '15 5K 27" 4.0 Quad i7 24/512GB SSD OWC ThunderDock 2 Blu-Ray ±RW MBP '14 Retina 15" 2.6 16/1TB iPhone 7+ 128 Jet Black iPad Pro 128 + Cellular

FOR SALE: MP '06 Yosemite 8x3.0 24/240GB SSD RAID 0, 240GB SSD, 1.5TB HDD RAID 0, 1TB HDD, Blu-Ray±RW, Radeon HD 5770
     
Socially Awkward Solo
Professional Poster
Join Date: Jul 2002
Location: Hanging on the wall at Jabba's Palace
Status: Offline
Reply With Quote
Jun 14, 2006, 05:41 PM
 
I remember when this was on the original iBooks in OS9 and when it started overwriting files it disappeared from future products. That is until now

"Laugh it up, fuzz ball!"
     
nerd
Senior User
Join Date: Jun 2002
Location: Albuquerque, NM
Status: Offline
Reply With Quote
Jun 14, 2006, 11:06 PM
 
I'm not trying to derail this thread but I remember back in OS 9 I would sleep my Pismo for 2 weeks. Then OS 10 came out and now we get maybe 4 days (I've really never tested this). Was OS 9 writing things to disk? I remember it took a little longer to wake up.
     
icruise
Moderator Emeritus
Join Date: Nov 2000
Location: Illinois
Status: Offline
Reply With Quote
Jun 15, 2006, 01:16 AM
 
I'm not sure of the technical explanation for it, but OS 9 and OS X definitely handle sleep differently. OS 9 did not have the instantaneous sleeping and waking that you get from OS X, but that came at the expense of battery life while in sleep.

By the way, I enabled Safe Sleep for my 12" PowerBook using the instructions found here and it's worked great. Finally lets me change batteries without shutting down or finding an outlet.
     
Socially Awkward Solo
Professional Poster
Join Date: Jul 2002
Location: Hanging on the wall at Jabba's Palace
Status: Offline
Reply With Quote
Jun 15, 2006, 11:01 AM
 
Originally Posted by nerd
I'm not trying to derail this thread but I remember back in OS 9 I would sleep my Pismo for 2 weeks. Then OS 10 came out and now we get maybe 4 days (I've really never tested this). Was OS 9 writing things to disk? I remember it took a little longer to wake up.
In the instructions for current Macs it tells you to turn the mac OFF if you are not going to use it for a couple days.

I don't see the point of sleeping a laptop for 2 weeks.

"Laugh it up, fuzz ball!"
     
jasong
Mac Elite
Join Date: Mar 2000
Location: Allston, MA, USA
Status: Offline
Reply With Quote
Jun 17, 2006, 02:57 PM
 
Maybe no point, but he's right, at one point it could be done. It was nice to put my PowerBook to sleep, bike to work, and not have lost 5% of my battery power. I would have (and still would) trade instant sleep/wake for the more power efficient OS 9 model.

One of the very few things about OS 9 I liked more than X.
-- Jason
     
AppleJockey
Forum Regular
Join Date: Nov 2005
Status: Offline
Reply With Quote
Jun 17, 2006, 06:25 PM
 
if my sony PSP can do it;
surely apple sould have osX do this on ALL machines
     
jasong
Mac Elite
Join Date: Mar 2000
Location: Allston, MA, USA
Status: Offline
Reply With Quote
Jun 17, 2006, 10:54 PM
 
uh, your PSP can do it because the hardware was designed to do it. OS X can't make hardware that wasn't designed for it do it magically. And what does one have to do with another? My Treo can make phone calls, surely apple sould have osX do this on ALL machines.? No, because they are unrelated devices that work in different ways and have absolutely nothing to do with each other.
-- Jason
     
AppleJockey
Forum Regular
Join Date: Nov 2005
Status: Offline
Reply With Quote
Jun 18, 2006, 08:04 AM
 
^
Glad your sense of humour has not gone into safe sleep mode
     
milhous
Mac Elite
Join Date: Sep 2000
Location: Millersville, PA
Status: Offline
Reply With Quote
Jun 19, 2006, 10:57 PM
 
tried the hack on my iBook G4 with no luck. but that's probably because there's no internal hard disk in it right now (waiting for replacement from seagate) and i'm running it off an external firewire hard drive.

definitely cool though, can't wait to get a new hard disk back in it.
F = ma
     
kick52
Baninated
Join Date: May 2005
Location: England
Status: Offline
Reply With Quote
Jun 20, 2006, 03:18 PM
 
i would love this on my powermac g4 (sig). go on it in the evening, make pm hybinate, go to bed, wake up, carry on.

is there anyway to get the hybinate function on my mac?
     
P
Moderator
Join Date: Apr 2000
Location: Gothenburg, Sweden
Status: Offline
Reply With Quote
Jun 20, 2006, 04:23 PM
 
Originally Posted by nerd
I'm not trying to derail this thread but I remember back in OS 9 I would sleep my Pismo for 2 weeks. Then OS 10 came out and now we get maybe 4 days (I've really never tested this). Was OS 9 writing things to disk? I remember it took a little longer to wake up.
It had a similar feature, yes. I think you could toggle it using a checkbox in the Energy Saver control panel, but that we were advised not to used it near the end because of data corruption issues. I think that was why Apple shelved it.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:27 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,