79464573

Date: 2025-02-24 19:08:09
Score: 0.5
Natty:
Report link

Pinning (another answer) reduces collection efficiency by preventing compaction (leaving big bubbles of unusable space behind pinned objects). You ideally only want to run collections when they are likely to reclaim a lot of memory. A pinned object can prevent compaction and drastically limit reclamation. You end up paying the cost of collection without much to show for it. The more this happens, the more of your collections become wasted CPU cycles and pause times.

If you want to make sure you stay in the LOH, you could allocate your memory in one go and then carve it up into smaller Memory<T> sections. As it happens, this is not entirely unlike using Marshal to get unmanaged memory but you're letting the GC do the book-keeping for you :)

If the whole 586MB memory pool all has the same lifetime, then you're done. However, if there are some varying lifetimes involved, you could use a hybrid approach, where you allocate some 'midsize' arrays for distinct lifetimes (but all still large enough to land them in the LOH) and carve them into smaller Memory<T> sections as needed.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: FrancisH