多少的GCHandle固定内存/对象将使垃圾收集器速度变慢? [英] How much GCHandle pinned memory/objects would make Garbage Collector slows down?

查看:178
本文介绍了多少的GCHandle固定内存/对象将使垃圾收集器速度变慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这个答案将取决于用户机器,但必须有上牵制数据



我需要一些最佳实践持有类似包含每个1.048.576字节字节5阵列。通常我会更喜欢使用的GCHandle (管理)的内存,但有些人说,这将在GC放缓。我知道可能发生,但多少内存/对象需要被固定到开始真正影响到GC?



下面是选项我有:




  1. GCHandle.Alloc GCHandleType.Pinned (管理)。它会减慢GC ??

  2. Marshal.AllocHGlobal (非托管接取)。不安全的代码

  3. 使用位图在SCAN0(非托管接取)来保存数据。不安全的代码


解决方案

这是一个无可救药无法回答的问题。固定的对象不减速的GC这么多,它只是在路上一个石头当GC压缩堆。很容易的工作岩石周围简单地跳过堆的固定部分。



结果远远糟糕的是,它会对代码运行的收集完成后,效果持久。由于堆不板结那么好,引用当地也没有那么好,从而使处理器将无法从CPU高速缓存获得尽可能多的里程。量化该减速是不可能的,这在很大程度上取决于运行之后的那种代码。只有更是雪上加霜,持续了一段时间,直到下一次GC。



唯一的好建议是,如果你的有无的引脚话的时间尽可能短量这样做。并避免可能发生的集合的,而的对象是固定场景。其中,大概,这意味着您避免同时保持引脚分配内存。总是不踏实,如果在程序运行多个线程,使得< gcServer>在.config文件吸引力元素。它选择使用大量的内存,但给人的线程自己的GC堆的分段不同的GC策略。有没有简单的指导,以确定何时要做到这一点,与真实的数据集需要分析。



无论Marshal.AllocHGlobal还是位图有GC堆任何显着的影响,他们从非托管内存堆分配。


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:

  1. GCHandle.Alloc GCHandleType.Pinned (managed). It will slow down GC??
  2. Marshal.AllocHGlobal (unmanaged acess). unsafe code
  3. use a Bitmap to hold data in Scan0 (unmanaged acess). unsafe code

解决方案

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.

这篇关于多少的GCHandle固定内存/对象将使垃圾收集器速度变慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