SlideShare a Scribd company logo
1 of 49
Download to read offline
LOOK MOMMY, NO GC!
DINA GOLDSHTEIN,
@DINAGOZIL
SDP MAY 2017 #SELASDP | @DINAGOZIL
AGENDA
▸ Motivation
▸ Identifying memory traffic problems
▸ Specific code patterns
▸ General tips and tricks
2
SDP MAY 2017 #SELASDP | @DINAGOZIL
IT’S NOT THAT SIMPLE
▸ Memory allocations are cheap
▸ But not free
▸ And have side effects
▸ Don’t forget to think about the memory
3
SDP MAY 2017 #SELASDP | @DINAGOZIL
THIS ISN’T JUST THEORY
4
SDP MAY 2017 #SELASDP | @DINAGOZIL
THIS ISN’T JUST THEORY
5
IDENTIFICATION
STEP I
SDP MAY 2017 #SELASDP | @DINAGOZIL
PERFORMANCE COUNTERS
▸ Provide numeric performance information about the system
▸ Located in different areas in the system - disk, .NET, networking, OS objects
▸ Available on-demand using built-in tools like perfmon and typeperf
▸ Can write your own but that’s not the topic of our talk…
7
SDP MAY 2017 #SELASDP | @DINAGOZIL
PERFMON
8
SDP MAY 2017 #SELASDP | @DINAGOZIL
DIVE DEEPER WITH EVENT TRACING FOR WINDOWS
▸ High-speed logging framework supporting more than 100K structured
messages per second
▸ .NET, drivers, services, third party components
▸ Can be turned on on-demand while running
▸ Very small overhead
▸ Can write your own but that’s not the topic of our talk…
9
PERFVIEW
DEMO
SDP MAY 2017 #SELASDP | @DINAGOZIL
STATIC CODE ANALYSIS
▸ Can detect code issues while coding without running the application
▸ Usually based on pattern-detection
▸ Undisposed IDisposable-s
▸ Inefficient String concatenations
▸ Wrong number of “dynamic” parameters
▸ ReSharper, Visual Studio, and more
11
SDP MAY 2017 #SELASDP | @DINAGOZIL
TIPS IN VS
12
SDP MAY 2017 #SELASDP | @DINAGOZIL
DYNAMIC MEMORY PROFILING
▸ Dynamic analysis can help uncover more subtle scenarios
▸ Can collect temporal information
▸ Managed and unmanaged memory leaks
▸ Lots of commercial tools available: .NET Memory Profiler, ANTS, dotMemory…
13
SDP MAY 2017 #SELASDP | @DINAGOZIL
.NET MEMORY PROFILER AND MEMORY LEAKS
14
SDP MAY 2017 #SELASDP | @DINAGOZIL
WHO’S HOLDING MY OBJECTS?
15
AVOIDANCE
STEP II
SDP MAY 2017 #SELASDP | @DINAGOZIL
GARBAGE, GARBAGE EVERYWHERE
17
SDP MAY 2017 #SELASDP | @DINAGOZIL
STRUCTS, COMPARISONS AND WHAT’S BETWEEN THEM
▸ We have a data structure containing a single int value
▸ How long does it take to search an element in a list of such data structures?
▸ Different implementations
▸ Default comparison
▸ Override Equals
▸ Implement IEquatable
18
SEARCHING
DEMO
SDP MAY 2017 #SELASDP | @DINAGOZIL
RESULTS
20
SDP MAY 2017 #SELASDP | @DINAGOZIL
HOW DOES SEARCHING WORK?
21
SDP MAY 2017 #SELASDP | @DINAGOZIL
HOW DOES COMPARING WORK?
22
SDP MAY 2017 #SELASDP | @DINAGOZIL
WHAT’S THE DIFFERENCE?
23
…
SDP MAY 2017 #SELASDP | @DINAGOZIL
OBJECT EQUALITY COMPARER
24
SDP MAY 2017 #SELASDP | @DINAGOZIL
GENERIC EQUALITY COMPARER
25
SDP MAY 2017 #SELASDP | @DINAGOZIL
WHAT’S TAKING ALL THE TIME?
26
SDP MAY 2017 #SELASDP | @DINAGOZIL
REFLECTION IS EXPENSIVE
27
SDP MAY 2017 #SELASDP | @DINAGOZIL
LAMBDAS AND CLOSURES
28
CAPTURING
DEMO
SDP MAY 2017 #SELASDP | @DINAGOZIL
THE RESULTS
30
SDP MAY 2017 #SELASDP | @DINAGOZIL
IT’S ALL ABOUT THE ALLOCATIONS
31
SDP MAY 2017 #SELASDP | @DINAGOZIL
UNDER THE HOOD - AN ALLOCATION FOR EACH CAPTURE
32
SDP MAY 2017 #SELASDP | @DINAGOZIL
UNDER THE HOOD - NO CAPTURING; HENCE, NO ALLOCATION!
33
SDP MAY 2017 #SELASDP | @DINAGOZIL
UNDER THE HOOD - NO DATA; HENCE, NO ALLOCATION!
34
SDP MAY 2017 #SELASDP | @DINAGOZIL
UNDER THE HOOD - BUG 🐞
35
SDP MAY 2017 #SELASDP | @DINAGOZIL
THE RESULTS
36
LINQ
DEMO
SDP MAY 2017 #SELASDP | @DINAGOZIL
RESULTS
38
DILIGENCE
STEP III
SDP MAY 2017 #SELASDP | @DINAGOZIL
DON’T CALL GC.COLLECT()
▸ You have a large heap. Don’t make it worse by frequent calls to full GC which is
going to take a long time.
▸ Instead, use what we learned to reduce memory usage
▸ And don’t forget to remove debugging code from production
40
SDP MAY 2017 #SELASDP | @DINAGOZIL
THIS IS NOT A DRILL
41
SDP MAY 2017 #SELASDP | @DINAGOZIL
AN (UN)FORTUNATE COINCIDENCE
42
SDP MAY 2017 #SELASDP | @DINAGOZIL
PREFER VALUE TYPES
▸ Value types (structs) have a compact memory layout, and can be embedded in
their parent object, making cache’s life easier and generally reducing footprint
43
SDP MAY 2017 #SELASDP | @DINAGOZIL
BASED ON A REAL STORY
44
SDP MAY 2017 #SELASDP | @DINAGOZIL
POOL, BUFFER AND REUSE EXPENSIVE OBJECTS
▸ Pool expensive or large objects
instead of returning them to GC
▸ Many OSS pools out there
▸ For large primitive buffer (e.g.
byte[]) may use slices
▸ A large preallocated buffer which
is sliced into small parts
▸ System.Slices on GitHub
45
SDP MAY 2017 #SELASDP | @DINAGOZIL
HARD TO WATCH
46
SDP MAY 2017 #SELASDP | @DINAGOZIL
AVOID FINALIZATION
▸ Finalizers run at some point after the object is no longer referenced by the
application (non-deterministic)
▸ Finalizers run on a separate thread and create potential concurrency issues
▸ Finalization prolongs object lifetime and can create leaks if finalizers don’t
complete quickly enough
▸ Better to use deterministic resource management (IDisposable)
47
SDP MAY 2017 #SELASDP | @DINAGOZIL
SUMMARY
▸ Although a single memory allocation is extremely fast, it adds up
▸ All based on real questions, stories and bugs
▸ Don’t overcomplicate where it’s not needed
▸ Measure
▸ And optimize…
▸ DIY: https://github.com/dinazil/look-mommy-no-gc
48
THANK YOU
DINA GOLDSHTEIN,
@DINAGOZIL

