Skip to content

Latest commit

 

History

History
78 lines (69 loc) · 4.63 KB

File metadata and controls

78 lines (69 loc) · 4.63 KB

Allocations in .net

This presentation is an excuse, dedicated to my students who heard from me through the years a lot of lies.

Examples = My corrected misunderstandings

  1. Allocations are expensive - No, expensive is Garbage collection
    • Allocation is move pointer of next free space after the newly allocated object
    • Large object allocation is expensive, because CLR guaranties its cleared.
    • GC does not count references to an object, it searches unreachable object from the roots (mark and sweep strategy)
    • Generations try to prevent memory fragmentation
    • There is a difference between Server and Client GC, ephemeral segment size
    • We have more heaps in .net (GC generations, + e.g. large objects/85 KB heap + pinned objects (.net 5+)), (Usually people mention 3x heap for each generation - not mentioned in the documentation)
    • Understand that stack has its limitations (max. size 1 MB by default, e.g number of frames)
    • Large object heap doesnt compact the memory, because it is performance hit
    • LOH and generation 2 are collected together
  2. Value types are always on stack
    • Static members are on heap, because they need to be reachable from everywhere, roots from beginning, since they are known and we may organize the memory
    • Compare boxing of input/output parameters
    • They are part of reference types
    • Call by reference/value
  3. The type is not always what you thing
    • Compare value/reference types
    • String and arrays are reference types
    • Tuple/ValueTuple, Task/ValueTask
  4. Closures are optimized
    • Method inside method or lambda
    • Captured local variable as state create

Takeaways

  • Always measure before refactoring
  • Think about garbage collections, because Allocation is cheap
  • Platform matters, you need to know what compiler generates, how your app is configured
  • Allocate a pool of large objects that you reuse instead of allocating temporary ones

Based on following resources