Exploring .NET memory management (iSense)

Developer Advocate
Apr. 12, 2017
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
1 of 38

More Related Content

What's hot

Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Marcos García
Java GCJava GC
Java GCRay Cheng
Let's talk about Garbage CollectionLet's talk about Garbage Collection
Let's talk about Garbage CollectionHaim Yadid
[262] netflix 빅데이터 플랫폼[262] netflix 빅데이터 플랫폼
[262] netflix 빅데이터 플랫폼NAVER D2
JVM Garbage Collection TuningJVM Garbage Collection Tuning
JVM Garbage Collection Tuningihji
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningCarol McDonald

Similar to Exploring .NET memory management (iSense)

ConFoo - Exploring .NET’s memory management – a trip down memory laneConFoo - Exploring .NET’s memory management – a trip down memory lane
ConFoo - Exploring .NET’s memory management – a trip down memory laneMaarten Balliauw
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...
Exploring .NET memory management - A trip down memory lane - Copenhagen .NET ...Maarten Balliauw
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...Maarten Balliauw
Exploring .NET memory management - JetBrains webinarExploring .NET memory management - JetBrains webinar
Exploring .NET memory management - JetBrains webinarMaarten Balliauw
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
Low latency in java 8 by Peter Lawrey Low latency in java 8 by Peter Lawrey
Low latency in java 8 by Peter Lawrey J On The Beach

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

More from Maarten Balliauw

Nerd sniping myself into a rabbit hole... Streaming online audio to a Sonos s...Nerd sniping myself into a rabbit hole... Streaming online audio to a Sonos s...
Nerd sniping myself into a rabbit hole... Streaming online audio to a Sonos s...Maarten Balliauw
Building a friendly .NET SDK to connect to SpaceBuilding a friendly .NET SDK to connect to Space
Building a friendly .NET SDK to connect to SpaceMaarten Balliauw
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...
Microservices for building an IDE - The innards of JetBrains Rider - NDC Oslo...Maarten Balliauw
Indexing and searching NuGet.org with Azure Functions and Search - .NET fwday...Indexing and searching NuGet.org with Azure Functions and Search - .NET fwday...
Indexing and searching NuGet.org with Azure Functions and Search - .NET fwday...Maarten Balliauw
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...Maarten Balliauw
.NET Conf 2019 - Indexing and searching NuGet.org with Azure Functions and Se....NET Conf 2019 - Indexing and searching NuGet.org with Azure Functions and Se...
.NET Conf 2019 - Indexing and searching NuGet.org with Azure Functions and Se...Maarten Balliauw

More from Maarten Balliauw(20)

Recently uploaded

The Ultimate Administrator’s Guide to HCL Nomad WebThe Ultimate Administrator’s Guide to HCL Nomad Web
The Ultimate Administrator’s Guide to HCL Nomad Webpanagenda
How to use the Cataloguing Code Ethics at your day job : a hands-on workshop ...How to use the Cataloguing Code Ethics at your day job : a hands-on workshop ...
How to use the Cataloguing Code Ethics at your day job : a hands-on workshop ...CILIP MDG
Advancing Equity and Inclusion for Deaf Students in Higher EducationAdvancing Equity and Inclusion for Deaf Students in Higher Education
Advancing Equity and Inclusion for Deaf Students in Higher Education3Play Media
Navigating the FutureNavigating the Future
Navigating the FutureOnBoard
Announcing InfluxDB ClusteredAnnouncing InfluxDB Clustered
Announcing InfluxDB ClusteredInfluxData
Enterprise Application and Data Protection on AWS with Amazon FSx for NetApp ...Enterprise Application and Data Protection on AWS with Amazon FSx for NetApp ...
Enterprise Application and Data Protection on AWS with Amazon FSx for NetApp ...LilyJang3

Recently uploaded(20)

Exploring .NET memory management (iSense)

Editor's Notes

  1. https://pixabay.com/en/memory-computer-component-pcb-1761599/
  2. https://pixabay.com/en/tires-used-tires-pfu-garbage-1846674/
  3. Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  4. Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  5. Application roots: Typically, these are global and static object pointers, local variables, and CPU registers.
  6. 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!
  7. 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!
  8. 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.
  9. 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 
  10. Will print “true” twice.
  11. 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
  12. Code = trick question, what if we enter same value twice? String equals, reference not equals!
  13. How many strings are stored
  14. How many strings are stored
  15. 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, ...
  16. Open ClrMD.sln Show DumpStringDuplicates Count total strings For each string, store value + count Dump to console
  17. 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