SlideShare a Scribd company logo
Debugging Your
Production JVM Machine
Ken Sipe
Perficient (PRFT)
kensipe@gmail.com
Abstract
> Learn the tools and techniques used to monitor,
trace and debugging running Java applications.
2
Agenda
> Java Memory Management
> Memory Management Tools
• Command-line Tools
• VisualVM
> Btrace
> Summary
> Resources
3
Agenda
> Java Memory Management
> Memory Management Tools
• Command-line Tools
• VisualVM
> Btrace
> Summary
> Resources
4
Donʼt Worry…
> C (malloc / free)
> C++ (new / delete)
> Java (new / gc)
> Memory Allocation / Deallocation in Java is
automatic
5
Object Lifetimes
> Most objects live very short lives
• 80-98% of all newly allocated objects die
• within a few million instructions
• Before another megabyte is allocated
> Old objects tend to live a long…. time
Memory Spaces
> New Space
• Where all objects are new (sort of…)
> Old Space
• Where all objects are old
New Space Old Space
Memory Spaces
> New Space Division
• Eden
• Where all objects are created
• Survivor Space 0
• Provide object aging
• Survivor Space 1
Eden Old SpaceSS0 SS1
Perm Spaces
> Permanent Space
• class information
• static information
Eden Old SpaceSS0 SS1 Perm Space
GC Responsibility
> Heap Walking from GC Roots
> Mark / Sweep
• Garbage detection (mark)
• Sort out the live ones from the dead ones
• Reference counting
• Garbage reclamation (sweep)
• Make space available
Minor Garbage Collection
> Minor gc (scavenge)
• When eden is “full” a minor gc is invoked
• Sweeps through eden and the current survivor
space, removing the dead and moving the living to
survivor space or old
• Ss0 and ss1 switch which is “current”
• A new tenuring age is calculated
Major Garbage Collection
> Major gc
• When old is “full”
• All spaces are garbage collected including perm
space
• All other activities in the jvm are suspended
Debugging Your Production JVM
Agenda
> Java Memory Management
> Memory Management Tools
• Command-line Tools
• VisualVM
> Btrace
> Summary
> Resources
14
Java Memory Tools
> JPS
• Getting the Process ID (PID)
> Jstat
• jstat -gcutil <pid> 250 7
Looking at the Heap
> %JAVA_HOME%/bin/jmap – histo:live <pid>
• Looking at all the “live” objects
> %JAVA_HOME%/bin/jmap – histo <pid>
• Looking at all objects
> The difference between is the list of unreachable
objects
Taking a Heap Dump
> %JAVA_HOME%/bin/jmap –
dump:live,file=heap.out,format=b <pid>
• Dumps the Heap to a file
> JConsole
JHat
> %JAVA_HOME%/bin/jhat <filename>
• Starts a web server to investigate the heap
> Queries
• Show instance count for all classes
• Show Heap Histogram
• Show Finalizer
• Use the Execute Object Query Language (OQL)
• select s from java.lang.String s where s.count >=100
JMX – Looking at Flags
MAT – Memory Analyzer Tool
VisualVM
> Open Source All-in-One Java Troubleshooting tool
> https://visualvm.dev.java.net/
21
Visualgc
> visualgc <pid>
> Visual Garbage Collection
Monitoring
• a graphical tool for monitoring the
HotSpot Garbage Collector,
Compiler, and class loader. It can
monitor both local and remote
JVMs.
23
DEMO
Agenda
> Java Memory Management
> Memory Management Tools
• Command-line Tools
• VisualVM
> Btrace
> Summary
> Resources
24
BTrace
> dynamically bytecode instrumenting (BCI)
• read / not write
• probe-based
• observe running Java applications
> Integration with DTrace on Solaris
> http://btrace.dev.java.net
> visualvm plugin
25
BTrace Tools
26
Target JVM
BTrace
Agent
VisualVM +
BTrace Plugin
BTrace command-line
BTrace Terminology
> Probe Point
• “location” or “event” at which tracing statements are
executed
> Trace Actions
• statements which are executed whenever a probe
fires
> Action Methods
• static methods which define a trace
27
Probes and Actions
> Probe Targets
• method entry / exit
• line number
• exceptions
• return from method
• exception throw (before)
• synchronization entry / exit
> Actions
• static methods in trace class
28
BTrace Restrictions
> no new objects
> no new arrays
> no exceptions
> no outer, inner, nested or
local classes
> no synchronization blocks
> no loops
> no interfaces
29
Tracing
> Annotations
• com.sun.btrace.annotations
• @BTrace
• denotes a btrace class
> Probe Points
• @OnMethod
• @OnTimer
• @OnEvent
• @OnExit
30
Simple Example: Looking for Object Size
31
Simple Example: Looking for Object Size
32
BTrace Run Options
> BTrace Command-Line
• btrace
• runs btrace tool (and compiles btrace script if necessary)
• btracec
• btrace script compiler
• btracer
• convenience script to start java project with tracing enabled at
application startup
> VisualVM + BTrace Plugin
33
Testing the Probe
> Start Target Application
• java -jar java2demo.jar
> Get the PID
• jps
> Inject Probe
• btrace <pid> Sizeof.java
34
BTrace + JMX Script
35
BTrace + jstat
36
ThreadLocal
37
BTrace Events
38
Checking Synchronization Entry / Exits
39
Tracing Opportunities
> Thread Monitors
> Socket / Web Services
> Object Creation and Size
> File Access
40
41
DEMO
Known Solutions with BTrace
> Terracotta
• concurrency issue
> Hibernate / Atlassian Issue
• thread / session management issue
• http://jira.atlassian.com/browse/CONF-12201
42
Summary
> jmap, jhat, jconsole, jstat
• tools already in the JDK bin directory
> VisualVM
• swiss army knife for JVM process monitoring and
tracing
> BTrace
• dynamic tracing tool
43
Resources
> Performance and Troubleshooting
• http://java.sun.com/performance/reference/
whitepapers/6_performance.html
• http://java.sun.com/javase/6/webnotes/trouble/TSG-
VM/html/docinfo.html
> VisualVM
• https://visualvm.dev.java.net/
> BTrace
• https://btrace.dev.java.net/
44
Ken Sipe
http://kensipe.blogspot.com
twitter: @kensipe

