Understanding and Tuning The Java Virtual Machine (JVM) For Websphere
Understanding and Tuning The Java Virtual Machine (JVM) For Websphere
You should understand what the IBM J9 JVM is and what new features it provides to the runtime as well as when to use those features.
You will understand how different garbage collection schemes work and how to use them to effect your applications response times
You will have a knowledge and understanding of some critical JVM tuning parameters that effect the runtime performance of the JVM
Lastly you will get to know which debugging tools are available for the JVM as well as understand when and where to use them most effectively.
2
Overview
JVM Basics
Debugging Tools
JVM Basics
Highest Level Overview Java is a Write Once Run Anywhere (WORA) 3rd generation Object Oriented programming language that is executed on a virtual machine The Java Virtual Machine (JVM) runs applications written in Java after the Java code has been compiled to bytecode via the javac process. The JVM in conjunction with other components performs optimization on your compiled Java code to attempt to make it as fast as native code The JVM performs automatic memory management (Garbage Collection) to ensure that system wide memory leaks do not occur and to allow for easier development by allowing developers not to explicitly have to perform memory management. There are multiple implementations of the JVM which all should execute any application written for the Java specification level that JVM was developed for.
4
JVM Basics
Which JVM do I have? The different platforms that WebSphere Application Server runs on have different JVM implementations in some cases
The IBM J9 JVM is the runtime environment on the following Operating Systems or Platforms AIX, Windows, Linux (x86), Linux (PPC), iSeries, zSeries
The Sun JVM is the runtime environment on all platforms running the Solaris Operating System
The HP JVM (which is a very simple Sun JVM port) is the runtime environment on all platforms running the HP-UX Operating System
JVM Basics
The Overall Java Application Stack JVM is built using OO design. Building Block components providing higher level function for simplified end user development and runtime JVMs core runtimes are developed in C or C++ and execute a large majority of function in native code Garbage collector, MMU, JIT, etc IO subroutines, OS calls The J2SE/J2EE APIs all exist at the Java Code layer. Makes data structures available Gives users access to needed function Allow black box interactions with system
6
Java calls
JNI
Pluggable components that dynamically load into the virtual machine Virtual machine JVM Profiler Debugger Realtime Profiler JIT Class loader Interpreter Exception handler Garbage collector
J2SE
Calls to C libraries
Native applications
OS-specific calls
Thread model
Operating system
7
JVM Basics
Class loader basics Uses parent-delegation model to allow additional security as well as allow users to implement their own class loaders Loads classes in a hierarchical manner by delegating to the current class loaders parent recursively What does the class loader do? Loading Obtaining of the byte code from the java class file Verification Validates that the code inside the class file does not attempt to perform any illegal operations in the JVM and that the class file is well formed and not corrupt Preparation Performs allocation and initialization of storage space for static class fields. Also creates method tables and object templates Initialization Executes the classes initialization method and initializes static fields if your java class has them
JVM Basics
JIT Basics The just-in-time compiler (JIT) is not really part of the JVM but is essential for a high performing Java application Java is Write Once Run Anywhere thus it is interpreted by nature and without the JIT could not compete with native code applications
The JIT works by compiling byte code loaded from the class loader when it is access by an application. Due to different platforms having different JITs there is no standard method for when a method is compiled. As your code accesses methods the JIT determines how frequently specific methods are accessed and compiles those touched often quickly to optimize performance
Overview
JVM Basics
Debugging Tools
10
11
12
13
Superior Java application execution performance Just-In-Time (JIT) compiler technology Far improved over JDK 1.4.2 and Suns JIT Maximized performance with minimized runtime overhead multiple optimization levels, multiple recompilations of the same method, many new optimizations dynamic observation of the execution of code via profiling to aggressively improve hot code Interpreter profiling to adapt compilation to compiled methods for block reordering, loop unrolling, etc.
14
Overview
JVM Basics
Debugging Tools
15
Two things to look at in GC: frequency and duration Frequency depends on the heap size and allocation rate Duration depends on the heap size and Java heap footprint
GC algorithm it is critical to understand how it works so that tuning is done more intelligently.
How do you eliminate GC bottlenecks minimize the use of objects by following good programming practices Set your heap size properly, memory-tune your JVM
16
WAS Classes and Objects JVM Runtime classes, extension classes and objects
Java Heap
Fixed Cost
- WebSpere Runtime
- XML Parser - ORB, JCE Security - JMX - Classloaders, etc User-Controlled - Thread pool - Connection pool - Monitoring/Logging - EJB cache (growable?) - Other cache (prepared statement, security, etc )
Application Footprint
WAS Footprint
Native Implementation (e.g. C++) Data Structures, Code, Runtime artifacts, OS Interface
Application Dependent
- number of classes - dynacache - security - resources - HTTP session size - WLM
18
JVM Footprint
Total available memory is there enough memory to satisfy every request for memory ?
Allocation Rate how often does the application requests for memory ?
Object Lifetime how long do these objects stay reserved by the application ?
19
Parallel Collectors two or more threads run at the same time to perform garbage collection Still uses the stop-the-world model but instead of only one GC thread, there are helper threads as well.
Concurrent Collectors collector threads are triggered to run while applications are running Does not use Stop-the-world but threads can be asked to perform garbage collection once in a while
20
Sun/HP JDK 5.0 Platforms Garbage collector always Generational but implementation is chosen based on class of system out of the box 1. 2. 3. Serial Collects objects one at a time in both new and old generations Throughput - Uses a parallel model for collecting objects in the new generation Concurrent Uses parallel collection in the new generation and concurrent in old.
21
Wilderness Thread B
Used Heap Thread Local Heap
Stack
22
System Heap
(JDK 1.4.2)
JVM Heap
Nursery/Young Generation Old Generation Permanent Space
Minor Collection takes place only in the young generation, normally done through direct copying very efficient Major Collection takes place in the old generation and uses the normal mark and sweep algorithm
23
24
Overview
JVM Basics
Debugging Tools
25
26
I want my application to run to completion as quickly as possible. -Xgcpolicy:optthruput My application requires good response time to unpredictable events. -Xgcpolicy:optavgpause My application has a high allocation and death rate. -Xgcpolicy:gencon My application is running on big metal and has high allocation rates on many threads. -Xgcpolicy:subpool
28
Some WebSphere applications perform better with Generational however some applications degrade in performance.
Numbers are approximate and only intended to show a general behaviour seen when running Trade6 compared to SPECjAppServer 29
-Xdisableexplicitgc - turns System.gc() calls into no-ops -Xifa:<on|off|force> - enables the Java code to run on z/OS zAAP processors -Xmaxe / -Xmine - sets the maxium or minimum expansion unit during allocation
30
31
-XX:+DisableExplicitGC - no System.gc() will be executed -XX:TargetSurvivorRatio - sets threshold in survivor space for promotion to kick in -XX:+UseAdaptiveSizePolicy - JVM determines good size for Eden, Survivor Spaces -XX:+UseISM - allows for bigger pages (4MB)
-XX:+UseMPSS (used only for Solaris 9) -XX:+AggressiveHeap -maximizes heap size and algorithms for speed -Xoptgc - optimizes GC in Young Generation (HP only)
32
We need to consider the respective size of the nursery and the tenured space. Two approaches Dynamic Specify the mininum and maxiumum heap size (e.g. Xms512m Xmx1024m) and in the Sun JDK case -XX:+AggressiveHeap The JVM will dynamically size the nursery and tenured space. May not give optimal performance Could be good for low response times. Fixed Be more specific on the nursery and/or tenured space sizes. Recommended approach for performance sensitive, server-side applications.
33
The tenured space must be large enough to hold all persistent data of the application. Too small will cause excessive GC or even out of memory conditions. For a typical WebSphere Application Server application this is ~100400Mb. One way to determine the tenure space size is to look at the amount of free heap exists after each GC in default mode %free heap x Total heap size
Analyse verbosegc to understand how frequently the tenured space gets collected. An optimal generational application will never have a collection in the tenured space. In the lab some WAS applications collect every ~15min.
34
Good WebSphere performance (throughput) requires a reasonable large nursery. A good starting point would be 512 megabyte. Move up or down to determine optimal value. Measure throughput and/or response times Analyse verbosegc to understand frequency and length of scavenges.
35
Tuning
Heap size options
Fix both nursery and tenured space
-XmnAm -XmoBm
Nursery
0 A 0
Tenured
B
36
Tips Tips
Yes Done.
37
Run your application, Run your application, analyze heap usage analyze heap usage and determine the and determine the steady state. steady state. Set your ms to the Set your ms to the steady state. steady state. Make sure your heap Make sure your heap never pages. Monitor never pages. Monitor your paging activities. your paging activities. A rule of thumb is to A rule of thumb is to keep 30% of your keep 30% of your heap free most of heap free most of the time. the time.
Tips Tips
Start
Measure throughput Measure throughput during steady state of during steady state of the benchmark to the benchmark to ensure consistent ensure consistent results results Some tuning Some tuning
No Remove tuning Parameter if Negative effect Is throughput as expected? Analyze Runtime behavior
Yes Done.
38
parameters will effect parameters will effect performance negatively performance negatively as they might be as they might be targeted for an targeted for an application with different application with different runtime characteristics runtime characteristics than your application than your application Add only one tuning Add only one tuning parameter at a time to parameter at a time to measure its impact alone measure its impact alone
Overview
JVM Basics
Debugging Tools
39
Debugging Tools
Garbage Collection Debugging/Analysis Tools (Verbose:GC) Your most indispensable tool directly from the JVM runtime
Pros can give a lot of detailed low-level information for serious debugging, enough for initial investigation readily available and it is free Cons Have to restart your server not suitable for production environments does not give object-level information for further analysis
40
Allocation request details, time it took to stop all mutator threads. Heap occupancy details before GC.
41
Debugging Tools
Garbage Collection Debugging/Analysis Tools Sun/HP JVM verbose:gc output
236.661: [GC 236.661: [DefNew Desired survivor size 61145088 bytes, new threshold 31 (max 31) - age 1: 16817808 bytes, 16817808 total - age 2: 20124840 bytes, 36942648 total : 630283K->36076K(657088K), 0.7287377 secs] 666617K->72411K(2037440K), 0.7289491 secs]
262.697: [GC 262.697: [DefNew Desired survivor size 61145088 bytes, new threshold 31 (max 31) - age 1: 15971824 bytes, 15971824 total - age 2: 3806192 bytes, 19778016 total
- age 3: 18963992 bytes, 38742008 total : 633452K->37833K(657088K), 0.6451270 secs] 669787K->74168K(2037440K), 0.6453326 secs]
286.232: [GC 286.233: [DefNew Desired survivor size 61145088 bytes, new threshold 31 (max 31) - age 1: 17242304 bytes, 17242304 total - age 2: - age 3: 5131296 bytes, 22373600 total 2684464 bytes, 25058064 total
- age 4: 18728192 bytes, 43786256 total : 635209K->42760K(657088K), 0.7164103 secs] 671544K->79094K(2037440K), 0.7166029 secs]
42
Debugging Tools
IBM JDK Debugging/Analysis Tools Thread dumps Available on all JVMs by issuing kill -3 <pid> on the command line where the<pid> is your servers process id In essence a snap shot in time of what your system is executing. Used to debug and find where threads are spending time in your system, or are hung in your system Heap dump Can be enabled to occur with a thread dump by setting the following JVM properties Click on Application Server -> server1 -> Process definition -> custom properties -> Enter Name = IBM_HEAPDUMP Value = true Enter Name = IBM_JAVA_HEAPDUMP_TEXT (this enables generating heapdump in txt format, which can be analyzed using heaproots) Value = true Can be analyzed using HeapRoots at http://www.alphaworks.ibm.com/tech/heaproots
43
Debugging Tools
IBM JDK Debugging/Analysis Tools Class loader runtime diagnostics -verbose:class Gives you information about which classes are loaded -Dibm.cl.verbose=<name> - Gives you specific information about how a class name you define is attempted to me loaded.
Runtime Performance Analysis A variety of third party tools will hook up to the IBM JVM to provide runtime level profiling Jprobe, Jprofiler, etc Hprof if built into the JDK as a profiler but is limited in function however still good for debugging simple unit test case performance issues.
44
http://www-106.ibm.com/developerworks/java/jdk/diagnosis/ Contains all the diagnostic guides for our JVMs PDF on GC and Memory usage
http://java.sun.com/docs/performance Contains a large amount of documentation and tuning for the Sun JVM Reference to all SUN JVM flags as well as an explanation of them
45
Thank you
Any questions ?
46
47
JVM Basics
The high level JVM Building Blocks Part 1
IBM JVM
Core Interface Encapsulates all interactions with user, external programs and operating environment
Execution Management Provides process control and management Threading engine resides here
48
Execution Engine - Provides all methods of executing Java Byte Code both compiled and interpreted.
JVM Basics
The high level JVM Building Blocks Part 2
IBM JVM
Diagnostics Encapsulates all debug and diagnostic services in the JVM Tracing, FFDC, RAS, Debug APIs
Core Interface (CI) Execution Management (XM) Execution Engine (XE) Diagnostics (DG) Class Loader (CL)
Class Loader Provides support for loading and unloading of Java binaries Performs loading, validation, initialization, and implements methods for reflection APIs
49
Data Conversion
JVM Basics
The high level JVM Building Blocks Part 3
IBM JVM
Storage Encompasses all support for storage services the JVM needs Heap management, and allocation strategies
Execution Engine (XE) Diagnostics (DG) Data Conversion Class Loader (CL) Lock (LK)
Storage (ST)
50
HPI A set of well defined functions that provide low level facilities and services in a platform neutral way. This interface is defined by
HPI
Eden
Survivor Space
Survivor Space
Young Generation
Old Generation
Permanent Space
51