Beruflich Dokumente
Kultur Dokumente
Startup time
Peak/sustained performance
Perceived performance
Performance as per the user
Scalability
Readiness of application to handle
increasing load without requiring change
in design
When faced with bad performance...
6
Performance Enhancements in JDK 1.3
java.nio package
This new package provides for improved
performance in buffer management, scalable
network and file i/o, character set support and
regular expression matching
Java 2D
The Java 2D enhancements such as new pipeline
architecture, in turn benefits Swing/JFC
performance.
Reflection
Many reflective operations (example:
Class.newInstance() ) have been rewritten for
higher performance 8
Performance Enhancements in JDK 1.4 (Contd)
Networking
Networking functionality in J2SDK 1.4.2 has
improved performance for HTTP Streaming
java.math
New static method probablePrime has been added
for generating prime numbers, thereby providing
an efficient way of generating prime numbers
Hotspot
A new full speed debugging has been added to the
Java Hotspot VM. The improved performance
allows for long running programs to be more easily
debugged, and tested
9
Pre J2SE 5.0 JVM Tuning
10
JVM Tuning – Pre 5.0
Survivor Ratio
Tenured Space
Permanent Space
-Xmx
-XX:NewRatio
virtual
virtual
virtual
-XX:MaxPermSize -XX:SurvivorRatio
Server applications
Select the Server Compiler
Java -server application
Turns on advanced compiler optimizations
Loop peeling and unrolling, inlining,
dead code elimination
18
Java HotSpot VM tuning options
19
Java HotSpot VM tuning options (Contd)
XX:+AggressiveHeap
Performance flag for high-throughput applications
Inspects system resources
Sets various parameters to be optimal for
long-running, memory allocation-intensive jobs
Size of initial heap based on available memory
MIN(½ RAM, RAM–160M)
Automatically sizes generations
Young generation is set to 3/8th overall heap
Turns on throughput collector
WARNING: AggressiveHeap can change
at anytime! 20
Pre J2SE 5.0 Performance Tools
21
GC Portal
Analyses verbose:gc output
Reconstructs application behaviour
Mathematically modelled
Outputs summary details of how the GC
operated during application lifetime
Results can be used to tune VM
Free download
http://developer.java.sun.com/developer/technicalArticles/
Programming/GCPortal/
Jvmstat
Experimental monitoring tool
Currently supports JDK 1.4.1 and above
Instrumentation of GC
Extracts information from JVM in
realtime
Lightweight
Non-intrusive
http://www.sun.com/developers/coolstuff/jvmstat
Jfluid
http://research.sun.com/projects/jfluid
Why is Tuning important?
25
J2SE 5.0 Performance Enhancements
26
J2SE 5.0 Performance Features
“Smart tuning”
Small systems performance optimizations
Client features
Class data sharing
Various JFC/Swing and Java 2D™ APIs
improvements
X86 Optimizations
27
Smart Tuning
JDK 5.0 release “Smart Tuning”
Forget everything you just heard!
Provide good “out-of-the-box” performance
without hand tuning
Examine host machine and configure
Hotspot appropriately
Dynamically adjust Java HotSpot VM
software environment at runtime
Simple tuning options based on application
requirements not JVM software internals
28
How “Smart Tuning” Works
Determine type of machine JVM
software is running on
Server machine
Larger heap
Parallel garbage collector
Server compiler
Client machine
Minimum heap
Serial garbage collector
Client compiler
Adaptive Heap Sizing policy 29
Effects of “Smart Tuning”
“Out-of-the-Box” Performance Composite Summary
225%
200%
175%
150%
125%
100%
75%
50%
25%
0%
JDK 1.4.2 FCS JDK 1.5.0
120%
100%
80%
60%
40%
20%
0%
Smart Tuning Hand Tuning
32
Client Performance in 5.0
J2SE™ Technology Client Performance Improvements
150
125
100
JDK 1.4.2
75
JDK 1.5.0
50
25
0
Startup Footprint Swing
Source: Sun Microsystems
33
Other Performance Enhancements in 5.0
StringBuilder class
Addition of StringBuilder class that works on
an unsynchronized StringBuffer for
performance enhancement.
You should replace all StringBuffer
occurences with StringBuilder
34
Other Performance Enhancements in 5.0 (Contd)
Java 2D technology
Improved accelertion of BufferedImage objects.
Support for hardware accelerated rendering of
OpenGL, and improved text rendering
performance
Image I/O
Performance and memory usage enhancements
when reading and writing JPEG files
35
J2SE 5.0 Performance Tools
36
J2SE 5.0 Platform Performance Tools
JConsole
J2SE monitoring and management console
JMX-compliant grpahical tool for monitoring JVMs –
both remote and local
JPS
JVM Process Status Tool
Lists instrumented Hotspot VM on a target system
JStat
JVM Statistics Monitoring Tool
Attaches to an instrumented Hotspot VM and
collects the logs
Performance stats as per the command line options37
J2SE 5.0 Platform Performance Tools (Contd)
JStatd
Launches an RMI server application that monitors
for the creation and termination of instrumented
Hotspot VMs and provide an interface to allow
remote monitoring tools to attach to JVMs running
on the local system
38
Summary
http://java.sun.com/j2se
http://java.sun.com/docs/hotspot/VMOptions.html
java.sun.com/blueprints/performance
java.sun.com/products/hotspot
research.sun.com/projects/jfluid
developers.sun.com/dev/coolstuff/jvmstat
developer.java.sun.com/developer/
technicalArticles/Programming/GCPortal
41
J2EE Performance
Understand the J2EE stack...
Performance Nugget
Covered in J2SE
Performance part
Out of scope.
However, we will
provide generic
guidelines.
Performance Tuning Guidelines
for J2EE Applications
General Performance Guidelines
Servlets and JavaServer Pages
Avoid using shared modified class variables and hence,
synchronized blocks of code in servlets
Session creation is expensive
Invalidate sessions no longer needed
Use <%page session=false%> directive to prevent
automatic session creation in JSP
Do not store large object graphs in HttpSession to
avoid forced Java serialization
Do not use HttpSession as a cache for transactional
data, instead use “read-only” entity beans, if provided
by your container, to access cached transactional data
General Performance Guidelines
Enterprise Java Beans
Cache EJB references to avoid JNDI lookups
Cache EJB home objects in the servlet's init()
Use setSessionContext() or ejbCreate() to cache bean
specific resources. Also release these resources in
ejbRemove() method.
Remove stateful session beans when they are not needed
to avoid passivation and hence, disk I/O
Use pass-by-reference for remote interfaces if possible
Sun Java System Application Server allows pass-by-
reference semantics in Sun deployment descriptor
General Performance Guidelines
Enterprise Java Beans (Contd.)
Make sure that rmic is generating EJB stubs without
nolocalstubs switch to generate stubs optimized for same-
process clients and servers
This helps performance for applications where EJB
clients (Servlets/JSP) and EJBs are co-located in the
same JVM
The rmic options can be changed using the
administrative tools provided by your container
EJB Pooling and Caching
Pooling - Caching Matrix
Pooling Caching
Stateless Session Beans X
Stateful Session Beans X
Entity beans (BMP/CMP) X X
Message-driven beans X
Pooling
A pool consists of instances of beans of the same type w/o identity
Pooling enhances performance by saving the amount of time spent
during request-cycle for bean class instance creation
Caching
Beans are cached when the number of concurrent users requesting
the services of beans exceeds that of the maximum allowable
number of bean instances
Enterprise Java Beans Pooling
Various settings
Steady pool size – specifies the initial and minimum number of
beans that must be maintained in a pool
Pool resize quantity – specifies the number of beans to be
created or deleted when the pool is being resized by the server
Maximum pool size – specifies the maximum pool size. Can
specify 0 to denote unbounded pool. CAUTION – JVM heap
may get filled with objects in the pool
Pool idle timeout – specifies the maximum time that a bean is
allowed to remain idle in the pool after which the bean is
destroyed
Enterprise Java Beans Pooling
Pool tuning tips
Ensure that initial and maximum values of pool size are
representative of normal and peak loads
Setting a very large initial or maximum value for pool size
might lead to ineffective use of system resources for
applications that do not have much concurrent load
Leading to huge garbage collection pauses
Setting a small initial or maximum value compared to typical
loads is going to cause a lot of object creation and object
destruction
Enterprise Java Beans Caching
Various settings
Cache resize quantity – specify the number of beans to be
created or deleted when the cache is being serviced
Maximum cache size – specifies the maximum number of beans
in a cache. Value of 0 denotes unbounded cache.
Cache idle timeout – specifies the maximum time that a stateful
session bean or entity bean is allowed to be idle in the cache,
after which the bean will be passivated
Removal timeout
This setting is applicable only to stateful session beans
It specifies the maximum time period for which the stateful
session bean is allowed to remain passivated after which, its
state will be removed from persistent store.
Enterprise Java Beans Caching
Various settings (Contd.)
The client will not be able to access bean after its state is
removed
Victim selection policy
This setting is applicable only to stateful session beans
It specifies the algorithm for selecting victims to be removed
from the stateful session bean cache
Some of the popular algorithms – NRU, LRU, FIFO
Enterprise Java Beans Caching
Tuning tips
It is good to be able to use beans from cache as much as
possible, theoretically, because:
A bean in cache represents ready state i.e. The bean has
identity associated with it
Beans moving out of cache have to be passivated or
destroyed
Once passivated the bean has to be activated to come back
into the cache
Therefore, any request serviced using these cached beans
avoids overhead of creation, setting identity, and potentially,
activation.
Enterprise Java Beans Caching
Tuning tips (Contd.)
However, there are downsides to caching extensively
Memory consumed by all beans affect the heap available in
the VM
Increasing objects and memory taken by cache means
longer, and perhaps more frequent, full GC
Application server might run out of memory
At the same time, maintaining a small cache will lead to lot of
passivation and activation of bean instances and hence,
serialization and de-serialization, thereby straining CPU cycles
and disk I/O
Note – A periodic cleaner will remove all beans in the cache
that have reached cache idle timeout period
Entity Bean Tuning Guidelines
General Guidelines
Provide a bigger cache for beans that are used more as compared
to those that are used less. For example, an Order entity bean that
is accessed comparatively lesser as compared to an Item entity
bean
Entity bean cache and pool sizes should be larger as compared to
session beans (stateful/stateless) taking into consideration finder
methods that return large number of entity stubs
Use lazy loading if you do not need all data represented by your
entity the first time you access it to optimize memory as well as
network bandwidth consumption
To code BMP for lazy loading, put relevant SQLs in the
appropriate getXXX() methods.
For CMP, most of the containers support lazy loading
Entity Bean Tuning Guidelines
General Guidelines (Contd.)
Lazy Loading Caveat
If your client ends up accessing data that is loaded lazily
frequently then you thought, your bean might end up making
multiple network round-trips
Closely monitor the data that your clients end up accessing
frequently and load that data while first loading the bean
Mark entity beans as read-only or read-mostly
Read-only – if your entity beans are never used for changing
database data, then you should consider marking them read-
only to avoid unnecessary ejbStore() calls at the end of each
transaction. This works great for data that never changes!
Entity Bean Tuning Guidelines
General Guidelines (Contd.)
Read-mostly – if your data changes infrequently, then mark
corresponding entity beans to be read-mostly. This will ensure
that container calls ejbLoad() on the bean after a specified
refresh timeout period
Entity Bean Tuning Guidelines
Commit Options
Tune commit options for entity beans (most application servers
support commit options B and C)
Commit option controls the action taken by the container
when the transaction in which bean participates is complete
Option B – when a transaction completes, the bean is kept in the
cache with its identity
This means the next invocation for the same primary key can
be serviced by the same instance from the cache (of course,
after calling ejbLoad() to synchronize state with the
database)
Entity Bean Tuning Guidelines
Commit Options (Contd.)
Option C – when a transaction completes, the bean's ejbPassivate()
is called and the bean is disassociated from its identity (primary
key). This bean is returned to the free pool.
This means that next invocation for the same primary key will
have to grab a bean instance from free pool, set the primary key
on this instance, and then call ejbActivate() on the instance.
Again, bean's ejbLoad() will be called to sync up with the
database
Entity Bean Tuning Guidelines
Difference between Commit Options
It is clear that option B avoids ejbActivate()/ejbPassivate() calls,
and hence, in most cases will offer better performance
However, if beans in the cache are rarely used, then commit option
C can perform better
Since in option C, container puts back the bean into the free
pool, instead of putting it in a cache where hit ratio is low
Entity Bean Tuning Guidelines
How to choose between Commit Options B and C?
First take a look at cache-hits value using your EJB server
monitoring tools
If the cache-hits are high, compared to cache-misses, then
option B will work great
Otherwise, use commit option C
Note – You'll still have to tune cache initial, maximum and
resize quantities to achieve optimized performance
For instance you should maintain a larger pool of entity
beans when using commit option C, as compared to cache,
since pool is used more
Message-driven Bean Tuning Guidelines
Reproduce Issue
Monitor Application
Performance not OK
Performance OK
Send the code into production
Monitoring J2EE Stack
Monitoring is the first step towards performance tuning
• jvmstat tools
• Command line options
– -verbose:[gc|class|jni]
– -Xprof
– -XX options
• Serviceability Agent
• JSR 174 interfaces
• Jconsole
Monitoring J2EE Stack
Monitoring is the first step towards performance tuning
• JMX interfaces
– SNMP, CIM/WBEM
• Bytecode instrumentation
– Various commercial tools
• JVMPI, JVMTI, JVMDI tools
– Commercial and free
Monitoring J2EE Stack
Monitoring is the first step towards performance tuning
• Custom instrumentation
– Tracking method enter/exit
• Custom logging
– Special performance events
• Custom managed beans
– Remotely monitor
Root cause analysis
Resolution
Increase JVM machine heap size
Use ParallelGC algorithm
Root cause analysis
High System CPU Utilization – Symptoms
Symptoms
High system time and low idle time
High user or wait time is another issue
System time of about 10 is usually a problem
Sometimes for database systems, system time could be as
high as 40
Use processor/CPU monitoring tools such as vmstat,
mpstat, top, prstat to find out about the symptoms
procs cpu
r b us sy id wa
1 1 40 5 51 3
2 0 32 57 8 3
0 0 36 58 4 2
0 0 30 45 2 3
Root cause analysis
High System CPU Utilization – Causes
What it means
Lots of calls into the operating system
Lots of I/O, socket creation, timestamping activities
Look further into
Application server thread dumps
Application server profiles
truss/trace of systems calls
Possible causes
Mutex/Lock contention
Unbuffered I/O
Root cause analysis
High System CPU Utilization – Resolution
truss output (Illustration)
1742: read(5, "CF", 1) = 1
1742: read(5, "9F", 1) = 1
1742: read(5, "06", 1) = 1
1742: read(5, "8F", 1) = 1
1742: read(5, "A8", 1) = 1
1742: read(5, " q", 1) = 1
1742: read(5, " Z", 1) = 1
1742: read(5, " *", 1) = 1
1742: read(5, " (", 1) = 1
1742: read(5, "B6", 1) = 1
Resolution
Introduce buffered I/O
Root cause analysis
High Disk Utilization – Symptoms
Symptoms
Especially when application server logs to a separate device
(/tmp usually), any other unusual disk activity should alarm
High database service times (100s of ms)
High %b (above 30)
High Wait CPU % time
Use iostat or similar such disk monitoring tool
server.applications.MyApp.my_jar.myBean.
bean-cache.numpassivationsuccess-count = 54581
Resolution
Increase stateful session bean cache size
Tuning J2EE Cluster Performance
Cluster performance factors
Latencies inherent in any server (regardless of
clustered or not)
Request processing time
Data retrieval (from EIS/RDBMS) time
Rendering/displaying data
Additional latencies due to clustering
Size, frequency, and efficiency of data transfer
Other costs of making data available across the cluster
Size of the application server cluster – sometimes the
cost of clustering overtakes the benefit
Additional tiers – load balancers and HA requirements:
How highly available stores are managed.
Cluster performance factors
Size, frequency and efficiency of data transfer
Size
How much of the data needs to be sent (10 M vs. 10 K)
Frequency
How frequently does the data need to be transferred (10
ms vs. 10 secs)
How efficient is the data transfer protocol
Memory replication protocol
JDBC to database
JMS messaging
Cluster performance factors
Size, frequency and efficiency of data transfer
Size
Keep data size/volume low – write only what matters
Avoid transferring complex object graphs
Frequency
How much latency can you take? Choose data transfer
frequency accordingly.
How efficient is the data transfer protocol
Memory replication protocol: tune the memory in terms
of replication efficiency
JDBC to database: tune the JDBC connections
JMS messaging: understand the cost involved in using
JMS brokers
Cluster performance factors
Other costs of making data available cross-cluster
http://java.sun.com/performance
Mastering Enterprise JavaBeans
book for performance and design
best practices
http://javaperformancetuning.com
Rima Patel Sriganesh
Member of Technical Staff/Technology Evangelist
rima.patel@sun.com