What is the relationship between Lion's Resume feature and Safe Sleep? I have disabled the "Restore windows when quitting and re-opening apps" checkbox in the General preferences pane in Lion, but after a recent sudden power loss (no battery) my iMac restarted in exactly the same state as I had left it the night before. What feature of Lion is making this (miracle) possible, and how do I ensure that it remans enabled? 



I read for example that I can "disable Resume", because it "duplicates" something called "Safe Sleep", but I'm not sure what any of these features are, how they are related, or how I can control them. For example, does the behavior I saw in my recent power loss mean that my machine had gone to sleep between my last use and the power failure, or is some kind of snapshotting taking place continuously in the background? I also understand that there are different "kinds" of sleep. If sleep is indeed necessary, do different triggers (keyboard, script, idle, etc.) for sleep result in different "kinds" of sleep.
Resume, counterintuitively, does not actually save anything itself. It simply relaunches all the applications that were running when you shut your computer down. It's the job of those Lion-compatible applications to restore their windows.

Safe sleep, on the other hand, saves a snapshot of your RAM to your HDD when you put your computer to sleep. In case of power failure, the machine boots from this file on startup.

Safe sleep is the only time (that I know of) that OS X saves its RAM.

You can use the pmset command line utility (specifically the sudo pmset -a hibernatemode x command where x is the sleep mode) to change the sleep mode, although there is no significant benefit in doing this.

The three main different kinds of sleep are as follows (from the pmset man page):


  hibernatemode = 0 (binary 0000) by default on supported desktops. The
  system will not back memory up to persistent storage. The
       system must wake from the contents of memory; the system will lose context on power loss. This is, historically, plain old sleep.
  
  hibernatemode = 3 (binary 0011) by default on supported portables. The system will store a copy of memory to persistent
  storage
       (the disk), and will power memory during sleep. The system will wake from memory, unless a power loss forces it to restore from
       disk image.
  
  hibernatemode = 25 (binary 0001 1001) is only settable via pmset. The system will store a copy of memory to persistent storage
       (the disk), and will remove power to memory. The system will restore from disk image. If you want "hibernation" - slower sleeps,
       slower wakes, and better battery life, you should use this setting.


As I stated before, configuring the sleep mode is not recommended (again from the pmset man page):


  We do not recommend modifying hibernation settings. Any changes you
  make are not supported. If you choose to do so anyway, we recommend
  using one of these three settings. For your sake and mine, please
  don't use anything other 0, 3, or 25.


I don't know about the script vs. user issue, although I highly doubt there is a difference.