More Related Content

Similar to Look Mommy, no GC! (SDP May 2017)

Bndtools and Maven: A Brave New World - N Bartlett & T Ward
Bndtools and Maven: A Brave New World - N Bartlett & T WardBndtools and Maven: A Brave New World - N Bartlett & T Ward
Bndtools and Maven: A Brave New World - N Bartlett & T Ward
mfrancis
 
uStorage - A storage architecture to provide Block-level Storage through Obje...
uStorage - A storage architecture to provide Block-level Storage through Obje...uStorage - A storage architecture to provide Block-level Storage through Obje...
uStorage - A storage architecture to provide Block-level Storage through Obje...
Felipe Gutierrez
 

Similar to Look Mommy, no GC! (SDP May 2017) (16)

Running efficient distributed teams
Running efficient distributed teamsRunning efficient distributed teams
Running efficient distributed teams
 
How to Write a Kick-Ass Blog Outline
How to Write a Kick-Ass Blog OutlineHow to Write a Kick-Ass Blog Outline
How to Write a Kick-Ass Blog Outline
 
UX Camp Europe 17 – Teamwork
UX Camp Europe 17 – TeamworkUX Camp Europe 17 – Teamwork
UX Camp Europe 17 – Teamwork
 
The Developer vs. The Relational Database: How to do it right
The Developer vs. The Relational Database: How to do it rightThe Developer vs. The Relational Database: How to do it right
The Developer vs. The Relational Database: How to do it right
 
