Even if your program is just a few lines of code, .NET's runtime will create a number of object in memory. Are all objects being destroyed by the garbage collector? Or is there a potential memory leak? And why is the application seemingly slow when having lots of objects in memory? In this webinar, we'll explore the new dotMemory 4 memory profiler. We'll see why we want to use a memory profiler and how easy it is to use JetBrains dotMemory for that.
2. Who am I?
Maarten Balliauw
Belgium (Antwerp)
Technical Evangelist, JetBrains
Focus on web
ASP.NET MVC, Windows Azure, ...
PHP, ...
Big passion: Cloud (Windows Azure)
Home brewer
http://blog.maartenballiauw.be
@maartenballiauw
5. Agenda
Why use a tool like dotMemory?
What happened to dotTrace Memory?
.NET Memory Management 101
Exploring dotMemory
UI concepts
Finding a memory leak
Analyzing memory traffic
6. Why use a tool like dotMemory?
.NET creates numerous objects in memory, even for simple programs
Memory is constantly being assigned and released (memory traffic)
What if some memory is retained while we expected it to be released?
7. Why use a tool like dotMemory?
dotMemory tries to answer:
Why is this object still in memory?
What causes the memory leak?
What takes so much memory?
How much memory traffic is going on?
Are there any memory allocation/distribution patterns violated?
E.g. event handlers not being unregistered, sparse arrays, …
Don’t just use it when things go wrong!
8. What does dotMemory do for me?
Collect memory snapshot information
Info about all objects on the managed heap at the moment a snapshot is
taken
Collect memory traffic information
How much memory is allocated / released
Analyze these during a profiling session
9. What happened to dotTrace Memory?
Did you even know we had a memory profiler before?
dotTrace Memory 3.5
Hasn’t had any significant updates since 2010
Complex to work with
Time for a fresh memory profiler: dotMemory 4!
Friendly – new UI displays complex information in an easy way
Powerful – works on very large programs (we use it on R#)
10. .NET Memory Management 101
Memory Allocation
.NET runtime reserves region of address space for every new process
managed heap
Objects are allocated in the heap
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
See http://confluence.jetbrains.com/display/NETCOM/What+You+Should+Know+about+.NET+Memory+Management
11. .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 that contains all the objects that are reachable from
these roots.
Object unreachable? GC removes the object from the heap, releasing
memory
After the object is removed, GC compacts reachable objects in memory.
Generations
Large Object Heap
See http://confluence.jetbrains.com/display/NETCOM/What+You+Should+Know+about+.NET+Memory+Management
12. .NET Memory Management 101
Memory Allocation
Memory Release or “Garbage Collection” (GC)
Generations
Managed heap divided in segments: generation 0, 1 and 2
New objects go into Gen 0
Gen 0 full? Perform GC and promote all reachable objects to Gen 1. This is typically pretty
fast.
Gen 1 full? Perform GC on Gen 1 and Gen 0. Promote all reachable objects to Gen 2.
Gen 2 full? Perform full GC (2, 1, 0). If not enough memory for new allocations, throws
OutOfMemoryException
Full GC has performance impact since all objects in managed heap are verified.
Large Object Heap
See http://confluence.jetbrains.com/display/NETCOM/What+You+Should+Know+about+.NET+Memory+Management
13. .NET Memory Management 101
Memory Allocation
Memory Release or “Garbage Collection” (GC)
Generations
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)
Large Object Heap
See http://confluence.jetbrains.com/display/NETCOM/What+You+Should+Know+about+.NET+Memory+Management
14. .NET Memory Management 101
Memory Allocation
Memory Release or “Garbage Collection” (GC)
Generations
Large Object Heap
Large objects (>85KB) stored in separate segment of managed heap: Large
Object Heap (LOH)
Objects in LOH collected only during full garbage collection
Survived objects in LOH are not compacted (by default). This means that
LOH becomes fragmented over time.
Fragmentation can cause OutOfMemoryException
See http://confluence.jetbrains.com/display/NETCOM/What+You+Should+Know+about+.NET+Memory+Management
17. Let’s see if we can find what’s wrong!
demo
Investigating a
memory leak
18. What just happened?
public AdWindow(Window owner)
{
// ...
DispatcherTimer[]
// Run the timer that changes the ad's image
adTimer = new DispatcherTimer();
adTimer.Interval = TimeSpan.FromSeconds(3);
adTimer.Tick += ChangeAds;
adTimer.Start();
DispatcherTimer
.Tick
AdWindow
}
adTimer.Tick -= ChangeAds;
19. How is the GC affecting our application?
demo
Analyzing
memory traffic
20. What just happened?
for (int i = 0; i < ....
StringReverse
r
StringReverse
r
StringReverse
r
21. Why use a tool like dotMemory?
Why is this object still in memory?
What takes so much memory?
How much memory traffic is going on?
Are there any memory allocation/distribution patterns violated?