Performance of Java Application - Part 1
Performance of Java Application - Part 1
Introduction
Why is it slow ?
•The virtual machine layer that abstracts Java away from the underlying
hardware increase the
overhead.
• These overheads can cause Java application to run slower that an equivalent
application written in a lower-level language.
• Java's advantages . platform-independence, memory management, powerful
exception
checking, built-in multi-threading, dynamic resource loading and security checks
. add costs.
Performance tuning is similar to playing a strategy game (but happily, you are
usually paid to do it!). Your target is to get a better score (lower time) than the
last score after each attempt. You are playing with, not against, the computer,
the programmer, the design and architecture, the compiler, and the flow of
control. Your opponents are time, competing applications, budgetary restrictions,
etc. (You can complete this list better than I can for your particular situation.)
There is no such switch, but very simple techniques sometimes provide the
equivalent. Techniques include switching compilers, turning on optimizations,
using a different runtime VM, finding two or three bottlenecks in the code or
architecture that have simple fixes, and so on. These techniques provide huge
improvements to applications, sometimes a 20-fold speedup. Order-of-
magnitude speedups are typical for the first round of performance tuning.
• System memory
• Disk (and network) input/output (I/O)
A Tuning Strategy
• 1. Identify the main bottlenecks (look for about the top five
bottlenecks)
• 2. Choose the quickest and easiest one to fix, and address it.
Advantage :
Identify bottleneck
8. Test that the alteration improves performance, and measure the improvement
9. Repeat from Step 1.
Perceived Performance
• The users has a particular view of performance that allows you to cut some
corners.
• Rules :
- if application is unresponsive for more than 2 sec, it is seem as slow.
- Users are not aware of response time improvements
•Threading : ensuring that your application remains reponsive to the user, even
while it is executing some other function.
• Caching : the caching technics help you to speed the data access. The read-
ahead algorithme use in disk hardware is fast when you reading forward through
a file.
Starting to tune
• User agreements : you should agree with your users what the performance of
the applications isexpected to be : response times, systemwide throughput,
max number of users, data, ...
• Setting benchmarks : these are precise specifications stating what part of code
needs to run in what amount of time. How much faster and in which parts, and
for how much effort ?
Taking Measurements
• Each run of your benchmarks needs to be under conditions that are identical as
possible.
• The benchmark should be run multiples times, and the full list of results
retained, not just the average and deviation.
• Run a initial benchmark to specify how far you need to go and highlight how
much you have achieved when you finish tuning.
What to measure ?
• Main : the wall-clock time (System.currentTimeMillis())
• Memory size
• Disk throughput
• Network traffic, throughput, and latency
Java doesn't provide mechanisms for measuring theses values directly.
Profiling Tools
• The OS interfere with the results by the allocation of different priorities to the
process.
Garbage Collection
• Some of the commercial profilers provide statistics showing what the garbage
collector is doing.
• The printout includes explicit synchronous calls to the garbage collector and
asynchronous executions of the garbage collector when free memory available
gets low.
• The important items that all -verbosegc output are
- the size of the heap after garbage collection
- the time taken to run the garbage collection
- the number of bytes reclaimed by the garbage collection.
• Interesting value :
- Cost of GC to your application (percentage)
- Cost of the GC in the application's processing time
GC Viewer
Method Calls
• Show where the bottlenecks in your code are and helping you to decide where
to target your efforts.
• Most method profilers work by sampling the call stack at regular intervals and
recording the methods on the stack.
• The JDK comes with a minimal profiler, obtain by using the -Xrunhprof option
(depends on theJDK). This option produces a profile data file (java.hprof.txt).
Object creation
Tools
• X86 Optimizations
Ø Larger heap
Ø Parallel garbage collector
Ø Server compiler
• Client machine
Ø Minimum heap
Ø Serial garbage collector
Ø Client compiler
• Adaptive Heap Sizing policy
•StringBuilder class
Ø Addition of StringBuilder class that works on an unsynchronized StringBuffer
for performance enhancement.
Ø You should replace all StringBuffer occurences with StringBuilder
•Java 2D technology
• Image I/O
•Jconsole
Ø J2SE monitoring and management console
Ø JMX-compliant graphical tool for monitoring JVMs - both remote and local
• JPS
• Jstat
Ø JVM Statistics Monitoring Tool
Ø Attaches to an instrumented Hotspot VM and collects the logs
Ø Performance stats as per the command line options
•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