This presentation: Sasha GoldshteinCTO, Sela Group [email protected] Garbage Collection...

18
This presentation: http://s.sashag.net/gctipsLT Sasha Goldshtein CTO, Sela Group blog.sashag.net @goldshtn .NET Garbage Collection Performance Tips

Transcript of This presentation: Sasha GoldshteinCTO, Sela Group [email protected] Garbage Collection...

Page 1: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Sasha Goldshtein CTO, Sela Groupblog.sashag.net @goldshtn

.NET Garbage Collection Performance Tips

Page 2: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Why This Talk?The .NET GC is a complex beastMany performance optimizations over the yearsStill many ways to shoot yourself in the foot

Mark and Sweep Generations LOHFragmentation Hoarding

Segments Finalization Background GC

VMMap PerfView Server GC

Page 3: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Refresher: Tracing GCGC starts from roots and marks reachable objects as “live”The rest of the heap is swept as garbageUsually holes are compacted and objects are moved aroundAllocations are very cheap

Page 4: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Problems?Naïve implementation blocks mutatorsResult: very long application pauses

Page 5: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

.NET GC Evolution

CLR 2.0

Workstation GC• Concurrent,

non-concurrent

Server GC• Parallel GC

CLR 2.0 SP1

GC latency APIGC notifications API

CLR 4.0

Background workstation GC

CLR 4.5

Concurrent and background server GC

CLR 4.5.1

LOH compaction API

Page 6: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Demo

GC Flavors

Page 7: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

GenerationsFull GC is slow and very inefficientNew objects die quickly, old objects die hardDivide the heap into regions, enabling partial collectionsSpecial area for large objects

Page 8: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Measurement and Improvement

Profile apps for allocations, not just timeLook for large allocations, long-lived objectsPool temporary large objectsUse value types when possible

Page 9: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Demo

Reducing GC Pressure

Page 10: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

GC and Virtual MemoryVirtualAlloc has 64KB granularity in user-mode

CLR allocates consecutive 16MB-64MB segments (x86) of memoryOn x86, easy to fragment the address space

Page 11: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Demo

VM Fragmentation

Page 12: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

FinalizationFinalizer (cleanup) runs automatically after object becomes unreachable

Page 13: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Finalization IssuesAllocating faster than can possibly finalizeRace conditions, deadlocksThe finalizer may run while a method is still executing on an instance

Page 14: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Demo

Finalization

Page 15: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

tips

Conclusions

• Measure early• Measure often• Measure again

Minimize the number and size of allocationsStrive to quickly kill temporary objectsPool temporary large objectsRely on Dispose() rather than finalization

Page 16: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Additional References

Page 17: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Questions

Page 18: This presentation:  Sasha GoldshteinCTO, Sela Group blog.sashag.net@goldshtn.NET Garbage Collection Performance Tips.

This presentation: http://s.sashag.net/gctipsLT

Sasha Goldshtein This presentation:blog.sashag.net s.sashag.net/gctipsLT@goldshtn

Thank You!