.NET Memory Primer (Martin Kulov)

  • 44 views
Uploaded on



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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
44
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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
  • 7. Premium community conference on Microsoft technologies itcampro@ itcamp14# x64 Memory Mapping
  • 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
  • 10. Premium community conference on Microsoft technologies itcampro@ itcamp14# Virtual Address Space
  • 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
  • 13. Premium community conference on Microsoft technologies itcampro@ itcamp14# Stack Layout
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. Premium community conference on Microsoft technologies itcampro@ itcamp14# DEMO: ALLOCATING MEMORY
  • 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
  • 26. Premium community conference on Microsoft technologies itcampro@ itcamp14# Workstation GC
  • 27. Premium community conference on Microsoft technologies itcampro@ itcamp14# Server GC
  • 28. Premium community conference on Microsoft technologies itcampro@ itcamp14# Concurrent GC – Before and After
  • 29. Premium community conference on Microsoft technologies itcampro@ itcamp14# Testing Concurrent Server GC
  • 30. Premium community conference on Microsoft technologies itcampro@ itcamp14# Q & A