net garbage collection performance tips
Post on 01-Dec-2014
5.544 Views
Preview:
DESCRIPTION
TRANSCRIPT
Sasha Goldshtein
CTOSela Group
@goldshtnblog.sashag.net
Garbage Collection Performance Tips
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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 Sweep Generations LOH
Fragmentation Hoarding Segments
Finalization Background GC
VMMap PerfView Server GC
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
4
PROBLEMS?
Naïve implementation blocks mutators Result: very long application pauses
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
5
GC FLAVORS
CLR 2.0
Workstation GC• Concurrent, non-
concurrent
Server GC• Parallel GC
CLR 4.0
Background workstation GC
CLR 4.5
Concurrent and background server GC
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
7
MEASUREMENT
Profile apps for allocations, not just time Look for large allocations, long-lived objs
Pool temporary large objects
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
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
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
ADDITIONAL REFERENCES
www.devconnections.com
GARBAGE COLLECTION PERFORMANCE TIPS
12
THANK YOU!
Sasha Goldshtein@goldshtn
sashag@sela.co.ilblog.sashag.net
top related