Deep Learning Design Patterns Study Jams - Overview
Deep Learning Design Patterns Study Jams - OverviewDeep Learning Design Patterns Study Jams - Overview
Deep Learning Design Patterns Study Jams - Overview
 
Project Management Careers in Data Science
Project Management Careers in Data ScienceProject Management Careers in Data Science
Project Management Careers in Data Science
 
Technical presentation of IGDB.com
Technical presentation of IGDB.comTechnical presentation of IGDB.com
Technical presentation of IGDB.com
 
Steam Learn: Introduction to NoSQL with MongoDB
Steam Learn: Introduction to NoSQL with MongoDBSteam Learn: Introduction to NoSQL with MongoDB
Steam Learn: Introduction to NoSQL with MongoDB
 
Bndtools and Maven: A Brave New World - N Bartlett & T Ward
Bndtools and Maven: A Brave New World - N Bartlett & T WardBndtools and Maven: A Brave New World - N Bartlett & T Ward
Bndtools and Maven: A Brave New World - N Bartlett & T Ward
 
Ahead Summit - From Strategy to User Experience - Meeting Design is Everythin...
Ahead Summit - From Strategy to User Experience - Meeting Design is Everythin...Ahead Summit - From Strategy to User Experience - Meeting Design is Everythin...
Ahead Summit - From Strategy to User Experience - Meeting Design is Everythin...
 
Practical Software Architecture DDD
Practical Software Architecture DDDPractical Software Architecture DDD
Practical Software Architecture DDD
 
Delicious : EDQ, OGG and ODI over Exadata for Perfection
Delicious : EDQ, OGG and ODI over Exadata for PerfectionDelicious : EDQ, OGG and ODI over Exadata for Perfection
Delicious : EDQ, OGG and ODI over Exadata for Perfection
 
uStorage - A storage architecture to provide Block-level Storage through Obje...
uStorage - A storage architecture to provide Block-level Storage through Obje...uStorage - A storage architecture to provide Block-level Storage through Obje...
uStorage - A storage architecture to provide Block-level Storage through Obje...
 
SPUnite17 Transforming your Organisation into a Digital Workplace
SPUnite17 Transforming your Organisation into a Digital WorkplaceSPUnite17 Transforming your Organisation into a Digital Workplace
SPUnite17 Transforming your Organisation into a Digital Workplace
 
Mastering Data Modeling for NoSQL Platforms
Mastering Data Modeling for NoSQL PlatformsMastering Data Modeling for NoSQL Platforms
Mastering Data Modeling for NoSQL Platforms
 
More Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer ExperienceMore Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer Experience
 

More from Dina Goldshtein

More from Dina Goldshtein (13)

How Does the Internet Work? (Wix she codes; branch)
How Does the Internet Work? (Wix she codes; branch)How Does the Internet Work? (Wix she codes; branch)
How Does the Internet Work? (Wix she codes; branch)
 
Self-Aware Applications: Automatic Production Monitoring (SDP November 2017)
Self-Aware Applications: Automatic Production Monitoring (SDP November 2017)Self-Aware Applications: Automatic Production Monitoring (SDP November 2017)
Self-Aware Applications: Automatic Production Monitoring (SDP November 2017)
 
Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)Look Mommy, No GC! (TechDays NL 2017)
Look Mommy, No GC! (TechDays NL 2017)
 
Self-Aware Applications: Automatic Production Monitoring (TechDays NL 2017)
Self-Aware Applications: Automatic Production Monitoring (TechDays NL 2017)Self-Aware Applications: Automatic Production Monitoring (TechDays NL 2017)
Self-Aware Applications: Automatic Production Monitoring (TechDays NL 2017)
 
ETW - Monitor Anything, Anytime, Anywhere (Velocity NYC 2017)
ETW - Monitor Anything, Anytime, Anywhere (Velocity NYC 2017)ETW - Monitor Anything, Anytime, Anywhere (Velocity NYC 2017)
ETW - Monitor Anything, Anytime, Anywhere (Velocity NYC 2017)
 
