Вы находитесь на странице: 1из 58

Java Trouble-Shooting


Sang Shin

Sun Microsystems, Inc.

javapassion.com

1
Topics
• JDK Debugging Tools
• JPDA (Java Platform Debugger Architecture)
architecture
• Remote Debugging

2
Performance,
Debugging,
Monitoring,
Management,
Code analysis,
Testing
Free online course

http://javapassion.com/
javaperformance/ 3
JDK Debugging Tools
jps
• List the JVMs that are currently running including
embedded VMs
> Associate a 'process number' with the running application
• jps
27798 Jps
25301 Main

• jps
-s
28029 sun.tools.jps.Jps
25301 org.netbeans.Main

• jps
-l
25301 Main -Djdk.home=/opt/java/javase/jdk1.6
-Dnetbeans.dirs=/opt/java/tools/netbeans-
6.0.1/nb6.0:/opt/java/tools/netbeans-
6.0.1/ide8:/opt/java/tools/netbeans- 5
jinfo
• List configuration information from a running VM or
a core file
> Information includes VM properties and command line
flags
• jinfo
<pid
from
jps>
Java System Properties:
java.vendor = Sun Microsystems Inc.
netbeans.user = /home/shulk/.netbeans/6.0
sun.java.launcher = SUN_STANDARD
sun.management.compiler = HotSpot Client Compiler
...
VM Flags:
-Djdk.home=/opt/java/javase/jdk1.6 -Dnetbeans.dirs=/opt/java/tools/netbeans-
6.0.1/nb6.0:/opt/java/tools/netbeans-6.0.1/ide8:/opt/java/tools...

6
jstat
• List the statistics for a given VM
> Class loading, GC on all spaces, hotspot compilation
• Provide a sample interval and the number of
samples to take

Process id Interval No. of sample

jstat -gcutil 25301 1000 10


S0 S1 E O P YGC YGCT FGC FGCT GCT
73.79 0.00 81.32 42.10 99.56 1344 8.880 36 17.363 26.243
73.79 0.00 81.32 42.10 99.56 1344 8.880 36 17.363 26.243
73.79 0.00 81.32 42.10 99.56 1344 8.880 36 17.363 26.243

7
jstack
• Prints the stack traces of all the the threads
attached to a virtual machine
> Application thread, internal VM thread,
• Also performs deadlock detection with -l option
• Use -F to force stack if VM is hung
> Solaris and Linux only

8
jstack – Sample Output

"Java Source Worker Thread" prio=10 tid=0x08267800 nid=0x63a5 waiting on condition


[0x4532c000..0x4532d040]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x54b8d090> (a
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:198)
...
at java.util.concurrent.ThreadPoolExecuto$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)

Locked ownable synchronizers:


- <0x54b8cdd0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)

9
jmap
• Prints memory related statistics
> Details the overall memory configuration
> Details section on each of the space with capacity, free
and used

10
jmap Sample Output (Solaris/Linux)
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 169869312 (162.0MB)
NewSize = 1048576 (1.0MB)
MaxNewSize = 4294901760 (4095.9375MB)
OldSize = 4194304 (4.0MB)
NewRatio = 12
SurvivorRatio = 8
PermSize = 33554432 (32.0MB)
MaxPermSize = 209715200 (200.0MB)
...
Eden Space:
capacity = 6881280 (6.5625MB)
used = 4437312 (4.23175048828125MB)
free = 2443968 (2.33074951171875MB)
64.48381696428571% used

11
jmap (Solaris/Linux)

12
jmap (Windows)

13
HPROF
• Uses the JVMTI to get information from a Java VM
• Data capture includes
> CPU usages, heap dump, thread states
• Start at commandline
> java
-Xrunhprof:<options>
MyJavaApp
> Includes format=b for jhat analysis
• Useful options
> heap=all – displays all heap info
> cpu=sample – sample active thread

• Dump on application exit or CTRL-\


14
jhat
• Allows you to interactively work with a memory
snapshot captured by jmap
> Use jmap
-dump:format=b,file=heap_dump_file
• Use jhat to 'mount' heap file
> Hosted on a web server
> Access through a standard browser
• Shows the following (standard query)
> All classes
> Object on the heap
> Instances
> Objects reachable from root set
15
jhat Object Query Language
• Develop custom query with object query language
> SQL like, uses JavaScript for expression in from and
where clause
> A set of built-in functions like heap, referrers, reachables,
sizeof, etc.
• Use to answer questions like
> Find all String instances that are over 1K in size
select
s
from
j.l.Sring
s
where
s.count
>=
1024
> Find all URL instances that is referenced by 2 or more
objects
select
u
from
j.n.URL
u
where
count(referrers(u))
>
2

