⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perf2.html

📁 jdbc书
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<P>
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.

</FONT>

<PRE>
class MyLock {

  static Integer count=new Integer(5);
  int test=0;

  public void letslock() {
     synchronized(count) {
        test++;
     }
  }
}

public class LockTest {

  public static void main(String args[]) {

     MyLock ml=new MyLock();
     long time = System.currentTimeMillis();

     for(int i=0;i&lt;5000;i++ ) {
      ml.letslock();
     }
     System.out.println("Time taken="+
      (System.currentTimeMillis()-time));
  }
}
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<H4>Java Hotspot</H4>

The Java HotSpot<FONT SIZE="-2"><SUP>TM</SUP></FONT> 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.
<P>
<STRONG>Adaptive optimization</STRONG>
<P>
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.
<P>
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.
<P>
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.
<P>
<STRONG>Improved Garbage Collection</STRONG>
<P>
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.
<P>
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.
<P>
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.
<P>
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.
<P>
<STRONG>Fast Thread Synchronization</STRONG>
<P>
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.
<P>


<A NAME="jit"></A>
<H3>Just-In-Time Compilers</H3>

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.

<P>
The JIT compiler was first made available 
as a performance update in the Java Development Kit 
(JDK<FONT SIZE="-2"><SUP>TM</SUP></FONT>) 1.1.6 software release and is
now a standard tool invoked whenever you use the 
<CODE>java</CODE> interpreter command in the Java 2 platform release. You 
can disable the JIT compiler using the <CODE>-Djava.compiler=NONE</CODE>
option to the Java VM. This is covered in more detail at the end of the JIT
section.

<p>
<H4>How do JIT Compilers work?</H4>
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. 
<p>
The <CODE>java.lang.Compiler</CODE> class is used to load the
native library and start the initialization inside the JIT compiler.
<p>
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. 
<p>
The JIT compiler uses its own invoker. Sun production releases check the method
access bit for value <CODE>ACC_MACHINE_COMPILED</CODE> to notify the 
interpreter that the code for this method has already been compiled and stored
in the loaded class.
<p>

<H4>When does the code become JIT compiled code?</H4>

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.
<p>
Once the code has been compiled the <CODE>ACC_MACHINE_COMPILED</CODE> bit, 
which is used on the Sun platform, is set. 
<p>
<H4>How can I see what the JIT compiler is doing?</H4>

The <CODE>JIT_ARGS</CODE> environment variable allows simple control of the
Sun Solaris JIT compiler. Two useful values are <CODE>trace</CODE> and <CODE>exclude(list)</CODE>. To exclude the methods from the <CODE>InlineMe</CODE> example and show a trace set
<CODE>JIT_ARGS</CODE> as follows:

</FONT>

<PRE>
<FONT SIZE="-1">

<STRONG>Unix:</STRONG>
export JIT_ARGS="trace exclude(InlineMe.addCount 
                               InlineMe.method1)"

$ java InlineMe                                               
Initializing the JIT library ...
DYNAMICALLY COMPILING java/lang/System.getProperty 
                                  mb=0x63e74
DYNAMICALLY COMPILING java/util/Properties.getProperty 
                                  mb=0x6de74
DYNAMICALLY COMPILING java/util/Hashtable.get 
                                  mb=0x714ec
DYNAMICALLY COMPILING java/lang/String.hashCode 
                                  mb=0x44aec
DYNAMICALLY COMPILING java/lang/String.equals 
                                  mb=0x447f8
DYNAMICALLY COMPILING java/lang/String.valueOf 
                                  mb=0x454c4
DYNAMICALLY COMPILING java/lang/String.toString 
                                  mb=0x451d0
DYNAMICALLY COMPILING java/lang/StringBuffer.&lt;init&gt; 
                                  mb=0x7d690
 &lt;&lt;&lt;&lt; Inlined java/lang/String.length (4)
</FONT>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

Notice that inlined methods such as <CODE>String.length</CODE> are exempt. 
The <CODE>String.length</CODE> is also a special method as it is normally
compiled into an internal shortcut bytecode by the Java Interpreter. When 
using the JIT compiler these optimizations provided by the Java Interpreter 
are disabled to enable the JIT compiler to understand which method is 
being called.

<H4>How to use the JIT to your advantage</H4>

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.
<p>
The JIT compiler also achieves a minor performance gain by not pre-checking
certain Java boundary conditions such as <CODE>Null</CODE> pointer 
or array out of bounds exceptions. The only way the JIT compiler knows it
has a null pointer exception is by a signal raised by the operating system.
Because the signal comes from the operating system and not the Java
VM, your program takes a performance hit. To ensure the best performance
when running an application with the JIT, make sure your code is very
clean with no errors like <CODE>Null</CODE> pointer or array out of bounds 
exceptions. 

<p>
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 <CODE>(Compiled Code)</CODE> in your Java stack traces. To disable the 
JIT compiler, supply a blank or invalid name for the name of the JIT compiler
when you invoke the interpreter command. The following examples
show the <CODE>javac</CODE> command to compile the source code
into bytecodes, and two forms of the <CODE>java</CODE> command to invoke the 
interpreter without the JIT compiler.

</FONT>

<PRE>
  javac MyClass.java
  java -Djava.compiler=NONE MyClass
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

or

</FONT>

<PRE>
  javac MyClass.java
  java -Djava.compiler="" MyClass
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<A NAME="3rd"></A>
<H3>Third-Party Tools</H3>

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. 
<p>
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.

<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>

</FONT>
</TD>
</TR>
</TABLE>




<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="TOP">
	
    <P ALIGN=CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    <A HREF="http://java.sun.com/jobs/">Employment</A> - 
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <a href="/siteinfo/faq.html">FAQ</a> |
    <a href="/feedback/index.html">Feedback</a> | 
    <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-99
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -