Javaday2010 - Jvm Power Tools - What is my JVM doing?

24
Carlo Bonamico - [email protected] - JUG Genova Javaday IV – Roma – 30 gennaio 2010 Managing & Tuning JDK monitoring & management power tools: what is my Java Virtual Machine doing? Carlo Bonamico - NIS s.r.l. / JUG Genova [email protected]

description

We are so used to Java that we often forget what's required behind the scenes to give us robustness, security, platform independence, performance, rich APIs and commercial/open source ecosystem. But it is interesting (and sometimes necessary) to get a deeper look at what's happening behind the scenes... The JVM gives us powerful tools to monitor, debug, tune & troubleshoot its internal mechanisms, including the Garbage Collector and dynamic Just in Time compiler

Transcript of Javaday2010 - Jvm Power Tools - What is my JVM doing?

Page 1: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Managing & Tuning

JDK monitoring & management power tools:

what is my Java Virtual Machine doing?

Carlo Bonamico - NIS s.r.l. / JUG [email protected]

Page 2: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

We are so used to Java...

● That we often forget what's required behind the scenes to give us robustness and security

platform independence

– linux/windows/solaris/you name it...

– 32/64 bit

performance

– with significant increases at every JVM release (think 1.6 vs 1.5)

– benefit to existing code with NO changes

manageability

rich APIs and commercial/open source ecosystem

● But it is interesting (and sometimes necessary) to get a deeper look at what's happening behind the scenes...

Page 3: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

The good news

● is that the JVM lets us (almost) forget about memory management, code compilation and optimization

● The better news is that it also gives us powerful tools to monitor debug tune troubleshoot

● Its internal mechanisms including the Garbage Collector and dynamic Just in Time compiler

Page 4: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

We will learn how to...

● find active JVMs with jps

● anaylze memory consumption and garbage collection behavior with jstat

● get an overview of VM activities with jconsole

● analyze in depth VM behavior with VisualVM identifying local and remote Virtual Machines

performance bottlenecks with VisualVM's profiler

threading issues with the Thread Dump Analyzer

causes of OutOfMemory errors and PermGen errors

● detect memory leaks

● trace and debug running applications in production with btrace

Page 5: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Finding the right VM with jps

● List all java processes● jps

including command line parameters and properties● jps ­lvm

● 29760 sun.tools.jps.Jps ­lvm ­Dapplication.home=/usr/lib/jvm/java­6­sun­1.6.0.16 ­Xms8m

● 4752 org.codehaus.classworlds.Launcher "cli:execute­phase" ­Xmx768m ­Dclassworlds.conf=/home/charlieb/build/apache­maven­2.2.1/bin/m2.conf ­Dmaven.home=/home/charlieb/build/apache­maven­2.2.1

on a remote machine (requires jstatd running on the server)● jps ­lvm server

Page 6: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

jstat

● To enable remote access to a machine stats● jstatd ­Djava.security.policy=jstatd.policy

which contains● grant codebase "file:${java.home}/../lib/tools.jar" {●    permission java.security.AllPermission;● };●

Page 7: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Efficient Garbage Collection

● Almost nobody does reference counting today :-) When your JVM is using a few GBs of memory, it cannot check all

objects all the time

● So you need a smart way of separating objects that are likely to be collected from the ones in use generations: objects that have been created recently are more likely to

be collected soon– think of request attributes

objects that have been there for a long time are unlikely to be collected – think of Spring services, EJBs, singletons

Page 8: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Generational Collection in the JVM

● Heap space split into Eden

– Young objects

Survivor 1/2

– Older objects

Old

● Perm(anent)Gen(eration)area for objects that will not be deallocated until the end class and method objects

Page 9: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Start with an OOM / Permgen

● OutOfMemoryError but increasing -Xmx will not help...

● Then it might be a PermGen space saturation increase -XX:MaxPermSize=256m find and detect classloading issues

Page 10: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Monitoring with jstat

● Command line tool for accessing GC and JIT stats● jstat ­gcutil <vmid> <interval>● jstat ­gcutil 21891 250

