Re: Garbage collectable pinned arrays!



Atmapuri <janez.makovsek@xxxxxxx> wrote:
I don't see why you'd even want it to be collectible, actually. The
point of pinning is to let unmanaged code access the object without
worrying that the GC will move it. But collecting the object and
letting its memory be used for something else is just as dangerous as
moving it!

You are mixing two points:

- reference to unamanged memory, where the word pinned also
means that it won't be collected.
- location of the array in GC (Heap or else).

When the array is pinned it is copied to heap. All I would like
to see is an option to allocate the array on the heap initially.

Unless you use __stackalloc or fixed size buffers, arrays are always
allocated on the heap.

The array is automatically allocated on the heap once it exceeds
a certain size and thu's becomes "pinned", because the heap is
never compacted and all addresses are absolute.

The heap absolutely *is* compacted. Only the large object heap isn't
compacted as I recall - is that what you're talking about?

Therefore, the pinned keyword during memory allocation would
only instruct the GC where to put the array. From there on it
can work as usual.

The reason to put the array on the heap is to avoid the
need to pin it, if you have to pass it to unmanaged code
multiple times. Thus in turn improving performance in
such cases.

So, I would like to specify that the memory is allocated
on the heap, and before passing it to the external code,

I can still call GC.KeepAlive(), to prevent the array from
being collected if needed. That is still less code than
calling GCHandle.Alloc and multiple times faster than
current implementation which copies the array.

If you do that without pinning, your code will break when the GC
compacts the generation containing the array.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
.



Relevant Pages

  • Re: Garbage collectable pinned arrays!
    ... reference to it anywhere on the call stack. ... But collecting the object and ... When the array is pinned it is copied to heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collectable pinned arrays!
    ... reference to it anywhere on the call stack. ... But collecting the object and ... When the array is pinned it is copied to heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collectable pinned arrays!
    ... reference to it anywhere on the call stack. ... But collecting the object and ... When the array is pinned it is copied to heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collectable pinned arrays!
    ... reference to it anywhere on the call stack. ... But collecting the object and ... When the array is pinned it is copied to heap. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array Fundamentals
    ... When a value-type object is created, C# allocates a single ... the values inside an Array should be allocated ... reside on the heap ... >type is that only the reference type exist on the heap. ...
    (microsoft.public.dotnet.languages.csharp)