Re: Garbage collectable pinned arrays!
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 12 Feb 2008 23:51:35 +0100
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.221c2857a51749bf8d2@xxxxxxxxxxxxxxxxxxxxxxx
Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:
<snip>
Jon, what you are doing here is not directly comparable with what the OP is
doing, but it illustrates the overhead taken by "protecting" an object in
memory.
The OP is measuring the overhead of a GCHandle.Alloc, which is not what
happens when passing an object during a call into unmanaged and not what
happens when interop 'pins' the object during a GC run.
By using a "fixed" and "unsafe" code block, you are effectively creating a
pointer, the OP is creating "pinned object handles".
Do we know that the OP can't actually use pointers? I probably missed a
post somewhere.
I don't know either, this thread is simply derailed as it is. As far as I understand, the OP thinks that pinning is
exactly what is done by GCHandle.Alloc with GCHandleType.Pinned, that's why he is measuring this methods overhead. But interop doesn't pin implicitely, it pins the arguments when needed. It's so easy to watch this when running some code in an unmanaged debugger, provide you know how to use your tools ;-).
It's also possible to inspect what happens when a GC comes along, the problem is that it takes some time to find out where to set the break-points.
Basically what the JIT does is nothing more than taking the address of the
first element of an array, put the address of the object (the array) in the
"Pointer Table" ( which is part of the GCInfo table) together with some
tracking info. The array is not really "pinned", but the GC knows how to
deal with this, that is, he inspects the pointer table before he starts a
scan, so he will not move or collect the object as long as the current
instruction falls in the range of the tracking info of this pointer.
Making sure that an object isn't moved or collected sounds like pinning
it to me. It's pinned as far as the IL goes, too - the variable is
explicitly marked with the "pinned" flag.
Yes, but what's been produced by the JIT is not the same as you "really" pin an object at run-time, say by using GCHandle.Alloc with GCHandleType.Pinned, and it's also not exactly the same as done by the Interop layer.
Fixed code blocks and pointers are the cheapest way to handle "pinned" buffers, this is quite normal as it can be done a compile time, which is not the case when using interop. Also, note that fixed was introduced after the initial release of the framework, the Pointer table did not exists in the first versions of the CLR.
Now, that may not be doing everything the OP is talking
about - but I don't see how it doesn't count as being pinned.
What I mean is, that it's not "pinned", like it's done when calling GCHAndle.Alloc, that doesn't mean the objects address isn't fixed for the duration of the scope of the declaring block.
Actually, there is no "pinned reference" in the GCInfo table (the pinned object table) when using "fixed", nor is there a bit set in the object header.
Not sure however what kind of "pinning" is used by the interop layer , but I'm sure it isn't the same as done with unsafe/fixed. I know that in V1.1 it was done by setting a bit in the objects header (for both PInvoke and COM interop), but possibly this has changed since more bits are used for other purposes.
Willy.
.
- Follow-Ups:
- Re: Garbage collectable pinned arrays!
- From: Jon Skeet [C# MVP]
- Re: Garbage collectable pinned arrays!
- References:
- Re: Garbage collectable pinned arrays!
- From: Jon Skeet [C# MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Jon Skeet [C# MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Atmapuri
- Re: Garbage collectable pinned arrays!
- From: Jon Skeet [C# MVP]
- Re: Garbage collectable pinned arrays!
- From: Willy Denoyette [MVP]
- Re: Garbage collectable pinned arrays!
- From: Jon Skeet [C# MVP]
- Re: Garbage collectable pinned arrays!
- Prev by Date: Re: Multithreaded GC!
- Next by Date: Re: Async Socket Disconnect Problem
- Previous by thread: Re: Garbage collectable pinned arrays!
- Next by thread: Re: Garbage collectable pinned arrays!
- Index(es):
Relevant Pages
|
Loading