Successfully reported this slideshow.
Your SlideShare is downloading. ×

Exploring .NET memory management (iSense)

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 38 Ad

Exploring .NET memory management (iSense)

Download to read offline

The .NET Garbage Collector (GC) is really cool. It helps providing our applications with virtually unlimited memory, so we can focus on writing code instead of manually freeing up memory. But how does .NET manage that memory? What are hidden allocations? Are strings evil? It still matters to understand when and where memory is allocated. In this talk, we’ll go over the base concepts of .NET memory management and explore how .NET helps us and how we can help .NET – making our apps better. Expect profiling, Intermediate Language (IL), ClrMD and more!

The .NET Garbage Collector (GC) is really cool. It helps providing our applications with virtually unlimited memory, so we can focus on writing code instead of manually freeing up memory. But how does .NET manage that memory? What are hidden allocations? Are strings evil? It still matters to understand when and where memory is allocated. In this talk, we’ll go over the base concepts of .NET memory management and explore how .NET helps us and how we can help .NET – making our apps better. Expect profiling, Intermediate Language (IL), ClrMD and more!

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to Exploring .NET memory management (iSense) (20)

Advertisement

More from Maarten Balliauw (20)

Recently uploaded (20)

Advertisement

Exploring .NET memory management (iSense)

  1. 1. Exploring .NET memory management A trip down memory lane Maarten Balliauw @maartenballiauw
  2. 2. Agenda Garbage Collector Helping the GC Allocations & hidden allocations Strings Exploring the heap
  3. 3. Garbage Collector
  4. 4. Memory management and GC Virtually unlimited memory for our applications Big chunk of memory pre-allocated Runtime manages allocation in that chunk Garbage Collector (GC) reclaims unused memory, making it available again
  5. 5. .NET memory management 101 Memory allocation Objects allocated in “managed heap” (big chunk of memory) Allocating memory is fast, it’s just adding a pointer Some unmanaged memory is also consumed (not GC-ed) .NET CLR, Dynamic libraries, Graphics buffer, … Memory release or “Garbage Collection” (GC) Generations Large Object Heap
  6. 6. .NET memory management 101 Memory allocation Memory release or “Garbage Collection” (GC) 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 Object unreachable? Remove object, release memory, compact heap Takes time to scan all objects! Generations Large Object Heap
  7. 7. .NET memory management 101 Memory allocation Memory release or “Garbage Collection” (GC) Generations Large Object Heap Generation 0 Generation 1 Generation 2 Short-lived objects (e.g. Local variables) In-between objects Long-lived objects (e.g. App’s main form)
  8. 8. .NET memory management 101 Memory allocation Memory release or “Garbage Collection” (GC) Generations Large Object Heap (LOH) Special segment for large objects (>85KB) Collected only during full garbage collection Not compacted (by default) Fragmentation can cause OutOfMemoryException
  9. 9. The .NET garbage collector When does it run? Vague… But usually: Out of memory condition – when the system fails to allocate or re-allocate memory After some significant allocation – if X memory is allocated since previous GC Failure of allocating some native resources – internal to .NET Profiler – when triggered from profiler API Forced – when calling methods on System.GC Application moves to background GC is not guaranteed to run http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx http://blogs.msdn.com/b/abhinaba/archive/2008/04/29/when-does-the-net-compact-framework-garbage-collector-run.aspx
  10. 10. The .NET garbage collector Runs very often for gen0 Short-lived objects, few references, fast to clean Local variable Web request/response Higher generation Usually more references, slower to clean GC pauses the running application to do its thing Usually short, except when not… Background GC (enabled by default) Concurrent with application threads May still introduce short locks/pauses, usually just for one thread
  11. 11. Helping the GC, avoid pauses Optimize allocations (but don’t do premature optimization – measure!) Don’t allocate at all Make use of IDisposable / using statement Clean up references, giving the GC an easy job Finalizers Beware! Moved to finalizer queue -> always gen++ Weak references Allow the GC to collect these objects, no need for checks
  12. 12. Helping the GC DEMO https://github.com/maartenba/memory-demos
  13. 13. Allocations
  14. 14. When is memory allocated? Not for value types (int, bool, struct, decimal, enum, float, byte, long, …) Allocated on stack, not on heap Not managed by garbage collector For reference types When you new When you ""
  15. 15. Hidden allocations! Boxing! Put and int in a box Take an int out of a box Lambda’s/closures Allocate compiler-generated DisplayClass to capture state Params arrays And more! int i = 42; // boxing - wraps the value type in an "object box" // (allocating a System.Object) object o = i; // unboxing - unpacking the "object box" into an int again // (CPU effort to unwrap) int j = (int)o;
  16. 16. How to find them? Experience Intermediate Language (IL) Profiler “Heap allocations viewer” ReSharper Heap Allocations Viewer plugin Roslyn’s Heap Allocation Analyzer Don’t do premature optimization – measure!
  17. 17. Hidden allocations DEMO https://github.com/maartenba/memory-demos ReSharper Heap Allocations Viewer plugin Roslyn’s Heap Allocation Analyzer
  18. 18. Don’t optimize what should not be optimized.
  19. 19. Measure! We know when allocations are done... ...but perhaps these don’t matter. Measure! How frequently are we allocating? How frequently are we collecting? What generation do we end up on? Are our allocations introducing pauses? www.jetbrains.com/dotmemory (and www.jetbrains.com/dottrace)
  20. 20. Garbage Collector & Allocations GC is optimized for high memory traffic in short-lived objects Use that knowledge! Don’t fear allocations! Don’t optimize what should not be optimized… GC is the concept that makes .NET / C# tick – use it! Know when allocations happen GC is awesome Gen2 collection that stop the world not so much… Measure!
  21. 21. Strings
  22. 22. Strings are objects .NET tries to make them look like a value type, but they are a reference type Read-only collection of char Length property A bunch of operator overloading Allocated on the managed heap var a = new string('-', 25); var b = "Hello, World!"; var c = httpClient.GetStringAsync("http://blog.maartenballiauw.be");
  23. 23. Measuring string allocations DEMO https://github.com/maartenba/memory-demos
  24. 24. String duplicates Any .NET application has them (System.Globalization duplicates quite a few) Are they bad? .NET GC is fast for short-lived objects, so meh. Don’t waste memory with string duplicates on gen2 (but: it’s okay to have strings there)
  25. 25. String literals Are all strings on the heap? Are all strings duplicated? var a = "Hello, World!"; var b = "Hello, World!"; Console.WriteLine(a == b); Console.WriteLine(Object.ReferenceEquals(a, b)); Prints true twice. So “Hello World” only in memory once?
  26. 26. Portable Executable (PE) #UserStrings DEMO https://github.com/maartenba/memory-demos
  27. 27. String literals in #US Compile-time optimization Store literals only once in PE header metadata stream ECMA-335 standard, section II.24.2.4 Reference literals (IL: ldstr) var a = Console.ReadLine(); var b = Console.ReadLine(); Console.WriteLine(a == b); Console.WriteLine(Object.ReferenceEquals(a, b)); String interning to the rescue!
  28. 28. String interning Store (and read) strings from the intern pool Simply call String.Intern when “allocating” or reading the string Scans intern pool and returns reference var url = string.Intern("http://blog.maartenballiauw.be"); var stringList = new List<string>(); for (int i = 0; i < 1000000; i++) { stringList.Add(url); }
  29. 29. String interning caveats Why are not all strings interned by default? CPU vs. memory Not on the heap but on intern pool No GC on intern pool – all strings in memory for AppDomain lifetime! Rule of thumb Lot of long-lived, few unique -> interning good Lot of long-lived, many unique -> no benefit, memory growth Lot of short-lived -> trust the GC Measure!
  30. 30. Exploring the heap for fun and profit
  31. 31. Memory layout Pointer to an “instance” Instance: Pointer to RTTI Property1 Property2 … RTTI: Property types and names Methods …
  32. 32. Theory is nice... Microsoft.Diagnostics.Runtime (ClrMD) “ClrMD is a set of advanced APIs for programmatically inspecting a crash dump of a .NET program much in the same way that the SOS Debugging Extensions (SOS) do. This allows you to write automated crash analysis for your applications as well as automate many common debugger tasks. In addition to reading crash dumps ClrMD also allows supports attaching to live processes.” Maarten’s definition: “LINQ-to-heap”
  33. 33. ClrMD introduction DEMO https://github.com/maartenba/memory-demos
  34. 34. String duplicates DEMO https://github.com/maartenba/memory-demos
  35. 35. “Path to root” (why is my object kept in memory) DEMO https://github.com/maartenba/memory-demos
  36. 36. Conclusion
  37. 37. Conclusion Garbage Collector (GC) optimized for high memory traffic + short-lived objects Don’t fear allocations! But beware of gen2 “stop the world” String interning when lot of long-lived, few unique Don’t optimize what should not be optimized… Measure! Using a profiler/memory analysis tool ClrMD to automate inspections dotMemory Unit, Benchmark.NET, … to profile unit tests Blog series: https://blog.maartenballiauw.be
  38. 38. Thank you! http://blog.maartenballiauw.be @maartenballiauw

