In this session we will look over the various ways .NET is collecting memory, tips how to help GC perform better and tools that will save your day.
This is a must attend session for those who still do not know how to troubleshoot memory issues. For the rest it is a nice refresh and new look of features in .NET 4.5. As usual there will be lots of demos.
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
.NET Memory Primer (Martin Kulov)
1. Premium community conference on Microsoft technologies itcampro@ itcamp14#
.NET Memory Primer
Martin Kulov
martin@kulov.net
2. Premium community conference on Microsoft technologies itcampro@ itcamp14#
Huge thanks to our sponsors & partners!
3. Premium community conference on Microsoft technologies itcampro@ itcamp14#
"Out of CPU, memory and disk, memory is typically
the most important for overall system performance."
Mark Russinovich
“All you worry about in a .NET application is the
memory.”
John Robbins
4. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• x86
–2 ^ 32 bits = 4GB /0x FFFF FFFF/
• x64
–2 ^ 64 bits = 16 EB /0x FFFF FFFF' FFFF FFFF/
Virtual Memory Limits
5. Premium community conference on Microsoft technologies itcampro@ itcamp14#
x86 Memory Mapping
* PFN - Page Frame Number database
6. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Max 4GB
–Windows Client, Windows Srv 2008 Standard
• Max 128GB
–Windows Srv 2003 SP1 Datacenter (PAE)
x86 Physical Memory Limits
8. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Max 4TB - Windows Srv 2012 Standard
• Limited per SKU
x64 Physical Memory Limits
9. Premium community conference on Microsoft technologies itcampro@ itcamp14#
Canonical Form Addresses
48-bit implementation 56-bit implementation 64-bit implementation
11. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Code
• Data
• Stacks
• Heaps
User Mode Memory
12. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Created for Each Thread
• Default to 1MB
• Hold Method Data /stack frame/
–Parameters
–Local variables
–Return address
• First In, First Out
Stacks
18. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Stack References
• Static References /Fields, ThreadStatic/
• CPU Registers
• Interop References /COM, API calls/
• Finalization Queue References
Object Roots /GC Roots/
19. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• a.k.a. Generational Garbage Collector /GC/
• Three Generations /SOH/
–Gen0 – short lived
–Gen1 – medium lived
–Gen2 – long lived
Nondeterministic Finalization
20. Premium community conference on Microsoft technologies itcampro@ itcamp14#
Before GC #1
Gen1 Gen0
Before GC #500
Gen2
Gen2
Gen2 Gen1 Gen0
Gen0
Before GC #0
Before GC #2
Gen2 Gen1 Gen0
Before GC #100
Gen2
Gen2 Gen1 Gen0
21. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Gen0 is Full
• Induced GC /System.GC.Collect()/
• System Pressure
Collection - When
22. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Rule of Thumb – Ratio 1:10:100
• .NET CLR Memory% time in GC
• .NET CLR Memory# Induced GC
• .NET CLR Memory# Gen X collections
Collection - Cost
23. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Size > 85KB
• Memory is marked as free during Gen2
• Avoid Temporary Large Objects
• Reuse Objects in LOH If Possible
• Many LOH Segments
• Fragmentation Problems
Large Object Heap
24. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Suspend Managed Threads
• Collect Garbage
• Resume Managed Threads
• Two Phases of GC
–Mark
–Compact
Collection - How
25. Premium community conference on Microsoft technologies itcampro@ itcamp14#
• Workstation GC – Non Concurrent
• Server GC – Non Concurrent
• Workstation GC – Concurrent
–Background GC /New in .NET 4/
• Server GC – Background /New in .NET 4.5/
GC Types