Presentation is loading. Please wait.

Presentation is loading. Please wait.

.NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group.

Similar presentations


Presentation on theme: ".NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group."— Presentation transcript:

1 .NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group

2 Why This Talk? The.NET GC is a complex beast Many performance optimizations over the years Still many ways to shoot yourself in the foot Mark and SweepGenerationsLOH Fragmentation HoardingSegments FinalizationBackground GC VMMapPerfView Server GC www.devreach.com

3 Refresher: Tracing GC.NET GC is not based on reference counting GC starts from roots and marks reachable objects as “live” The rest of the heap is swept as garbage Usually holes are compacted and objects are moved around Allocations are very cheap www.devreach.com

4 Problems? Naïve implementation blocks mutators Full GC is very slow (think multi-GB heaps) Result: very long application pauses www.devreach.com

5 GC Flavors CLR 2.0 Workstation GC Concurrent, non- concurrent Server GC Parallel GC CLR 4.0 Workstation background GC CLR 4.5 Server concurrent and background GC www.devreach.com DEMO

6 Generations Full GC is slow and very inefficient – most objects are live New objects die quickly, old objects die hard Divide the heap into regions, enabling partial collections – Tricky to deal with cross-generation references Special area for large objects (>85K) www.devreach.com

7 Measurement Profile apps for allocations, not just time Look for large allocation sources Look for objects with a long lifetime Pool temporary large objects (cf. WCF BufferManager)BufferManager www.devreach.com DEMOS

8 GC and Virtual Memory Windows VirtualAlloc has 64KB granularity in user-mode CLR allocates consecutive 16MB-64MB segments (x86) of memory On x86, easy to fragment the address space VMMap to detect, x64/VM hoarding to fix www.devreach.com DEMO

9 Finalization Finalizer (cleanup) runs automatically after object becomes unreachable Performance problems and bugs: – Allocation/finalization rate – Race conditions, deadlocks – The finalizer may run while a method is still executing on an instance www.devreach.com DEMO

10 Conclusions Minimize the number and size of allocations Strive to quickly kill temporary objects Pool temporary large objects Don’t use finalization. Just don’t use it We have only scratched the surface here! Pro.NET Performance: http://s.sashag.net/pronetperformancehttp://s.sashag.net/pronetperformance Maoni Stephens MSDN blog: http://blogs.msdn.com/b/maoni/http://blogs.msdn.com/b/maoni/ Advanced.NET Debugging (Hewardt): http://advanceddotnetdebugging.com/http://advanceddotnetdebugging.com/ www.devreach.com

11 Thank You! @goldshtn blog.sashag.net www.linkedin.com/in/goldshtn Sasha Goldshtein | SELA Group Two free Pro.NET Performance eBooks! Send an email to sashag@sela.co.il with an answer to the following question: What do you need to do for your console application to use background server GC in.NET 4.5?


Download ppt ".NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group."

Similar presentations


Ads by Google