5 misconceptions about .net memory management wednesday june 1 st 2011

41
www.red-gate.com/ memoryprofiler 5 Misconceptions about .NET Memory Management Wednesday June 1 st 2011 Clive Tong and Andrew Hunter

Upload: skah

Post on 23-Feb-2016

31 views

Category:

Documents


2 download

DESCRIPTION

5 Misconceptions about .NET Memory Management Wednesday June 1 st 2011 Clive Tong and A ndrew Hunter. Your presenters. Developer, .NET Reflector. Developer, ANTS Memory Profiler. The webinar. Clive : 5 misconceptions about .NET memory management (30 mins ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

5 Misconceptions about.NET Memory Management

Wednesday June 1st 2011

Clive Tong and Andrew Hunter

Page 2: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Developer, .NET Reflector

Developer, ANTS Memory Profiler

Your presenters

Page 3: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

The webinar• Clive: 5 misconceptions about .NET memory

management (30 mins)

• Andrew: Q&A session for any questions to do with .NET memory

• $50 Amazon voucher for the best answer to Clive’s question.

• Please type questions into your GoToWebinar chat box

Page 4: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Some Misconceptions About Memory Management in .NET

Clive TongRed Gate Software

Page 5: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Five misconceptions

1 A garbage collector collects garbage

2 Doing lots of gen0 collections is bad

3 Performance counters are great for

understanding what is happening

4 .NET doesn’t leak memory

5 All objects are treated the same

Page 6: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

1) The Garbage Collectors Collect Garbage

• The focus is really on the non-garbage• Most (young) objects die young• Designed to collect dead items without processing them

individually

• Note this is for generation 0

Page 7: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

A simple mutator

var collect = new List<B>();while(true){ collect.Add(new A()); new A(); new A();}

Page 8: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Allocation in generation 0Generation 0

After one iteration of the while loop

As we go into the fourth iteration

Page 9: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

No space, so copy

Promote

gen0

gen1

Page 10: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Fix-up

gen0

gen1

Page 11: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

And recycle

gen0

gen1

Page 12: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Observations

• Trick is that objects are allowed to move• Need to get to a safe point when pointers

can be fixed

• Additionally this allows quick allocation

Page 13: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Fast allocation

0:000> u 002c2020 mov eax,dword ptr [ecx+4] mov edx,dword ptr fs:[0E40h] add eax,dword ptr [edx+48h] cmp eax,dword ptr [edx+4Ch] ja 002c203b mov dword ptr [edx+48h],eax sub eax,dword ptr [ecx+4] mov dword ptr [eax],ecx ret002c203b: jmp clr!JIT_NewFast (5cbded01)

Page 14: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Solution

• Avoid locking too frequently by having thread specific allocation buffers

Thread 1 allocation buffer Thread 2 allocation buffer

Page 15: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

And there is one other trick• Use generations to focus attention – 0,1,2• Need to track object references 0:000> u 5cbc2fb0

clr!JIT_WriteBarrierEAX: mov dword ptr [edx],eax cmp eax,271100Ch jb clr!JIT_WriteBarrierEAX+0x17 (5cbc2fc7) shr edx,0Ah nop cmp byte ptr [edx+0BD63E0h],0FFh jne clr!JIT_WriteBarrierEAX+0x1a (5cbc2fca) retclr!JIT_WriteBarrierEAX+0x18: nop nop mov byte ptr [edx+0BD63E0h],0FFh ret

Page 16: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

2) Lots of gen0 collections is bad

• We have covered this already• Time proportional to live data• Though with (fixed) overheads

• Worse case double allocation

Page 17: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

3) Performance counters are accurate

• Quick demonstration

Page 18: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Periodic measurements

• It’s important to remember that these are updated when a collection happens

• No collection means the counter is stuck• The average value can be misleading

Page 19: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

class Program { static void Main(string[] args) {

var accumulator = new List<Program>();

while (true) { DateTime start = DateTime.Now; while ((DateTime.Now - start).TotalSeconds < 15) { accumulator.Add(new Program()); Thread.Sleep(1); }

Console.WriteLine(accumulator.Count); } }}

Page 20: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 21: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 22: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 23: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 24: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 25: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 26: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

4) .NET doesn’t leak

• It’s a question of definition

• Old definition -Forgot to de-allocate after use -Lost the final pointer to some memory -Forgot your responsibilities

Page 27: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

New definition

• Premature release was often fatal• Those days are gone (*)• The runtime is ultra-cautious

• It’s difficult to have an effective cost model

(*) except when you aggressively dispose

Page 28: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

What makes things live longer?

• Runtime• User• Library• Compiler

Page 29: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Runtime

• Type of build• Having a debugger attached • Heuristics choosing when to collect higher

generations• Finalizers

Page 30: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

User

• Event handlers• Assigning temporary values to fields• Value only needed on some code paths

Page 31: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Library

• Caches without a lifetime policy• Data binding

Page 32: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Compiler

• Closures

Page 33: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Closures class Program { private static Func<int> s_LongLived;

static void Main(string[] args) { var x = 20; var y = new int[20200];

Func<int> getSum = () => x + y.Length; Func<int> getFirst = () => x;

s_LongLived = getFirst; } }

Page 34: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Page 35: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

5) All objects are created equal

• Copying takes a while• Need a better way to handle large objects

• Resort to standard mark-and-sweep• No compact at the moment

Page 36: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

No copying during collection

Before

After

Page 37: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Some observations

• No movement, so no fix-ups• Potential parallelism• But potential fragmentation

• Only do when gen2 is collected• Temporary large objects don’t fit model• All blocks are touched

Page 38: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

The problem of fragmentation

Before collection

After collection

Next allocation

Allocate block of this size:

Page 39: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Conclusion

• There’s a lot going on inside your process heap

• The only way to really understand what is going on is to use tools to visualize things

Page 40: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Q&A Session

• Please type your questions into the GoToWebinar box

• We will read out your questions for Andrew to answer

Page 41: 5 Misconceptions about .NET Memory Management Wednesday June 1 st  2011

www.red-gate.com/memoryprofiler

Thank you!

• A recording of this webinar will be emailed to you tomorrow

• Do get in touch!