Practical Lessons inMemory AnalysisAndrew Johnsonadvisory software engineerIBM United Kingdom Limited
2Based on TS-4118 at JavaOne 2009Original presentation by• Krum Tsvetkov - SAP AG• Andrew Johnson – IBM United Kingdom Li...
3GoalLearn practical tips and tricks for theanalysis of common memory-relatedproblems
4AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ ...
5Why care about memory?Memory is a crucial resourceExcessive memory usage can cause:• Crashes in the application• Unresp...
6How will this session help me?Present common memory-related issuesGive hints how to solve them using heapdumps and Memo...
7Heap dumps are useful for analysisHeap dumps come “for free”They are suitable for productionThe comprehensive data all...
8AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ ...
9First, collect data for analysisEnable heap dumps on OutOfMemoryErrorTrigger heap dumps on demand if neededHow-to for ...
10How to get a “good” heap dump When memory is exhausted the leak will occupymost of the processing space Ensure big eno...
11How to analyze memory leaksFind the biggest objectsAnalyze why they are kept in memoryAnalyze what makes them big
12Demonstration Analysis of a Memory Leak
13How the leak analysis was doneGot a “good” heap dumpFound the biggest objects (in the dominatortree)Analyzed who kept...
14AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ...
15What is a heavy request?An expensive operation which can cause anOutOfMemoryError“Acute” rather than a “chronic” probl...
16Inspect the thread to find its activityLook at the thread attributes - name, class,etc...Look at the local variablesL...
17DemonstrationAnalysis of a Heavy Request
18How thread activity was analyzedInspected thread attributes and localvariables (in object explorer)Analyzed stack trace
19AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ...
20Why is memory footprint important? A lower memory footprint can improve:• The scaling of an application / server• Perfo...
21Find where footprint can be optimizedInspect the set of retained objectsSearch for inefficiently used data structures...
22Demonstration• Analysis of High Memory Footprint
23How memory footprint was analyzedAnalyzed retained objects (in dominatortree, retained set)Used “Group by Value” to fi...
24AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ...
25Non-heap memory can be exhaustedWhat is in the non-heap memory?• Metadata for classes• Interned Strings (for some JVMs)...
26Why is a class loader not released? The trivial case – thereis a “normal” referenceto it A common case – it isthe cont...
27How to analyze “leaking” class loadersFind classes loaded more than onceFind redundant loaders by their nameLook at t...
28DemonstrationAnalysis of “Leaking” Class Loaders
29How “leaking” class loaders analysis was doneUsed the “Duplicate Classes” queryFound the loaders that load redundantcl...
30AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ...
31SummaryHeap dumps are very helpful for memoryanalysisMemory Analyzer offers rich analysis featuresetThe analysis can ...
32Questions and answersMemory Analyzer homepage @ Eclipse• http://www.eclipse.org/mat/• Binary downloads• Source download...
33Thank youAndrew Johnson• IBM United Kingdom LimitedMemory Analyzer homepage @ Eclipse• http://www.eclipse.org/mat/IBM...
Upcoming SlideShare
Loading in …5
×

Practical lessons in memory analysis

4,769 views

Published on

Published in: Education, Technology

