What are pinned objects?

2019-01-07 15:38发布

I am trying to find a memory leak using ants memory profiler, and I've encountered in a new term:

Pinned objects.

Can some one give me a good & simple explanation about what this objects are, How can I pinn/Unpinn objects, and detect who pinned objects?

Thanks

7条回答
来,给爷笑一个
2楼-- · 2019-01-07 16:31

A pinned object is one that is not allowed to move. The garbage collector is normally compacting the memory in that it moves all objects to "one or more clusters". This is to create large chunks of free space.

This basically means if someone else (outside) has a pointer to the memory address of an object, this may point to random content - as the object has moved.

Pinning an object tells the GC to NOT MOVE IT. This is normally useless and ONLY makes sense when working with pointers - like when using PInvoke. Sometimes you need to turn in an address to a structure (in the memory layout term), and if that is implemented in a class, you have to pin that.

To answer concrete:

  • You can not find out who pinned an obiect.
  • Pinning is done with the FIXED statement. This is only allowed in unsafe code.

Check:

http://msdn.microsoft.com/en-us/library/f58wzh21%28VS.80%29.aspx

查看更多
登录 后发表回答