|
The new Java Virtual Machines (VMs) have features to increase performance, and you can use a number of tools to increase application performance or reduce the size of generated class files. Such features and tools improve the performance of your application with little or no change required to your application. Java VM Features The Java® 2 Plaftform release has introduced many performance improvements over previous releases, including faster memory allocation, reduction of class sizes, improved garbage collection, streamlined monitors and a built-in JIT as standard. When using the new Java 2 VM straight out of the box you will see an improvement, however by understanding how the speed-ups work you can tune your application to squeeze out every last bit of performance. Method Inlining The Java 2 release of the Java VM automatically inlines simple methods at runtime. In an un-optimized Java VM, every time a new method is called, a new stack frame is created. The creation of a new stack frame requires additional resources as well as some re-mapping of the stack, the end result is that creating new stack frames incurs a small overhead. Method inlining increases performance by reducing the number of method calls your program makes. The Java VM inlining code inlines methods that return constants or only access internal fields. To take advantage of method inlining you can do one of two things. You can either make a method look attractive to the VM to inline or manually inline a method if it doesn't break your object model. Manual inlining in this context means simply moving the code from a method into the method that is calling it.Automatic VM inlining is illustrated using the following small example:
In the current state the java -Xrunhprof:cpu=times InlineMe
This generates a
If you change the
public void addCount() {
counter=counter+1;
}
And run the profiler again: java -Xrunhprof:cpu=times InlineMe
This time the
Streamlined synchronization Synchronized methods and objects have until Java 2 always incurred an additional performance hit as the mechanism used to implement the locking of this code used a global monitor registry which was only single threaded in some areas such as searching for existing monitors. In the Java 2 release, each thread has a monitor registry and so many of the existing bottlenecks have been removed. If you have previously used other locking mechanisms because of the performance hit with synchronized methods it is now worthwhile re-visiting this code and incorporating the new Java 2 streamlined locks. In the following example which is creating monitors for the synchronized block you can achieve a 40% speed up. Time taken was 14ms using JDK 1.1.7 and only 10ms with Java 2 on a Sun Ultra 1.
Java Hotspot The Java HotSpot VM is Sun Microsystem's next-generation virtual machine implementation. The Java HotSpot VM adheres to the same specification as the Java 2 VM, and runs the same byte codes, but it has been re-engineered to leverage new technologies like adaptive optimization and improved garbage collection models to dramatically improve the speed of the Java VM. Adaptive optimization The Java Hotspot does not include a plug-in JIT compiler but instead compiles and inline methods that appear it has determined as being the most used in the application. This means that on the first pass through the Java bytecodes are interpreted as if you did not have a JIT compiler present. If the code then appears as being a hotspot in your application the hotspot compiler will compiler the bytecodes into native code which is then stored in a cache and inline methods at the same time. See the inlining section for details on the advantages to inlining code. One advantage to selective compilation over a JIT compiler is that the byte compiler can be spend more time generating highly optimized for the areas that would benefit from the optimization most. The compiler can also avoid compiling code that may be best run in interpreted mode. Earlier versions of the Java HotSpot VM were not able to optimize code that was not currently in use. The downside to this is if the application was in a huge busy loop the optimizer would not be able to compile the code for area until the loop had finished. Later Java Hotspot VM releases use on-stack replacement, meaning that code can be compiled into native code even if it is in use by the interpreter. Improved Garbage Collection The garbage collector used in the Java HotSpot VM introduces several improvements over existing garbage collectors. The first is that the garbage collector is termed a fully accurate collector. What this means is that the garbage collector knows exactly what is an object reference and what is just data. The use of direct references to objects on the heap in a Java HotSpot VM instead of using object handles. This increased knowledge means that memory fragmentation can be reduced which results in a more compact memory footprint. The second improvement is in the use of generational copying. Java creates a large number of objects on the heap and often these objects are short lived. By placing newly created objects in a memory bucket, waiting for the bucket to fill up and then only copy the remaining live objects to a new area the block of memory that the bucket used can be freed in one block. This means that the VM does not have to search for a hole to fit each new object in the heap and means that smaller sections of memory need to be manipulated at a time. For older objects the garbage collector makes a sweep through the heap and compacts holes from dead objects directly, removing the need for a free list used in earlier garbage collection algorithms. The third area of improvement is to remove the perception of garbage collection pauses by staggering the compaction of large free object spaces into smaller groups and compacting them incrementally. Fast Thread Synchronization The Java HotSpot VM also improves existing synchronized code. Synchronized methods and code blocks have always had a performance overhead when run in a Java VM. The Java HotSpot implements the monitor entry and exit synchronization points itself and does not depend on the local OS to provide this synchronization. This results in a large speed improvement especially to often heavily synchronized GUI applications. The simplest tool used to increase the performance of your application is the Just-In-Time (JIT) compiler. A JIT is a code generator that converts Java bytecode into native machine code. Java programs invoked with a JIT generally run much faster than when the bytecode is executed by the interpreter. The Java Hotspot VM removes the need for a JIT compiler in most cases however you may still find the JIT compiler being used in earlier releases.
The JIT compiler was first made available
as a performance update in the Java Development Kit
(JDK) 1.1.6 software release and is
now a standard tool invoked whenever you use the
How do JIT Compilers work? JIT compilers are supplied as standalone platform-dependent native libraries. If the JIT Compiler library exists, the Java VM initializes Java Native Interface (JNI) native code hooks to call JIT functions available in that library instead of the equivalent function in the interpreter.
The When the Java VM invokes a Java method, it uses an invoker method as specified in the method block of the loaded class object. The Java VM has several invoker methods, for example, a different invoker is used if the method is synchronized or if it is a native method.
The JIT compiler uses its own invoker. Sun production releases check the method
access bit for value When does the code become JIT compiled code? When a method is called the first time the JIT compiler compiles the method block into native code for this method and stored that in the code block for that method.
Once the code has been compiled the How can I see what the JIT compiler is doing?
The
Notice that inlined methods such as How to use the JIT to your advantage The first thing to remember is that the JIT compiler achieves most of its speed improvements the second time it calls a method. The JIT compiler does compile the whole method instead of interpreting it line by line which can also be a performance gain for when running an application with the JIT enabled. This means that if code is only called once you will not see a significant performance gain. The JIT compiler also ignores class constructors so if possible keep constructor code to a minimum.
The JIT compiler also achieves a minor performance gain by not pre-checking
certain Java boundary conditions such as
You might want to disable the JIT compiler if you are running the Java VM in
remote debug mode, or if you want to see source line numbers instead of the
label javac MyClass.java java -Djava.compiler=NONE MyClass or javac MyClass.java java -Djava.compiler="" MyClass Third-Party Tools Some of the other tools available include those that reduce the size of the generated Java class files. The Java class file contains an area called a constant pool. The constant pool keeps a list of strings and other information for the class file in one place for reference. One of the pieces of information available in the constant pool are the method and field name. The class file refers to a field in the class as a reference to an entry in the constant pool. This means that as long as the references stay the same, it does not matter what the values stored in the constant pool are. This knowledge is exploited by several tools that rewrite the names of the field and methods in the constant pool into shortened names. This technique can reduce the class file by a significant percentage with the benefit that a smaller class file means a shorter network download. [TOP] | ||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||