简体   繁体   English

手动GC Gen2数据分配

[英]Manual GC Gen2 data allocation

I'm prototyping some managed directx game engine before moving to c++ syntax horror. 在转向c ++语法恐怖之前,我正在对一些托管Directx游戏引擎进行原型设计。 So let's say I've got some data (fe an array or a hashset of references) that I'm sure it'll stay alive throughout whole application's life. 因此,假设我有一些数据(例如数组或引用的哈希集),我确信它会在整个应用程序的生命周期中保持有效。 Since performance is crucial here and I'm trying to avoid any lag spikes on generation promotion, I'd like to ask if there's any way to initialize an object (allocate its memory) straight ahead in GC's generation 2? 由于性能在这里至关重要,并且我正努力避免在代升级方面出现任何滞后峰值,因此我想问一问是否有任何方法可以在GC的第二代中直接初始化对象(分配其内存)? I couldn't find an answer for that, but I'm pretty sure I've seen someone doing that before. 我找不到答案,但是我可以肯定我以前见过有人这样做。

Alternatively since there would be no real need to "manage" that piece of memory, would it be possible to allocate it with unmanaged code, but to expose it to the rest of the code as a .NET type? 或者,由于实际上不需要“管理”该内存,是否可以使用非托管代码分配它,而是将其作为.NET类型公开给其余代码?

You can't allocate directly in Gen 2. All allocations happen in either Gen 0 or on the large object heap (if they are 85000 bytes or larger). 您不能直接在Gen 2中进行分配。所有分配都在Gen 0或大型对象堆中进行(如果它们是85000字节或更大)。 However, pushing something to Gen 2 is easy: Just allocate everything you want to go to Gen 2 and force GCs at that point. 但是,将某些东西推到Gen 2很容易:只需分配您想要进入Gen 2的所有内容,然后在此时强制GC。 You can call GC.GetGeneration to inspect the generation of a given object. 您可以调用GC.GetGeneration来检查给定对象的生成。

Another thing to do is keep a pool of objects. 另一件事是保留对象池。 Ie instead of releasing objects and thus making them eligible for GC, you return them to a pool. 即,您可以将它们返回到池中,而不是释放对象并因此使它们符合GC的条件。 This reduces allocations and thus the number of GCs as well. 这减少了分配,因此也减少了GC的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM