I'm sure this answer will depends the user machine, but there must be some best practices on pinning data.
I need to hold like 5 arrays of bytes containing 1.048.576 bytes each. Normally I would prefer to use GCHandle
(managed) memory, but some people says it would slow down the GC. I know that can happen, but how much memory/objects need to be pinned to start to really affect the GC?
Here are the options that I have:
GCHandle.Alloc GCHandleType.Pinned
(managed). It will slow down GC??Marshal.AllocHGlobal
(unmanaged acess). unsafe codeBitmap
to hold data in Scan0 (unmanaged acess). unsafe codeOne of the most misunderstood parts of garbage collection is that it doesn't actually collect dead or unused objects; it collects used ones, the so-called survivors. Garbage collection is slow if most objects survive the collection process.
GC is slow, mostly because it needs to pause program execution to collect garbage. Think of it like this — your CPU can only work on one thing at a time.
Short of avoiding garbage collection altogether, there is only one way to make garbage collection faster: ensure that as few objects as possible are reachable during the garbage collection. The fewer objects that are alive, the less there is to be marked. This is the rationale behind the generational heap.
When a garbage collection is triggered, the garbage collector reclaims the memory that's occupied by dead objects. The reclaiming process compacts live objects so that they are moved together, and the dead space is removed, thereby making the heap smaller.
This is a hopelessly unanswerable question. Pinned objects do not slow down the GC that much, it is just a rock in the road when the GC compacts the heap. Easy enough to work around that rock by simply skipping the pinned section of the heap.
The far worse outcome is that it will have a lasting effect on the code that runs after the collection is completed. Since the heap isn't compacted that well, the locality of references is not as good so the processor won't be able to get as much mileage from the CPU caches. Quantifying that slow-down isn't possible, it greatly depends on the kind of code that runs afterwards. Only that it is worse and lasts for a while, until the next GC.
The only good advice is that, if you have to pin, then do so for as short amount of time as possible. And to avoid scenarios where a collection may occur while an object is pinned. Which, roughly, means that you avoid allocating memory while holding the pin. Not always practical if the program is running multiple threads, making the <gcServer>
element in the .config file attractive. Which selects a different GC strategy that uses a lot more memory but gives threads their own GC heap segments. There is no simple guidance to determine when to do this, profiling with realistic data sets is required.
Neither Marshal.AllocHGlobal nor Bitmap have any notable affect on the GC heap, they allocate from unmanaged memory heaps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With