使用GC.AddMemory pressure与非托管资源的要点是什么?与非、要点、资源、GC

由网友(忘了你亡了我)分享简介:我已经知道了这个问题,通过C#的MSDN和CLR。I've read about this issue on MSDN and on CLR via c#.想象一下,我们有分配2MB的非托管HBITMAP和8字节管理的位图指向它。什么是讲述它的气相色谱AddMemory pressure如果它永远不会是能够使任何有...

我已经知道了这个问题,通过C#的MSDN和CLR。

I've read about this issue on MSDN and on CLR via c#.

想象一下,我们有分配2MB的非托管HBITMAP和8字节管理的位图指向它。什么是讲述它的气相色谱AddMemory pressure如果它永远不会是能够使任何有关对象,因为它被分配的非托管资源,因此,不容易受到垃圾收集点?

Imagine we have a 2Mb unmanaged HBITMAP allocated and a 8 bytes managed bitmap pointing to it. What's the point of telling the GC about it with AddMemoryPressure if it is never going to be able to make anything about the object, as it is allocated as unmanaged resource, thus, not susceptible to garbage collections?

推荐答案

的AddMemory pressure的一点是要告诉垃圾收集器,有一个大容量的内存分配与该对象。如果它是不受管理,垃圾收集器不知道这件事;只有管​​理的部分。由于管理的部分相对较小,GC可以让它通过垃圾收集好几次,可能需要被释放,基本上浪费内存。

The point of AddMemoryPressure is to tell the garbage collector that there's a large amount of memory allocated with that object. If it's unmanaged, the garbage collector doesn't know about it; only the managed portion. Since the managed portion is relatively small, the GC may let it pass for garbage collection several times, essentially wasting memory that might need to be freed.

是的,你仍然需要手工分配和释放的非托管内存。你无法摆脱的。你只需要使用AddMemory pressure以确保GC知道它的存在。

Yes, you still have to manually allocate and deallocate the unmanaged memory. You can't get away from that. You just use AddMemoryPressure to ensure that the GC knows it's there.

编辑:

好,以防其中一个,我可以做到这一点,但它会做没有什么大的区别,因为GC将不能够做些我喜欢的类型的事情,如果我理解正确的是:1)我会宣布我的变量,8个字节管理,2MB非托管字节。然后我会使用它,需要处理,所以非托管的内存被释放。现在它只会ocuppy 8个字节。现在,我的眼睛,已经停靠在年底b​​eggining AddMemory pressure和将removememory pressure就不会取得什么不同。什么是我收到错了吗?对不起,如此是烦人这个的 - 豪尔赫·布兰科

Well, in case one, I could do it, but it'd make no big difference, as the GC wouldn't be able to do a thing about my type, if I understand this correctly: 1) I'd declare my variable, 8 managed bytes, 2mb unmanaged bytes. I'd then use it, call dispose, so unmanaged memory is freed. Right now it will only ocuppy 8 bytes. Now, to my eyes, having called in the beggining AddMemoryPressure and RemoveMemoryPressure at the end wouldn't have made anything different. What am I getting wrong? Sorry for being so anoying about this. -- Jorge Branco

我想我看到您的问题。

是的,如果你能保证你随时调用处置,然后是,你不必费心AddMemory pressure和将removememory pressure 。没有等价,由于参考仍然存在,类型永远不会被收集。

Yes, if you can guarantee that you always call Dispose, then yes, you don't need to bother with AddMemoryPressure and RemoveMemoryPressure. There is no equivalence, since the reference still exists and the type would never be collected.

这就是说,你仍然想使用AddMemory pressure和将removememory pressure,为了完整的缘故。如果,例如,你的类的用户忘记调用Dispose?在这种情况下,假设你实施了处置方式不当,你最终会收回非托管字节定稿,即当管理对象被收集。在这种情况下,所需的存储器pressure到仍处于活动状态,从而使该对象更容易被回收。

That said, you still want to use AddMemoryPressure and RemoveMemoryPressure, for completeness sake. What if, for example, the user of your class forgot to call Dispose? In that case, assuming you implemented the Disposal pattern properly, you'll end up reclaiming your unmanaged bytes at finalization, i.e. when the managed object is collected. In that case, you want the memory pressure to still be active, so that the object is more likely to be reclaimed.

阅读全文

相关推荐

最新文章