More Related Content

ODP
Quick introduction to Java Garbage Collector (JVM GC)
ODP
Java Garbage Collection, Monitoring, and Tuning
PPT
An Introduction to JVM Internals and Garbage Collection in Java
PDF
JVM Garbage Collection Tuning
PDF
Java 9: The (G1) GC Awakens!
PDF
Performance and predictability (1)
PPTX
JVM @ Taobao - QCon Hangzhou 2011
PDF
Collections forceawakens
Quick introduction to Java Garbage Collector (JVM GC)
Java Garbage Collection, Monitoring, and Tuning
An Introduction to JVM Internals and Garbage Collection in Java
JVM Garbage Collection Tuning
Java 9: The (G1) GC Awakens!
Performance and predictability (1)
JVM @ Taobao - QCon Hangzhou 2011
Collections forceawakens

What's hot (20)

PDF
Let's talk about Garbage Collection
PDF
Performance and predictability
PDF
Java collections the force awakens
PDF
JCConf 2018 - Retrospect and Prospect of Java
PDF
Java Garbage Collection - How it works
PDF
cb streams - gavin pickin
PPTX
Stream processing from single node to a cluster
PPTX
JVM languages "flame wars"
PPTX
Millions quotes per second in pure java
PDF
Understanding Garbage Collection
PPT
Garbage collection in JVM
PPTX
BDM32: AdamCloud Project - Part II
PDF
(JVM) Garbage Collection - Brown Bag Session
PDF
Performance van Java 8 en verder - Jeroen Borgers
PPTX
Dive into spark2
PPTX
Java GC
PDF
Extending Node.js using C++
PPTX
RedisConf17 - Internet Archive - Preventing Cache Stampede with Redis and XFetch
PDF
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
PDF
Time Series Processing with Solr and Spark: Presented by Josef Adersberger, Q...
Let's talk about Garbage Collection
Performance and predictability
Java collections the force awakens
JCConf 2018 - Retrospect and Prospect of Java
Java Garbage Collection - How it works
cb streams - gavin pickin
Stream processing from single node to a cluster
JVM languages "flame wars"
Millions quotes per second in pure java
Understanding Garbage Collection
Garbage collection in JVM
BDM32: AdamCloud Project - Part II
(JVM) Garbage Collection - Brown Bag Session
Performance van Java 8 en verder - Jeroen Borgers
Dive into spark2
Java GC
Extending Node.js using C++
RedisConf17 - Internet Archive - Preventing Cache Stampede with Redis and XFetch
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Time Series Processing with Solr and Spark: Presented by Josef Adersberger, Q...
Ad

