How do I get .NET to garbage collect aggressively?

后端 未结 12 775
情书的邮戳
情书的邮戳 2020-12-02 14:33

I have an application that is used in image processing, and I find myself typically allocating arrays in the 4000x4000 ushort size, as well as the occasional float and the l

12条回答
  •  萌比男神i
    2020-12-02 15:09

    The GC doesn't take into account the unmanaged heap. If you are creating lots of objects that are merely wrappers in C# to larger unmanaged memory then your memory is being devoured but the GC can't make rational decisions based on this as it only see the managed heap.

    You end up in a situation where the GC doesn't think you are short of memory because most of the things on your gen 1 heap are 8 byte references where in actual fact they are like icebergs at sea. Most of the memory is below!

    You can make use of these GC calls:

    • System::GC::AddMemoryPressure(sizeOfField);
    • System::GC::RemoveMemoryPressure(sizeOfField);

    These methods allow the GC to see the unmanaged memory (if you provide it the right figures).

提交回复
热议问题