net garbage collection performance tips

12
Sasha Goldshtein CTO Sela Group @goldshtn blog.sashag.net Garbage Collection Performance Tips

Upload: sasha-goldshtein

Post on 01-Dec-2014

5.550 views

Category:

Technology


0 download

DESCRIPTION

In this presentation we review the basics of how the .NET garbage collector operates, and then tackle some best practices that can make the performance of your CLR-based applications improve considerably. For example, we look at garbage collection flavors (server GC, background GC, and others), large object allocations, and finalization. We also review ways to track object allocations and even resolve memory leaks using the PerfView tool.

TRANSCRIPT

Page 1: NET Garbage Collection Performance Tips

Sasha Goldshtein

CTOSela Group

@goldshtnblog.sashag.net

Garbage Collection Performance Tips

Page 2: 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

Page 3: NET Garbage Collection Performance Tips

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

Page 4: NET Garbage Collection Performance Tips

www.devconnections.com

GARBAGE COLLECTION PERFORMANCE TIPS

4

PROBLEMS?

Naïve implementation blocks mutators Result: very long application pauses

Page 5: NET Garbage Collection Performance Tips

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

Page 6: NET Garbage Collection Performance Tips

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)

Page 7: NET Garbage Collection Performance Tips

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

Page 8: NET Garbage Collection Performance Tips

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

Page 9: NET Garbage Collection Performance Tips

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

Page 10: NET Garbage Collection Performance Tips

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

Page 11: NET Garbage Collection Performance Tips

www.devconnections.com

GARBAGE COLLECTION PERFORMANCE TIPS

ADDITIONAL REFERENCES

Page 12: NET Garbage Collection Performance Tips

www.devconnections.com

GARBAGE COLLECTION PERFORMANCE TIPS

12

THANK YOU!

Sasha Goldshtein@goldshtn

[email protected]