dotMemoryNotes

Exploring NET’s Memory Management

A Trip Down Memory Lane

Hurriedly made notes from the “Exploring NET’s Memory Management A Trip Down Memory Lane” webcast.

GC

The garbage collector is part of the .NET runtime.
We compile to IL, the runtime compiles this to machine code. It handles type safety, security, threads and memory.

  • Virtually unlimited memory for our applications. You don’t have to consider memory allocation for the most part.
  • Big chunk of memory allocated when application starts. When we declare a variable it is added to this allocation.
  • The GC reclaims memory. Objects allocated in the managed heap. This makes it fast as it just adds a pointer.
  • .NET used some unmanaged memory for use itself.
  • GC releases objects no longer in use by examining application roots.
  • GC builds a graph of all the objects that are reachable from these roots. It takes time to scan these objects.
  • Generations
  • Large Object Heap.
  • The GC divides the heap into generations.
    • Gen-0: Short lived (e.g. local variable).
    • Gen-1: In-between’
    • Gen-2: Long-lived objects (e.g. Application’s main form).
  • When object scans, if it’s still needed it will promote the object to a higher generation.
  • Large Object Heap (LOH)
    • Special segment for large objects (>85Kb).
    • Collected only during full garbage collection.
    • Not compacted (by default)
    • Fragmentation can cause OutOfMemoryException.
  • When does GC run?
    • Out of memory condition.
    • After some ‘significant’ allocation.
    • Failure to allocate some native resource (internal to .NET).
    • Profiler - when triggered from the profiler API.
    • Forced - Call to System.GC.
    • Application moved to background.
  • GC pauses the running application to scan the heap.
  • Helping the GC. Use IDisposable & using.
  • Finalizers: BEWARE!! The object will move to the finalizer queue, which will always cause the object to be promoted a generation.
  • Weak references: Allow the GC to collect these objects.
    • Whenever the GC passes through it, it will be collected. Speeds up scan.

When is Memory Allocated?

  • Not for value types
    • Allocated on the stack.
  • Reference types.
    • new
    • “” (string.Empty)
  • Boxing.
  • Closures.
  • Param arrays.
  • … more.
  • How to see?
    • Resharper Heap Allocations Viewer plugin.
    • Roslyn’s Heap Allocation Analyzer.

GC

  • GC is optimised for high memory traffic in short lived objects.
  • *

Strings

  • Strings are objects.
    • Is actually a readonly collection of char.
  • String duplicates are normal.
    • .NET GC is fast.
    • They represent the balance on CUP v memory.
    • Gen-0 is nothing, gen-2 might be bad.

How can we tell is a string is a duplicate and is in the Intern Pool?

var a = "Hello World!";
var b = "Hello World!";

// a == b is true
// Object.ReferenceEquals(a, b) is true

Heap

  • Pointer to Run Time Type Information (RTTI)

clrMD

  • Open crash dumps or monitor running processes.

Thanks for reading.

Feel free to contact me @BanksySan!

No comments:

Post a Comment