16
Demo:
JDK Debugging Tools

http://www.javapassion.com/handsonlabs/javase6tools
17
Visual Tool - jconsole
• Bundle with JDK
> Graphical console that enables you monitor and manage
Java applications
> API to create your own plugin to jconsole
• Provides information on
> Memory usage and GC activities
> Threads, thread stack trace, locks
> Objects pending finalization
> Runtime information such as uptime, CPU time
> JVM information such as classpath, properties,
command line arguments, etc.
18
Visual Tool – visualvm
• Open source project
> Based on NetBeans platform, uses the update center
> Current plugin includes VisualGC, jconsole, thread dump
analyzer, profiler
> Runs only on JDK 6 and later
> http://visualvm.dev.java.net

19
Demo:
VisualVM

http://www.javapassion.com/handsonlabs/javavisualvm
20
When to Use Them
over NetBeans Profiler?
Usecases
• Use jps to find which PID you want to dump
• Use jmap to generate a heap dump that can be
opened with the NetBeans profiler
• Use OQL capability of jhat
> Other features of jhat are now addressed by
HeapWalker
• Use jstack when full attach of the profiler is too
much of a hassle
> Useful in the field

22
Java Platform Debugger
Architecture (JPDA)
Topics
• What is JPDA?
• JPDA architecture
• Using JPDA

24
What is JPDA?
What is JPDA?
• Java Platform Debugger Architecture
• JPDA is a multi-tiered debugging architecture
• Allows tools developers to easily create
debugger applications
• Portable across platforms, virtual machine (VM)
implementations and JDK versions.

26
Goals of JPDA
• To provide standard interfaces which allow Java
programming language debugging tools to be easily written
without regard to platform specifics such hardware,
operating system and virtual machine implementation.
• To describe a complete architecture for implementing these
interfaces, including remote and cross-platform debugging.
• To provide a reference implementation of this architecture.
• To provide a highly modular architecture where the
implementation and/or client of an interface can be different
than the reference implementation or different from the
JPDA component.
27
JPDA Architecture
JPDA Architecture

29
JPDA Architecture
• JVM TI (Java VM Tool Interface)
> JVM TI is a new interface introduced in J2SE 5.0
which replaces JVMDI.
> It defines the debugging services a VM provides.
• JDWP (Java Debug Wire Protocol)
> Defines the communication between debuggee and
debugger processes.
• JDI (Java Debug Interface)
> Defines a high-level Java language interface which
tool developers can easily use to write remote
debugger applications.
30
JPDA is Layered Architecture
• Reference implementation includes
> JVM TI implementations on multiple Sun VMs (see
VM documentation).
> A back-end which uses JVM TI to implement the
debuggee side of JDWP.
> A front-end which uses the debugger side of JDWP
to implement JDI.
> Two simple example debugger applications which
are built on JDI.
• Implementations may be substituted.

31
Using JPDA
Using JPDA
• A debugger developer may hook into JPDA at
any layer.
• Since the JDI is the highest level and easiest to
use we encourage developers to use this
interface.
> Example scenario: Suppose a company develops a
debugger using JDI. They can use it with the
reference implementation and it will automatically
work with the VMs and platforms Sun supports. It
can also work, for example, with the reference
implementation front-end and a debuggee running
another company's VM that implements JDWP
(which might use or by-pass JVM TI). 33
Using JPDA (Continued..)
• Some debuggers are built on top of lower
layers, JDWP (for example if the front-end is not
written in the Java language) or JVM TI (for
specialized debuggers which need low-level
functionality).

34
Remote
Debugging,
Monitoring,
Profiling,
Managing

35
Topics
• Remote debugging
• Remote profiling
• Remote monitoring & management

36
Remote
Debugging
Needs for Remote Debugging
• Remote debugging is useful when you are
developing an application that runs on a web server
or on a different environment than the computer on
which you are developing the application.
• Debugging remote objects without remote
debugging capability requires inserting a myriad of
System.out.println( ) statements or other logging
code.
> Using logging code for debugging is slow and inefficient,
whereas using NetBeans for remote debugging is much
more powerful.

38
Remote Debugging via NetBeans
• The NetBeans debugger can attach to a Java
process already running in a separate JVM, either
on the same computer or on a remote machine.
• This gives a developer the same power for
debugging remote applications and components
running in Java-based server containers.

39
Remote Debugging via NetBeans
• Attaching to a remote JVM makes it possible to use
breakpoints, conditionals, watches, and other
debugging features with Java applications, applets,
servlets, Enterprise JavaBeans, and RMI or CORBA
server objects.

