What are pinned objects?

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

Leave a Comment