net memory primer (martin kulov)

30
Premium community conference on Microsoft technologies itcampro @ itcamp14 # .NET Memory Primer Martin Kulov [email protected]

Upload: itcamp

Post on 13-May-2015

80 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

.NET Memory Primer

Martin Kulov

[email protected]

Page 2: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Huge thanks to our sponsors & partners!

Page 3: NET Memory Primer (Martin Kulov)

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

Page 4: NET Memory Primer (Martin Kulov)

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

Page 5: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

x86 Memory Mapping

* PFN - Page Frame Number database

Page 6: NET Memory Primer (Martin Kulov)

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

Page 7: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

x64 Memory Mapping

Page 8: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Max 4TB - Windows Srv 2012 Standard

• Limited per SKU

x64 Physical Memory Limits

Page 9: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Canonical Form Addresses

48-bit implementation 56-bit implementation 64-bit implementation

Page 10: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Virtual Address Space

Page 11: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Code

• Data

• Stacks

• Heaps

User Mode Memory

Page 12: NET Memory Primer (Martin Kulov)

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

Page 13: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Stack Layout

Page 14: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• ChildEBP RetAddr Caller,Callee

• 08e4e1a4 751b149d _WaitForSingleObjectEx@12+0x98, calling _ZwWaitForSingleObject@12

• 08e4e1e8 718b53c2 ?LeaveRuntimeNoThrow@Thread@@SGJI@Z+0xd7, calling __EH_epilog3

• 08e4e210 755b1194 _WaitForSingleObjectExImplementation@12+0x75, calling _WaitForSingleObjectEx@12

• 08e4e228 718b54d7 ?LoadImage@PEImage@@SGPAV1@PAUHINSTANCE__@@@Z+0x1af

• ...

• 08e4f5f4 71a10647 ?intermediateThreadProc@Thread@@CGKPAX@Z+0x49

• 08e4f784 71a10635 ?intermediateThreadProc@Thread@@CGKPAX@Z+0x37, calling __alloca_probe_16

• 08e4f798 755b336a @BaseThreadInitThunk@12+0xe

• 08e4f7a4 77639f72 ___RtlUserThreadStart@8+0x70

• 08e4f7e4 77639f45 __RtlUserThreadStart@8+0x1b, calling ___RtlUserThreadStart@8

Call Stack Example

Page 15: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Hold Dynamically Allocated Data

• 1 Heap Per Logical Processor

• Heap Has Segments

–Process Heap

–Code Heap /JITed code/

– Small Object Heap /SOH/

– Large Object Heap /LOH/

Heaps

Page 16: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Stack

–Value Types /Int32, Bool, Struct, etc…/

–Pointers to Reference Types

• Heap

–Reference Types /Object, String, Array, etc…/

– Free Areas

Allocating .NET Memory

Page 17: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

DEMO: ALLOCATING MEMORY

Page 18: NET Memory Primer (Martin Kulov)

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/

Page 19: NET Memory Primer (Martin Kulov)

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

Page 20: NET Memory Primer (Martin Kulov)

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

Page 21: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Gen0 is Full

• Induced GC /System.GC.Collect()/

• System Pressure

Collection - When

Page 22: NET Memory Primer (Martin Kulov)

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

Page 23: NET Memory Primer (Martin Kulov)

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

Page 24: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

• Suspend Managed Threads

• Collect Garbage

• Resume Managed Threads

• Two Phases of GC

–Mark

–Compact

Collection - How

Page 25: NET Memory Primer (Martin Kulov)

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

Page 26: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Workstation GC

Page 27: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Server GC

Page 28: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Concurrent GC – Before and After

Page 29: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Testing Concurrent Server GC

Page 30: NET Memory Primer (Martin Kulov)

Premium community conference on Microsoft technologies itcampro@ itcamp14#

Q & A