refresh every 250 ms●   S0     S1     E      O      P     YGC    YGCT    FGC    FGCT     GCT●  12.44   0.00  27.20   9.49  96.70    78    0.176     5    0.495    0.672●  12.44   0.00  62.16   9.49  96.70    78    0.176     5    0.495    0.672●  12.44   0.00  83.97   9.49  96.70    78    0.176     5    0.495    0.672●   0.00   7.74   0.00   9.51  96.70    79    0.177     5    0.495    0.673●   0.00   7.74  23.37   9.51  96.70    79    0.177     5    0.495    0.673●   0.00   7.74  43.82   9.51  96.70    79    0.177     5    0.495    0.673●   0.00   7.74  58.11   9.51  96.71    79    0.177     5    0.495    0.673

S0/S1 = survivor spaces, E = Eden, O = Old, P = Permanent All values are % of available space

GC

Page 11: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

JVM Overview with jconsole

● Included since JDK 1.5 memory analysis CPU load Classloading stats Thread stats Classpath JMX console (and more...)

Page 12: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Classpath, launch arguments and properties

Page 13: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Incremental GC as seen by jconsole

Page 14: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

In-depth with (j)visualvm

● Even more powerful than jconsole Included in the JDK since 1.6.0_07

● Supports local VMs through the

JVMPI interface– the same used by any IDE

for debug

remote VMs through– jstatd

– direct JMX over RMI connection

Page 15: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Thread Monitoring and Dump

Page 16: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Memory analysis

Page 17: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

And more...

● CPU/Memory profiler subset of

NetBeans profiler new sampling

profiler since version 1.2.1

Page 18: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

BTrace

● B(ytecode)Trace conceptually similar to Solaris DTrace can be used on any Java platform/app.

● Dynamically connects to a running Java application and attach short “scripts” which log a range of events and infos

such as– method calls

– execution times

– constructor invocation

– available memory

Page 19: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Btrace scripts

● Script execution is tied to several situations reaching a specific line number invoking a given method

– identified by signature or annotation

returning from a method invoking system calls (e.g. exit) recurring timer

● In a way, BTrace scripts are very similar to read-only AOP’s aspects can be attached to any existing Java code

Page 20: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Examples

● The scripts are simple Java classes which take advantage of methods from the btrace API simple but powerful annotations.

● Tracing session creation with hibernate● @BTrace

● public class HelloWorldTrace {

● // probe the openSession() method of Hibernate SessionFactory

● @OnMethod(

● clazz=”org.hibernate.SessionFactory”,

● method=”openSession”

● )

● public static void onOpenSessionTrace() {

● // you can only log using the stati println method from BTraceUtils

● println(”Hibernate: opening a new session…”);

● }

● }

Page 21: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Examples: JAX-WS

● Intercepts all calls to methods in classes which are annotated with @WebService and logs execution times

● @OnMethod(● clazz="@javax.jws.WebService",● method="@javax.jws.WebMethod"● )● public static void onWebserviceEntry() {

– print("entering webservice ");– println(strcat(strcat(name(probeClass()), "."), probeMethod()));– startTime = timeMillis();

● }●

● @OnMethod(● clazz="@javax.jws.WebService",● method="@javax.jws.WebMethod",● location=@Location(Kind.RETURN)● )● public static void onWebserviceReturn() {

– println(strcat("Time taken (msec) ", str(timeMillis() ­ startTime)));● }

Page 22: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Yesterday news

● JDK 1.6.0_18 released last week includes visualvm 1.2.1 includes NUMA-aware Garbage Collector

– Non-Uniform Memory Allocation– takes into account that the cost of accessing RAM is not

uniform anymore– 30% more efficient with large memory allocations– linux/solaris only :-)

Page 23: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

References

● VisualVM official site https://visualvm.dev.java.net

● BTrace https://btrace.dev.java.net

● Articles on Hotspot http://java.sun.com/javase/technologies/hotspot/index

.jsp

Page 24: Javaday2010 - Jvm Power Tools - What is my JVM doing?

Carlo Bonamico - [email protected] - JUG Genova

Javaday IV – Roma – 30 gennaio 2010

Thanks for your attention!

● Learn more at http://www.carlobonamico.com/blog http://juggenova.net

– presentations, demos, code samples● Meet me at the my company (NIS) booth in the

exhibition hall

● Contact me at [email protected]

● Related reading: Continuous Integration with Hudson http://www.slideshare.net/carlo.bonamico/continuous-integration-with-hudson