SlideShare a Scribd company logo
1 of 19
Ronn Black							October 2010 Analyzing Memory Usage and Leaks
NextObjPtr NextObjPtr
NextPtr NextPtr Gen1 Gen2 Large Object Heap Gen2 Large Object Heap Gen0 Roots Gen0 Roots Gen1
Things that can cause Memory Leaks Circular References? Form Control Control Control Control +Controls +Parent +Parent +Parent +Parent
Things that can cause memory leaks Rooted References? public class Preferences  { static Preferences instance; public static Preferences GetPrefs() { if (instance == null) instance = new Preferences();        return instance; } public event PrefsChanged; }
Things that can cause memory leaks Rooted References? Form Control Control Control Control Preferences +Controls +Parent +Parent +Parent +Parent $GetPrefs +PrefsChanged
Things that can cause memory leaks Lists, Hashtables, Dictionaries? Control Control Control T List<T> +Parent +Parent +Parent …
Things that can cause memory leaks public class Foo { public static void DoSomething() {        List<Bar> bars; ... //Do Something bar.Clear(); 			bar = null; } }
Type Initializers? public class Foo { 	static Dictionary<string, Bar> _bars; public static Foo() { //Initialize the Lookup table 			_bars = new Dictionary<string, Bar>(); 			_bars.Add(“EndUp”, new Bar()); ... } } Things that can cause memory leaks
Leaking Stack Memory Uncontrolled thread creation. Buggy Thread cleanup Never ending recursion. Things that can cause memory leaks
Leaking Unmanaged Heap memory Interoperating with Unmanaged code through Invoke & Com interop. Abort Finalizers Dynamically generating an assembly in memory. XmlSerializer Things that can cause memory leaks
Leaking Managed Heap memory Large Object Heap Fragmentation. Unneeded Rooted References. Excessive time in GC Finalizers Logging items removed from Cache. Delegates Things that can cause memory leaks
General approach to troubleshooting Identify if is actually a leak. Determine the type of leak (Managed or unmanaged) Analyze objects on the heaps to determine what is being kept alive.
Terms % Time in GC – Percentage of time spent performing GC since last GC cycle. # Bytes in all Heaps – Current memory allocated in all .Net heaps (Gen0-2 + LOH) Gen0, Gen1, Gen2, LargeObject Heap – Current bytes in each of the heaps. Promoted Memory from Gen0, Gen1 – bytes promoted from Gen0 to Gen1 (Gen1 to Gen2) Finalization Survivors - # of objects that survive collection because they are waiting finalization. Private Bytes (Process) – total memory allocated by process that can’t be shared with other processes (includes .Net memory and unmanaged memory)
Demo 7 - Leaky program?
Contact & Reference Material Ronn Black  rblack@btsoft.org http://msdn.microsoft.com/en-us/library/ms973837.aspx (Garbage Collector Basics and Performance Hints) http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&DisplayLang=en (CLR Profiler for .Net 2.0) http://www.openasthra.com/multithreading/heap-overview/ (Heap Overview) http://74.125.155.132/search?q=cache:44hDjSztDf4J:doc.bughunter.net/buffer-overflow/advanced-malloc-exploits.html+malloc+overview&cd=21&hl=en&ct=clnk&gl=us Advanced Malloc exploits http://msdn.microsoft.com/en-us/magazine/cc534993.aspx (Large Object Heap Uncovered) http://msdn.microsoft.com/en-us/library/aa970850.aspx (Weak Event Patterns)

More Related Content

Similar to Analyzing memory usage and leaks

Similar to Analyzing memory usage and leaks (20)

Provenance for Data Munging Environments
Provenance for Data Munging EnvironmentsProvenance for Data Munging Environments
Provenance for Data Munging Environments
 
Memory profiler and garbage collector in C#
Memory profiler and garbage collector in C#Memory profiler and garbage collector in C#
Memory profiler and garbage collector in C#
 
Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"
 
Why Python (for Statisticians)
Why Python (for Statisticians)Why Python (for Statisticians)
Why Python (for Statisticians)
 
What Your Tech Lead Thinks You Know (But Didn't Teach You)
What Your Tech Lead Thinks You Know (But Didn't Teach You)What Your Tech Lead Thinks You Know (But Didn't Teach You)
What Your Tech Lead Thinks You Know (But Didn't Teach You)
 
Getting Git Right
Getting Git RightGetting Git Right
Getting Git Right
 
Stripe CTF3 wrap-up
Stripe CTF3 wrap-upStripe CTF3 wrap-up
Stripe CTF3 wrap-up
 
Leveraging NTFS Timeline Forensics during the Analysis of Malware
Leveraging NTFS Timeline Forensics during the Analysis of MalwareLeveraging NTFS Timeline Forensics during the Analysis of Malware
Leveraging NTFS Timeline Forensics during the Analysis of Malware
 
“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection“Show Me the Garbage!”, Understanding Garbage Collection
“Show Me the Garbage!”, Understanding Garbage Collection
 
Intro to git (UT biocomputing 2015)
Intro to git (UT biocomputing 2015)Intro to git (UT biocomputing 2015)
Intro to git (UT biocomputing 2015)
 
dotMemory 4 - What's inside?
dotMemory 4 - What's inside?dotMemory 4 - What's inside?
dotMemory 4 - What's inside?
 
Static and Dynamic Analysis at Ning
Static and Dynamic Analysis at NingStatic and Dynamic Analysis at Ning
Static and Dynamic Analysis at Ning
 
What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)What is Python? (Silicon Valley CodeCamp 2014)
What is Python? (Silicon Valley CodeCamp 2014)
 
CT Brown - Doing next-gen sequencing analysis in the cloud
CT Brown - Doing next-gen sequencing analysis in the cloudCT Brown - Doing next-gen sequencing analysis in the cloud
CT Brown - Doing next-gen sequencing analysis in the cloud
 
Talk at Bioinformatics Open Source Conference, 2012
Talk at Bioinformatics Open Source Conference, 2012Talk at Bioinformatics Open Source Conference, 2012
Talk at Bioinformatics Open Source Conference, 2012
 
(Re-) Discovering Lost Web Pages
(Re-) Discovering Lost Web Pages(Re-) Discovering Lost Web Pages
(Re-) Discovering Lost Web Pages
 
Introductory Clojure Presentation
Introductory Clojure PresentationIntroductory Clojure Presentation
Introductory Clojure Presentation
 
Bang a Gong, GIT It On, or Running Drupal With a GIT Repository (11/04/20 - B...
Bang a Gong, GIT It On, or Running Drupal With a GIT Repository (11/04/20 - B...Bang a Gong, GIT It On, or Running Drupal With a GIT Repository (11/04/20 - B...
Bang a Gong, GIT It On, or Running Drupal With a GIT Repository (11/04/20 - B...
 
Building Fast and Scalable Persistence Layers with Spring Data JPA
Building Fast and Scalable Persistence Layers with Spring Data JPABuilding Fast and Scalable Persistence Layers with Spring Data JPA
Building Fast and Scalable Persistence Layers with Spring Data JPA
 
Dealing with web scale data
Dealing with web scale dataDealing with web scale data
Dealing with web scale data
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

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
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Analyzing memory usage and leaks

  • 1. Ronn Black October 2010 Analyzing Memory Usage and Leaks
  • 3. NextPtr NextPtr Gen1 Gen2 Large Object Heap Gen2 Large Object Heap Gen0 Roots Gen0 Roots Gen1
  • 4. Things that can cause Memory Leaks Circular References? Form Control Control Control Control +Controls +Parent +Parent +Parent +Parent
  • 5. Things that can cause memory leaks Rooted References? public class Preferences { static Preferences instance; public static Preferences GetPrefs() { if (instance == null) instance = new Preferences(); return instance; } public event PrefsChanged; }
  • 6. Things that can cause memory leaks Rooted References? Form Control Control Control Control Preferences +Controls +Parent +Parent +Parent +Parent $GetPrefs +PrefsChanged
  • 7. Things that can cause memory leaks Lists, Hashtables, Dictionaries? Control Control Control T List<T> +Parent +Parent +Parent …
  • 8. Things that can cause memory leaks public class Foo { public static void DoSomething() { List<Bar> bars; ... //Do Something bar.Clear(); bar = null; } }
  • 9. Type Initializers? public class Foo { static Dictionary<string, Bar> _bars; public static Foo() { //Initialize the Lookup table _bars = new Dictionary<string, Bar>(); _bars.Add(“EndUp”, new Bar()); ... } } Things that can cause memory leaks
  • 10. Leaking Stack Memory Uncontrolled thread creation. Buggy Thread cleanup Never ending recursion. Things that can cause memory leaks
  • 11. Leaking Unmanaged Heap memory Interoperating with Unmanaged code through Invoke & Com interop. Abort Finalizers Dynamically generating an assembly in memory. XmlSerializer Things that can cause memory leaks
  • 12. Leaking Managed Heap memory Large Object Heap Fragmentation. Unneeded Rooted References. Excessive time in GC Finalizers Logging items removed from Cache. Delegates Things that can cause memory leaks
  • 13. General approach to troubleshooting Identify if is actually a leak. Determine the type of leak (Managed or unmanaged) Analyze objects on the heaps to determine what is being kept alive.
  • 14. Terms % Time in GC – Percentage of time spent performing GC since last GC cycle. # Bytes in all Heaps – Current memory allocated in all .Net heaps (Gen0-2 + LOH) Gen0, Gen1, Gen2, LargeObject Heap – Current bytes in each of the heaps. Promoted Memory from Gen0, Gen1 – bytes promoted from Gen0 to Gen1 (Gen1 to Gen2) Finalization Survivors - # of objects that survive collection because they are waiting finalization. Private Bytes (Process) – total memory allocated by process that can’t be shared with other processes (includes .Net memory and unmanaged memory)
  • 15.
  • 16.
  • 17.
  • 18. Demo 7 - Leaky program?
  • 19. Contact & Reference Material Ronn Black rblack@btsoft.org http://msdn.microsoft.com/en-us/library/ms973837.aspx (Garbage Collector Basics and Performance Hints) http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&DisplayLang=en (CLR Profiler for .Net 2.0) http://www.openasthra.com/multithreading/heap-overview/ (Heap Overview) http://74.125.155.132/search?q=cache:44hDjSztDf4J:doc.bughunter.net/buffer-overflow/advanced-malloc-exploits.html+malloc+overview&cd=21&hl=en&ct=clnk&gl=us Advanced Malloc exploits http://msdn.microsoft.com/en-us/magazine/cc534993.aspx (Large Object Heap Uncovered) http://msdn.microsoft.com/en-us/library/aa970850.aspx (Weak Event Patterns)