开发者

How does Windows Hibernation works

Out of curiosity, I was looking for an article/documentation on "how windows hibernate option work", i.e. when one selects "Hibernate" option in windows shutdown dialog. The reply I got from some sources was that, its mere serialization of memory and registers.

Pardon me if I am wrong here. If windows coul开发者_C百科d serialize any applications, process or objects regardless of whether its serializable or non-serializable, how come .NET limits serializable objects to those with [Serializable] attribute or ISerializable interface?


Inside a process address-space, everything is just bytes; some stack, some managed heap, etc. Bytes are inherently serializable - they are just bytes. All hibernate has to do is suspend the threads and write the entire address space to disk.

With objects, you want to save them to some out-of-memory structure. Unfortunately, it makes no sense to store addresses etc, as it is exceptionally unlikely to rehydrate into exactly the same point in memory. Additionally, many things like unmanaged object handles will make no sense when rehydrated. It is also extremely likely that you want to save just a small block of objects, not an entire process space. And even in a small graph, those objects could be scattered all over the place - so you can't just copy out a few pages of memory.

Also keep in mind that a common use of serialization is to deep-clone objects; if you relied on the in-memory representation of objects, you would have to deserialize to exactly the same place in memory - so you can't have cloned anything. And that is *before you touch on concepts such as compacting garbage collectors, which move objects around in memory while you aren't looking.

Also consider that you might be loading the data into a different platform / architecture, or want to write a specific format (xml, json, etc).

So instead of just copying raw memory, serialization code must look at individual objects, traversing references and writing an object graph in a way that allows rehydration from a source that has nothing at all to do with raw memory. Much harder.


These are two very different things. Windows has control of what is in memory at any given time, and has direct access to the hardware. "Serialization" is one word to describe what it does to support hibernation I suppose, but it's not the same concept as in the CLR.

In .NET serialization is an explicit operation generally initiated by the developer; the attribute tells the framework that your type marked with it can be streamed out and back in without worrying about state or variance in behavior.

Technically the CLR can serialize anything I suppose, after all it has access to the underlying representation of each type, and keeps track of each object instance. So I guess you could "hibernate" an entire app domain at any given point; that would be closer to what Windows does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