Practical lessons in memory analysis
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


Practical lessons in memory analysis






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Practical lessons in memory analysis Presentation Transcript

  • 1. Practical Lessons inMemory AnalysisAndrew Johnsonadvisory software engineerIBM United Kingdom Limited
  • 2. 2Based on TS-4118 at JavaOne 2009Original presentation by• Krum Tsvetkov - SAP AG• Andrew Johnson – IBM United Kingdom Limited
  • 3. 3GoalLearn practical tips and tricks for theanalysis of common memory-relatedproblems
  • 4. 4AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 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. 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. 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•
  • 8. 8AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 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:
  • 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. 11How to analyze memory leaksFind the biggest objectsAnalyze why they are kept in memoryAnalyze what makes them big
  • 12. 12Demonstration Analysis of a Memory Leak
  • 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. 14AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 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. 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. 17DemonstrationAnalysis of a Heavy Request
  • 18. 18How thread activity was analyzedInspected thread attributes and localvariables (in object explorer)Analyzed stack trace
  • 19. 19AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 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. 21Find where footprint can be optimizedInspect the set of retained objectsSearch for inefficiently used data structuresLook for redundant data
  • 22. 22Demonstration• Analysis of High Memory Footprint
  • 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. 24AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 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. 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. 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. 28DemonstrationAnalysis of “Leaking” Class Loaders
  • 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. 30AgendaIntroductionAnalyzing Memory LeaksAnalyzing a Heavy RequestReducing Memory FootprintNon-heap MemorySummaryQ & A
  • 31. 31SummaryHeap dumps are very helpful for memoryanalysisMemory Analyzer offers rich analysis featuresetThe analysis can often be automated
  • 32. 32Questions and answersMemory Analyzer homepage @ Eclipse•• Binary downloads• Source downloads• Forum• Getting started guide• Frequently asked questions• Bug reports
  • 33. 33Thank youAndrew Johnson• IBM United Kingdom LimitedMemory Analyzer homepage @ Eclipse•IBM Support Assistant•• IBM Monitoring and Diagnostic Tools – MemoryAnalyzer