Practical lessons in memory analysis

  1. 1. Practical Lessons inMemory AnalysisAndrew Johnsonadvisory software engineerIBM United Kingdom Limited
  2. 2. 2Based on TS-4118 at JavaOne 2009Original presentation by• Krum Tsvetkov - SAP AG• Andrew Johnson – IBM United Kingdom Limited
  3. 3. 3GoalLearn practical tips and tricks for theanalysis of common memory-relatedproblems
  4. 4. 4AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  5. 5. 5Why care about memory?Memory is a crucial resourceExcessive memory usage can cause:• Crashes in the application• Unresponsive applications• Unpredictable program behaviorMemory analysis is complicated
  6. 6. 6How will this session help me?Present common memory-related issuesGive hints how to solve them using heapdumps and Memory AnalyzerShow many demonstrations using real-lifeheap dumpsShow how to simplify analysis byautomating it
  7. 7. 7Heap dumps are useful for analysisHeap dumps come “for free”They are suitable for productionThe comprehensive data allows detailedanalysisThere is wide platform coverage• HPROF dumps from HotSpot based JVMs• DTFJ system dumps from IBM JVMs• Portable Heap Dumps (PHD) file from IBM JVMsJSR-326/Apache Kato standardization• http://incubator.apache.org/projects/kato.html
  8. 8. 8AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  9. 9. 9First, collect data for analysisEnable heap dumps on OutOfMemoryErrorTrigger heap dumps on demand if neededHow-to for getting a heap dump:http://wiki.eclipse.org/index.php/MemoryAnalyzer#Gett
  10. 10. 10How to get a “good” heap dump When memory is exhausted the leak will occupymost of the processing space Ensure big enough processing space, this will makethe leak easier to findMaxHeapMinHeapProcessingSpaceBaseMemory
  11. 11. 11How to analyze memory leaksFind the biggest objectsAnalyze why they are kept in memoryAnalyze what makes them big
  12. 12. 12Demonstration Analysis of a Memory Leak
  13. 13. 13How the leak analysis was doneGot a “good” heap dumpFound the biggest objects (in the dominatortree)Analyzed who kept them alive (using paths)Analyzed what made them big (looking attheir retained set)Used “Leak Report” to automate theanalysis
  14. 14. 14AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  15. 15. 15What is a heavy request?An expensive operation which can cause anOutOfMemoryError“Acute” rather than a “chronic” problemExamples:• Reading a big file in memory not in chunks• Try to load a whole DB table in memory
  16. 16. 16Inspect the thread to find its activityLook at the thread attributes - name, class,etc...Look at the local variablesLook at the stack of the thread
  17. 17. 17DemonstrationAnalysis of a Heavy Request
  18. 18. 18How thread activity was analyzedInspected thread attributes and localvariables (in object explorer)Analyzed stack trace
  19. 19. 19AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  20. 20. 20Why is memory footprint important? A lower memory footprint can improve:• The scaling of an application / server• Performance by increasing the time between GCsMaxHeapMinHeapProcessingSpaceBaseMemory
  21. 21. 21Find where footprint can be optimizedInspect the set of retained objectsSearch for inefficiently used data structuresLook for redundant data
  22. 22. 22Demonstration• Analysis of High Memory Footprint
  23. 23. 23How memory footprint was analyzedAnalyzed retained objects (in dominatortree, retained set)Used “Group by Value” to find redundantobjectsUsed the commands from the “Collections”groupUsed “Component Report” to automate theanalysis
  24. 24. 24AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  25. 25. 25Non-heap memory can be exhaustedWhat is in the non-heap memory?• Metadata for classes• Interned Strings (for some JVMs)• NIO direct byte buffers• BitmapsHow can it be exhausted?• Too many interned Strings• Classes packaged and loaded multiple times• Class loaders which are not properly released
  26. 26. 26Why is a class loader not released? The trivial case – thereis a “normal” referenceto it A common case – it isthe context class loaderof a thread The registry problem –one instance is enoughClass loaderClass ClassInstance Instance Instance InstanceInstanceRegistryRoot
  27. 27. 27How to analyze “leaking” class loadersFind classes loaded more than onceFind redundant loaders by their nameLook at the paths to the suspect classloaders
  28. 28. 28DemonstrationAnalysis of “Leaking” Class Loaders
  29. 29. 29How “leaking” class loaders analysis was doneUsed the “Duplicate Classes” queryFound the loaders that load redundantclassesAnalyzed why they are not released (usingpaths)
  30. 30. 30AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  31. 31. 31SummaryHeap dumps are very helpful for memoryanalysisMemory Analyzer offers rich analysis featuresetThe analysis can often be automated
  32. 32. 32Questions and answersMemory Analyzer homepage @ Eclipse• http://www.eclipse.org/mat/• Binary downloads• Source downloads• Forum• Getting started guide• Frequently asked questions• Bug reports
  33. 33. 33Thank youAndrew Johnson• IBM United Kingdom LimitedMemory Analyzer homepage @ Eclipse• http://www.eclipse.org/mat/IBM Support Assistant• http://www-01.ibm.com/software/support/isa/• IBM Monitoring and Diagnostic Tools – MemoryAnalyzer

×