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:20

Pinning is used to keep the garbage collector from relocating objects. It can hurt performance by restricting what the garbage collector can do. In general pinned objects should be pinned for as short as time as possible.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-21 20:23

I thought this article was pretty good as well.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-21 20:29

One reason you have multiple generations in a garbage collector is to avoid losing memory to fragmentation.

Every function call can mean the creation and deletion collection of multiple objects, and so the memory heap for your program tends to fragment very quickly. This leaves holes behind that aren't very usable. The result is that your program needs to be periodically de-fragmented, just like a hard disk. This is part of what happens during a collection.

When an object survives a collection, it is moved to a longer-lived generation on the theory that if it survived one collection it will probably survive others. Thus the later generations have less turn-over and don't fragment as much. This means your program spends less time overall juggling things around to clean up holes and wastes less memory. This is also an improvement over traditional memory management (malloc/free or new/delete), which left it up to the operating system to manage any memory fragmentation.

The reason an object survives collection is because there is something somewhere that is still "in scope" and holds a reference to that object. There are a few ways you can cause this to happen and then forget about the reference, so it is possible to "leak" memory in managed code.

Sometimes people are tempted to call GC.Collect() in an effort to get the garbage collector to clean something up. Perhaps they've discovered they have a leak, or think memory is becoming over-fragmented. You should resist those urges. While garbage collection in .Net is not perfect, is it very good, and it's almost certainly much better at cleaning up memory than you are. Odds are that if an object can and should be collected, it will be. Remember that calling GC.Collect() can actually make things worse by helping the garbage collector move objects up to a higher generation, and thus keeping them around for longer than they otherwise would be.

Instead, if you suspect you have a leak, look to your own code for something like a global or static variable that might hold a reference to a lot of other items. The only time you should call GC.Collect() is when you have information about the nature of the program that is not available to the garbage collector, and that's pretty rare as the GC knows every reference you've created.

"Pinning" is for when you need to pass an object to an unmanaged library. The garbage collector can move an object's physical location in memory, and so you need to "pin" it in one place or the pointer used by the unmanaged library could become invalid. A pinned object cannot be collected, and so you shouldn't pin an object for any longer than necessary.

查看更多
狗以群分
5楼-- · 2019-01-21 20:33

Here is a short but complete program that I wrote to demostrate gen 2 garbage collection in a running application

http://nomorehacks.wordpress.com/2008/11/27/forcing-the-garbage-collector/

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-21 20:37

http://msdn.microsoft.com/en-us/library/ms973837.aspx

Very large topic. Above is a good summary from MS.

查看更多
Ridiculous、
7楼-- · 2019-01-21 20:39

This is an interesting article on the topic that you might find informative.

查看更多
登录 后发表回答