Self-Aware Applications: Automatic Production Monitoring (NDC Sydney 2017)
Self-Aware Applications: Automatic Production Monitoring (NDC Sydney 2017)Self-Aware Applications: Automatic Production Monitoring (NDC Sydney 2017)
Self-Aware Applications: Automatic Production Monitoring (NDC Sydney 2017)
 
ETW - Monitor Anything, Anytime, Anywhere (NDC Oslo 2017)
ETW - Monitor Anything, Anytime, Anywhere (NDC Oslo 2017)ETW - Monitor Anything, Anytime, Anywhere (NDC Oslo 2017)
ETW - Monitor Anything, Anytime, Anywhere (NDC Oslo 2017)
 
How does the Internet Work?
How does the Internet Work?How does the Internet Work?
How does the Internet Work?
 
How does the Internet Work?
How does the Internet Work?How does the Internet Work?
How does the Internet Work?
 
Things They Don’t Teach You @ School
Things They Don’t Teach You @ SchoolThings They Don’t Teach You @ School
Things They Don’t Teach You @ School
 
What's New in C++ 11/14?
What's New in C++ 11/14?What's New in C++ 11/14?
What's New in C++ 11/14?
 
HTML5 Canvas
HTML5 CanvasHTML5 Canvas
HTML5 Canvas
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Look Mommy, no GC! (SDP May 2017)

  • 1. LOOK MOMMY, NO GC! DINA GOLDSHTEIN, @DINAGOZIL
  • 2. SDP MAY 2017 #SELASDP | @DINAGOZIL AGENDA ▸ Motivation ▸ Identifying memory traffic problems ▸ Specific code patterns ▸ General tips and tricks 2
  • 3. SDP MAY 2017 #SELASDP | @DINAGOZIL IT’S NOT THAT SIMPLE ▸ Memory allocations are cheap ▸ But not free ▸ And have side effects ▸ Don’t forget to think about the memory 3
  • 4. SDP MAY 2017 #SELASDP | @DINAGOZIL THIS ISN’T JUST THEORY 4
  • 5. SDP MAY 2017 #SELASDP | @DINAGOZIL THIS ISN’T JUST THEORY 5
  • 7. SDP MAY 2017 #SELASDP | @DINAGOZIL PERFORMANCE COUNTERS ▸ Provide numeric performance information about the system ▸ Located in different areas in the system - disk, .NET, networking, OS objects ▸ Available on-demand using built-in tools like perfmon and typeperf ▸ Can write your own but that’s not the topic of our talk… 7
  • 8. SDP MAY 2017 #SELASDP | @DINAGOZIL PERFMON 8
  • 9. SDP MAY 2017 #SELASDP | @DINAGOZIL DIVE DEEPER WITH EVENT TRACING FOR WINDOWS ▸ High-speed logging framework supporting more than 100K structured messages per second ▸ .NET, drivers, services, third party components ▸ Can be turned on on-demand while running ▸ Very small overhead ▸ Can write your own but that’s not the topic of our talk… 9
  • 11. SDP MAY 2017 #SELASDP | @DINAGOZIL STATIC CODE ANALYSIS ▸ Can detect code issues while coding without running the application ▸ Usually based on pattern-detection ▸ Undisposed IDisposable-s ▸ Inefficient String concatenations ▸ Wrong number of “dynamic” parameters ▸ ReSharper, Visual Studio, and more 11
  • 12. SDP MAY 2017 #SELASDP | @DINAGOZIL TIPS IN VS 12
  • 13. SDP MAY 2017 #SELASDP | @DINAGOZIL DYNAMIC MEMORY PROFILING ▸ Dynamic analysis can help uncover more subtle scenarios ▸ Can collect temporal information ▸ Managed and unmanaged memory leaks ▸ Lots of commercial tools available: .NET Memory Profiler, ANTS, dotMemory… 13
  • 14. SDP MAY 2017 #SELASDP | @DINAGOZIL .NET MEMORY PROFILER AND MEMORY LEAKS 14
  • 15. SDP MAY 2017 #SELASDP | @DINAGOZIL WHO’S HOLDING MY OBJECTS? 15
  • 17. SDP MAY 2017 #SELASDP | @DINAGOZIL GARBAGE, GARBAGE EVERYWHERE 17
  • 18. SDP MAY 2017 #SELASDP | @DINAGOZIL STRUCTS, COMPARISONS AND WHAT’S BETWEEN THEM ▸ We have a data structure containing a single int value ▸ How long does it take to search an element in a list of such data structures? ▸ Different implementations ▸ Default comparison ▸ Override Equals ▸ Implement IEquatable 18
  • 20. SDP MAY 2017 #SELASDP | @DINAGOZIL RESULTS 20
  • 21. SDP MAY 2017 #SELASDP | @DINAGOZIL HOW DOES SEARCHING WORK? 21
  • 22. SDP MAY 2017 #SELASDP | @DINAGOZIL HOW DOES COMPARING WORK? 22
  • 23. SDP MAY 2017 #SELASDP | @DINAGOZIL WHAT’S THE DIFFERENCE? 23 …
  • 24. SDP MAY 2017 #SELASDP | @DINAGOZIL OBJECT EQUALITY COMPARER 24
  • 25. SDP MAY 2017 #SELASDP | @DINAGOZIL GENERIC EQUALITY COMPARER 25
  • 26. SDP MAY 2017 #SELASDP | @DINAGOZIL WHAT’S TAKING ALL THE TIME? 26
  • 27. SDP MAY 2017 #SELASDP | @DINAGOZIL REFLECTION IS EXPENSIVE 27
  • 28. SDP MAY 2017 #SELASDP | @DINAGOZIL LAMBDAS AND CLOSURES 28
  • 30. SDP MAY 2017 #SELASDP | @DINAGOZIL THE RESULTS 30
  • 31. SDP MAY 2017 #SELASDP | @DINAGOZIL IT’S ALL ABOUT THE ALLOCATIONS 31
  • 32. SDP MAY 2017 #SELASDP | @DINAGOZIL UNDER THE HOOD - AN ALLOCATION FOR EACH CAPTURE 32
  • 33. SDP MAY 2017 #SELASDP | @DINAGOZIL UNDER THE HOOD - NO CAPTURING; HENCE, NO ALLOCATION! 33
  • 34. SDP MAY 2017 #SELASDP | @DINAGOZIL UNDER THE HOOD - NO DATA; HENCE, NO ALLOCATION! 34
  • 35. SDP MAY 2017 #SELASDP | @DINAGOZIL UNDER THE HOOD - BUG 🐞 35
  • 36. SDP MAY 2017 #SELASDP | @DINAGOZIL THE RESULTS 36
  • 38. SDP MAY 2017 #SELASDP | @DINAGOZIL RESULTS 38
  • 40. SDP MAY 2017 #SELASDP | @DINAGOZIL DON’T CALL GC.COLLECT() ▸ You have a large heap. Don’t make it worse by frequent calls to full GC which is going to take a long time. ▸ Instead, use what we learned to reduce memory usage ▸ And don’t forget to remove debugging code from production 40
  • 41. SDP MAY 2017 #SELASDP | @DINAGOZIL THIS IS NOT A DRILL 41
  • 42. SDP MAY 2017 #SELASDP | @DINAGOZIL AN (UN)FORTUNATE COINCIDENCE 42
  • 43. SDP MAY 2017 #SELASDP | @DINAGOZIL PREFER VALUE TYPES ▸ Value types (structs) have a compact memory layout, and can be embedded in their parent object, making cache’s life easier and generally reducing footprint 43
  • 44. SDP MAY 2017 #SELASDP | @DINAGOZIL BASED ON A REAL STORY 44
  • 45. SDP MAY 2017 #SELASDP | @DINAGOZIL POOL, BUFFER AND REUSE EXPENSIVE OBJECTS ▸ Pool expensive or large objects instead of returning them to GC ▸ Many OSS pools out there ▸ For large primitive buffer (e.g. byte[]) may use slices ▸ A large preallocated buffer which is sliced into small parts ▸ System.Slices on GitHub 45
  • 46. SDP MAY 2017 #SELASDP | @DINAGOZIL HARD TO WATCH 46
  • 47. SDP MAY 2017 #SELASDP | @DINAGOZIL AVOID FINALIZATION ▸ Finalizers run at some point after the object is no longer referenced by the application (non-deterministic) ▸ Finalizers run on a separate thread and create potential concurrency issues ▸ Finalization prolongs object lifetime and can create leaks if finalizers don’t complete quickly enough ▸ Better to use deterministic resource management (IDisposable) 47
  • 48. SDP MAY 2017 #SELASDP | @DINAGOZIL SUMMARY ▸ Although a single memory allocation is extremely fast, it adds up ▸ All based on real questions, stories and bugs ▸ Don’t overcomplicate where it’s not needed ▸ Measure ▸ And optimize… ▸ DIY: https://github.com/dinazil/look-mommy-no-gc 48