Garbage collection in .NET (generations)

2019-01-21 19:58发布

I have read a lot of .NET performance articles that describe Gen1,Gen2 garbage collection and objects surviving the generations.

Why does objects survives the collection?

What is pinning?

How do I learn more about this?

7条回答
霸刀☆藐视天下
2楼-- · 2019-01-21 20:40

http://blogs.msdn.com/maoni/ is a good resource.
Asking questions here helps too :)

For your questions:

Why do objects survive the collection:
Objects survive a collection when they are "live objects", or "Reachable objects". Reachable objects are objects where there is a reference to them from another object that is on:

  1. The stack
  2. The finalization queue
  3. another object in a higher generation ( for example a Gen2 Object holding a reference to a Gen0 Object )
  4. The handle table ( a data structure used by the CLR, and require a seperate post on its own:) )

What is Pinning:
Pinning means making sure the object doesn't move in memory. objects move in memory as a result of a compacting GC, you can create a GCHandle of typed pinned if you want to pin an object, pinning also happens automatically behind the sciene for objects that are passed to native code via PInvoke ( like strings that are passed as output, the internal buffer gets pinned during the call to PInvoke ).

Check out http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx for a good example about GCHandle.

查看更多
登录 后发表回答