Editor's Notes

  • https://pixabay.com/en/memory-computer-component-pcb-1761599/
  • https://pixabay.com/en/tires-used-tires-pfu-garbage-1846674/
  • Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  • Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  • Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  • Open TripDownMemoryLane.sln
    Show WeakReferenceDemo (demo “1-1”)
    Explain weak reference allows GC to collect reference
    Show Cache object – has weak references to data, we expect these to probably be cleaned up by GC
    Attach profiler, run demo “1-1”, snapshot, see 20 instances of WeakReference<Data>
    Snapshot again, compare – see WeakReference<Data> has been regenerated a couple of times
    Show DisposeObjectsDemo (demo “1-2”)
    Explain first demo does not dispose and relies on GC + finalizers. This will mean our object remains in memory for two GC cycles!
    Explain dispose does clean them up and requires only one cycle
    In SampleDisposable, explain GC.SuppressFinalize -> tell the GC no finalizer queue work is needed here!
  • Open TripDownMemoryLane.sln
    Show Demo02_Random
    Open IL viewer tool window, show what happens in IL for each code sample
    Explain IL viewer + hovering statements to see what they do
    BoxingRing() – show boxing and unboxing statements in IL, explain they consume CPU and allocate an object
    ParamsArray() – the call to ParamsArrayImpl() actually allocates a new string array! CPU + memory
    AverageWithinBounds() – temporary class is created to capture state of all variables, then passed around
    IL_0000: newobj instance void TripDownMemoryLane.Demo02.Demo02_Random/'<>c__DisplayClass3_0'::.ctor()
    Lambdas() – same thing, temporary class to capture state in the loop
    IL_001f: newobj instance void Allocatey.Talk.Demo02_Random/'<>c__DisplayClass4_0'::.ctor()
    Show Demo02_ValidateArgumentsDemo – this one is fun!
    Explain what we want to do: build a guard function – check a condition, show error
    First one is the easy one, but it allocates a string and runs string.Format
    Second one is better – does not allocate the string! But does allocate a function and a state capture...
    Third one – allocates an array (params)
    Fourth one – no allocations, yay! Using overloads...
    Show heap allocations viewer!
  • There is an old adage in IT that says “don’t do premature optimization”. In other words: maybe some allocations are okay to have, as the GC will take care of cleaning them up anyway. While some do not agree with this, I believe in the middle ground.

    The garbage collector is optimized for high memory traffic in short-lived objects, and I think it’s okay to make use of what the .NET runtime has to offer us here. If it’s in a critical path of a production application, fewer allocations are better, but we can’t write software with zero allocations - it’s what our high-level programming language uses to make our developer life easier. It’s not okay to have objects go to gen2 and stay there when in fact they should be gone from memory.

    Learn where allocations happen, using any of the above methods, and profile your production applications frequently to see if there are large objects in higher generations of the heap that don’t belong there.
  • Open TripDownMemoryLane.sln
    Show StringAllocationsDemo (demo “4”)
    Show AllocateSomeStrings, mention a few strings will be allocated (a, b and c)
    AllocateSomeStringDuplicates – same thing, but a lot of strings! In loop, every string wil be added to memory, crazy!
    Run with dotMemory attached, capture snapshot
    See string duplicates!
    Just for fun, attach to devenv.exe 
  • Will print “true” twice.
  • Open our demo application in dotPeek
    Explain PE headers
    Show #US table
    Open StringAllocationDemo class. Jump to IL code, show ldstr statement for strings that are in #US table
  • Code = trick question, what if we enter same value twice? String equals, reference not equals!
  • How many strings are stored
  • How many strings are stored
  • Open ClrMD.sln
    Explain: two projects, one target application, one running ClrMD to analyze what we have
    Open ClrMD.Explorer.Program, show attaching ClrMD
    Get CLR version – gets info about the current CLR version
    Get runtime – gets info about the actual runtime hosting our app
    Show DumpClrInfo – get info, stress DAC data access components location – defines the runtime structures, used by ClrMD and VS Debugger etc to explore runtime while debugging/profiling/...
    Explore DumpHeapObjects, stress the heap structure
    Loop object addresses - foreach (var objectAddress in generation)
    Get type of object at address - var type = heap.GetObjectType(objectAddress.Ptr);
    Use type info to get value - type.GetValue(objectAddress.Ptr)
    Explore type autocomplete – structure to get enum, method addresses, ...
  • Open ClrMD.sln
    Show DumpStringDuplicates
    Count total strings
    For each string, store value + count
    Dump to console
  • Open ClrMD.sln
    Run ClrMd.Target with dotMemory attached
    Show Clock object retention path
    Explain what this means (object held in memory because...)
    Show ClrMd.Target code, explain in code
    Can we build this type of analysis ourselves? Yes we can!
    Show DumpRetention
    Enumerate all objects, find our Clock object (get type of object at address, compare)
    When we have the address of our object, enumerate all object roots (all trees of objects that are in use)
    Walk all of these trees and find our object address
    If found, we’re done!
    Run it, show output, show DGML output as well

×