.NET generation 0 heap size

后端 未结 7 1838
礼貌的吻别
礼貌的吻别 2021-01-01 21:21

Is it possible to set a minimal size of a generation 0 heap in .NET?

I have a folowing sistuation. I have a function that allocates around 20-30 MB of 1KB objects, d

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 21:46

    Thank you guys for your help.

    Whell, the sitution is very interesting. I've never mentioned that i'm storing those 30MB of objects in an array whoose size is 100000. I'm first allocating that array and then filling it with objects. Since that array is larger than 85K that array is stored in the Large Objects Heap. It turns out that for the garbage collection to collect objects in that heap, it needs to run gen2 collection, so every time there is not enough space in the Large Objects Heap, it runs the gen2 collector, which is trashing the performance. Here is the simple example that will constantly call gen2 collection. You need to run it in debug mode (because of compiler optimizations).

    static void Main(string[] args)
    {
         while (true)
         {
             byte[] objects = new byte[100000];
         }
    }
    

    Does this mean that whenever I need a temporary array whose size is larger than 85K, that array will end up in the larget objects heap?

提交回复
热议问题