40
How to build and run the target
application
• Sun's VM implementations require command line
options to load the JDWP agent for debugging for
the target application
• On the computer where the application (debugee) is
located, start the application in debugging mode.
> JDK5 & JDK6: (Using new JVM TI interface)
> -agentlib:jdwp=<sub-options>
> Prior version of JDK5, JDK5 & JDK6
> -Xdebug
> -Xrunjdwp:<sub-options>
41
<sub-options>
• transport
> is a method of communication between a debugger and the virtual
machine that is being debugged.
• address
> when establishing a connection, transport addresses is used to
identify the end-point of the connection.
• server
> if the server property is 'y', the application will listen for a debugger
to attach; otherwise, it will attach to the debugger at the specified
address.
• suspend
> if suspend is 'n', the application will start immediately and will not
wait for a debugger to attach to it. If 'y', the application will be
42
How to build and run the target
application: Example1
• -agentlib:
jdwp=transport=dt_socket,server=y,address=8000
> Listen for a socket connection on port 8000.
> Suspend this VM until attached (connected) by debugger
application (suspend=y by default).
> Once the debugger application connects, it sends a JDWP
command to resume the VM.

43
How to build and run the target
application: Example2
• -agentlib:
jdwp=transport=dt_socket,server=y,address=localhost:
8000,timeout=5000
> Listen for a socket connection on port 8000 on the loopback
address only.
> Terminate if the debugger does not attach within 5 seconds.
> Suspend this VM until attached (connected) by debugger
application (suspend=y by default).
> Once the debugger application connects, it sends a JDWP
command to resume the VM.

44
How to build and run the target
application: Example3
• -agentlib:
jdwp=transport=dt_shmem,server=y,suspend=n
> Choose an available shared memory transport address and
print it to stdout.
> Listen for a shared memory connection at that address.
> Allow the VM to begin executing before the debugger
application attaches.

45
How to build and run the target
application: Example4
• -agentlib:
jdwp=transport=dt_socket,address=myhost:8000
> Attach to a running debugger application via socket on host
myhost at port 8000.
> Suspend this VM until attached (connected) by debugger
application (suspend=y by default).

46
How to build and run the target
application: Example5
• -agentlib:
jdwp=transport=dt_socket,server=y,address=8000,ont
hrow=java.io.IOException,launch=/usr/local/bin/debug
stub
> Wait for an instance of java.io.IOException to be thrown in
this VM.
> Suspend the VM (suspend=y by default).
> Listen for a socket connection on port 8000.
> Execute the following: "/usr/local/bin/debugstub dt_socket
myhost:8000".
> This program can launch a debugger process in a separate
window which will attach to this VM and begin debugging it.47
How to build and run the target
application: Example6
• -agentlib:
jdwp=transport=dt_shmem,server=y,onuncaught=y,lau
nch=d:\bin\debugstub.exe
> Wait for an uncaught exception to be thrown in this VM.
Suspend the VM.
> Select a shared memory transport address and listen for a
connection at that address.
> Execute the following: "d:\bin\debugstub.exe dt_shmem
<address>", where <address> is the selected shared
memory address.
> This program can launch a debugger process in a separate
window which will attach to this VM and begin debugging 48
it.
Demo:
Remote Debugging

http://www.javapassion.com/handsonlabs/javadebugremote
49
Remote
Profiling
Remote Profiling with NetBeans
• You can profile an application that is running on a
remote system such as a web server by attaching
the profiler to the application.
• When you use this mode, the remote application
starts after the profiler is attached.
> This mode enables you to obtain profiling data on the
startup of the target JVM.

51
NetBeans Profiler Remote Pack
• To attach profiler to an application on a remote
system, you need to download and install the
Profiler Remote Pack on the remote system.
• The remote system needs to be started on the
Profiler Remote Pack and configured to support
remote profiling.
• You can download the Profiler Remote Pack from
the Profiler web site:
> htttp://profiler.netbeans.org

52
How to use Attach Wizard
• You use the Attach Wizard to specify the
attachment settings for your project.
• In the Attach Wizard you specify the type of
application and the remote location.
• Based on the details that you provide, the Attach
Wizard provides you with a set of instructions on
how to configure the remote system to support
profiling.

53
How to use Attach Wizard
• After configuring the remote system according to
the instructions, you can attach the profiler to the
remote location.
> You only need to configure the attach mode once.
> The attachment settings are associated with that project.
> You can go through the Attach Wizard at any time to
change any of the attachment settings.

54
Remote
Monitoring &
Management
JMX Architecture
Remote
Manager

Application
JMX
Agent

Manag
es
56
Tools Remote Monitoring and
Management
• JConsole
• VisualVM (with JConsole plug-in)
• jstat

57
Java Trouble-Shooting


Sang Shin

Sun Microsystems, Inc.

javapassion.com

58

Вам также может понравиться