Similar to Debugging Your Production JVM (20)

PDF
java-monitoring-troubleshooting
PPT
Heap & thread dump
PPT
12 virtualmachine
PDF
Solaris Kernel Debugging V1.0
PDF
Why GC is eating all my CPU?
PPTX
DIY Java Profiling
PDF
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
PDF
One-Byte Modification for Breaking Memory Forensic Analysis
PDF
.NET Core, ASP.NET Core Course, Session 5
PPTX
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
PDF
Profiler Guided Java Performance Tuning
PPTX
Micrometrics to forecast performance tsunamis
PDF
Solaris DTrace, An Introduction
PPTX
Day 7 - Make it Fast
PPTX
mobile development with androiddfdgdfhdgfdhf.pptx
PDF
JDK Tools For Performance Diagnostics
PDF
Building source code level profiler for C++.pdf
PDF
Solr Troubleshooting - TreeMap approach
PDF
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
java-monitoring-troubleshooting
Heap & thread dump
12 virtualmachine
Solaris Kernel Debugging V1.0
Why GC is eating all my CPU?
DIY Java Profiling
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
One-Byte Modification for Breaking Memory Forensic Analysis
.NET Core, ASP.NET Core Course, Session 5
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
Profiler Guided Java Performance Tuning
Micrometrics to forecast performance tsunamis
Solaris DTrace, An Introduction
Day 7 - Make it Fast
mobile development with androiddfdgdfhdgfdhf.pptx
JDK Tools For Performance Diagnostics
Building source code level profiler for C++.pdf
Solr Troubleshooting - TreeMap approach
Solr Troubleshooting - Treemap Approach: Presented by Alexandre Rafolovitch, ...
Ad

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Modernizing your data center with Dell and AMD
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Spectral efficient network and resource selection model in 5G networks
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
Teaching material agriculture food technology
Modernizing your data center with Dell and AMD
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Debugging Your Production JVM

  • 1. Debugging Your Production JVM Machine Ken Sipe Perficient (PRFT) kensipe@gmail.com
  • 2. Abstract > Learn the tools and techniques used to monitor, trace and debugging running Java applications. 2
  • 3. Agenda > Java Memory Management > Memory Management Tools • Command-line Tools • VisualVM > Btrace > Summary > Resources 3
  • 4. Agenda > Java Memory Management > Memory Management Tools • Command-line Tools • VisualVM > Btrace > Summary > Resources 4
  • 5. Donʼt Worry… > C (malloc / free) > C++ (new / delete) > Java (new / gc) > Memory Allocation / Deallocation in Java is automatic 5
  • 6. Object Lifetimes > Most objects live very short lives • 80-98% of all newly allocated objects die • within a few million instructions • Before another megabyte is allocated > Old objects tend to live a long…. time
  • 7. Memory Spaces > New Space • Where all objects are new (sort of…) > Old Space • Where all objects are old New Space Old Space
  • 8. Memory Spaces > New Space Division • Eden • Where all objects are created • Survivor Space 0 • Provide object aging • Survivor Space 1 Eden Old SpaceSS0 SS1
  • 9. Perm Spaces > Permanent Space • class information • static information Eden Old SpaceSS0 SS1 Perm Space
  • 10. GC Responsibility > Heap Walking from GC Roots > Mark / Sweep • Garbage detection (mark) • Sort out the live ones from the dead ones • Reference counting • Garbage reclamation (sweep) • Make space available
  • 11. Minor Garbage Collection > Minor gc (scavenge) • When eden is “full” a minor gc is invoked • Sweeps through eden and the current survivor space, removing the dead and moving the living to survivor space or old • Ss0 and ss1 switch which is “current” • A new tenuring age is calculated
  • 12. Major Garbage Collection > Major gc • When old is “full” • All spaces are garbage collected including perm space • All other activities in the jvm are suspended
  • 14. Agenda > Java Memory Management > Memory Management Tools • Command-line Tools • VisualVM > Btrace > Summary > Resources 14
  • 15. Java Memory Tools > JPS • Getting the Process ID (PID) > Jstat • jstat -gcutil <pid> 250 7
  • 16. Looking at the Heap > %JAVA_HOME%/bin/jmap – histo:live <pid> • Looking at all the “live” objects > %JAVA_HOME%/bin/jmap – histo <pid> • Looking at all objects > The difference between is the list of unreachable objects
  • 17. Taking a Heap Dump > %JAVA_HOME%/bin/jmap – dump:live,file=heap.out,format=b <pid> • Dumps the Heap to a file > JConsole
  • 18. JHat > %JAVA_HOME%/bin/jhat <filename> • Starts a web server to investigate the heap > Queries • Show instance count for all classes • Show Heap Histogram • Show Finalizer • Use the Execute Object Query Language (OQL) • select s from java.lang.String s where s.count >=100
  • 19. JMX – Looking at Flags
  • 20. MAT – Memory Analyzer Tool
  • 21. VisualVM > Open Source All-in-One Java Troubleshooting tool > https://visualvm.dev.java.net/ 21
  • 22. Visualgc > visualgc <pid> > Visual Garbage Collection Monitoring • a graphical tool for monitoring the HotSpot Garbage Collector, Compiler, and class loader. It can monitor both local and remote JVMs.
  • 24. Agenda > Java Memory Management > Memory Management Tools • Command-line Tools • VisualVM > Btrace > Summary > Resources 24
  • 25. BTrace > dynamically bytecode instrumenting (BCI) • read / not write • probe-based • observe running Java applications > Integration with DTrace on Solaris > http://btrace.dev.java.net > visualvm plugin 25
  • 26. BTrace Tools 26 Target JVM BTrace Agent VisualVM + BTrace Plugin BTrace command-line
  • 27. BTrace Terminology > Probe Point • “location” or “event” at which tracing statements are executed > Trace Actions • statements which are executed whenever a probe fires > Action Methods • static methods which define a trace 27
  • 28. Probes and Actions > Probe Targets • method entry / exit • line number • exceptions • return from method • exception throw (before) • synchronization entry / exit > Actions • static methods in trace class 28
  • 29. BTrace Restrictions > no new objects > no new arrays > no exceptions > no outer, inner, nested or local classes > no synchronization blocks > no loops > no interfaces 29
  • 30. Tracing > Annotations • com.sun.btrace.annotations • @BTrace • denotes a btrace class > Probe Points • @OnMethod • @OnTimer • @OnEvent • @OnExit 30
  • 31. Simple Example: Looking for Object Size 31
  • 32. Simple Example: Looking for Object Size 32
  • 33. BTrace Run Options > BTrace Command-Line • btrace • runs btrace tool (and compiles btrace script if necessary) • btracec • btrace script compiler • btracer • convenience script to start java project with tracing enabled at application startup > VisualVM + BTrace Plugin 33
  • 34. Testing the Probe > Start Target Application • java -jar java2demo.jar > Get the PID • jps > Inject Probe • btrace <pid> Sizeof.java 34
  • 35. BTrace + JMX Script 35
  • 40. Tracing Opportunities > Thread Monitors > Socket / Web Services > Object Creation and Size > File Access 40
  • 42. Known Solutions with BTrace > Terracotta • concurrency issue > Hibernate / Atlassian Issue • thread / session management issue • http://jira.atlassian.com/browse/CONF-12201 42
  • 43. Summary > jmap, jhat, jconsole, jstat • tools already in the JDK bin directory > VisualVM • swiss army knife for JVM process monitoring and tracing > BTrace • dynamic tracing tool 43
  • 44. Resources > Performance and Troubleshooting • http://java.sun.com/performance/reference/ whitepapers/6_performance.html • http://java.sun.com/javase/6/webnotes/trouble/TSG- VM/html/docinfo.html > VisualVM • https://visualvm.dev.java.net/ > BTrace • https://btrace.dev.java.net/ 44