📄 perf3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Writing Advanced Applications, Chapter 8: Performance Analysis</TITLE>
<META NAME="AUTHOR" CONTENT="Monica Pawlan and Calvin Austin">
<META NAME="KEYWORDS" CONTENT="programming, advanced, Java 2">
<META NAME="OWNER" CONTENT="Editorial/JDC">
<META NAME="revision" CONTENT="@(#)perf3.src 1.19 09/21/99 JDC">
</HEAD>
<!-- Start Body Insert-->
<BODY BGCOLOR="#ffffff">
<!-- End Body Insert-->
<!-- Start PageTop Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR ALIGN="CENTER" VALIGN="TOP">
<TD WIDTH="157" ALIGN="LEFT">
<IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
<A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
<BR>
<IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>
<TD>
<FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD ALIGN="RIGHT">
<IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD WIDTH="100%">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
<TR>
<TD BGCOLOR="#CC9966" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD BGCOLOR="#CC9966">
<IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN="LEFT">
<IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
<TD>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="CENTER">
<A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
<TD VALIGN="CENTER">
<FONT FACE="Helvetica" SIZE="1">
<INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
<TD VALIGN="CENTER">
<INPUT TYPE="image" SRC="/images/search.button.gif"
value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<P>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR VALIGN="TOP">
<TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"
HEIGHT="18" ALT=""></TD>
<TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
<A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR VALIGN="TOP">
<TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<!-- End PageTop Insert -->
<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">
<!-- tab categories -->
<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>
<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>
<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR><TD> </TD></TR>
<!-- End NavBar Insert -->
<!-- START SUB-NAV -->
<TR>
<TD><!-- INSERT SUB-NAV INFO -->
</TD>
</TR>
<!-- END SUB-NAV -->
</TABLE>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>
<!-- Template Version 2.0 -->
<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD VALIGN="TOP">
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="top"></A>
<DIV ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>
<H2>Writing Advanced Applications<BR>
<FONT SIZE="3">Chapter 8 Continued: Performance Analysis</FONT></H2>
<FONT SIZE="-1">[<A HREF="perf2.html"><<BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="perf4.html">NEXT>></A>]</FONT></DIV>
<P>
Another way to improve performance is with performance analysis.
Performance analysis is looking at program execution to pinpoint
where bottlenecks or other performance problems such as memory
leaks might occur. Once you know where potential touble spots are,
you can change your code to remove or reduce their impact.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#profile">Profiling</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#analysis">Analyze a Program</A></FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="#os">Operating System Performance Tools</A></FONT>
</UL>
<HR>
<A NAME="profile"></A>
<H3>Profiling</H3>
The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Virtual Machines (VMs) have had
the ability to provide simple profile reports since Java Development Kit
(JDK<FONT SIZE="-2"><SUP>TM</SUP></FONT>) 1.0.2. However, the information they
provided was limited to a sorted list of method calls a program had called.
<P>
The Java® 2 platform software provides much better profiling capabilities than
previously available and analysis of this generated data has been made easier
by the emergence of a Heap Analysis Tool (HAT). The heap analysis tool,
as its name implies, lets you analyze profile reports of the heap.
The heap is a block of memory the Java VM uses when it is running.
The heap analysis tool lets you generate reports on objects that were
used to run your application. Not only can you get a listing of the
most frequently called methods and the memory used in calling those
methods, but you can also track down memory leaks. Memory leaks can
have a significant impact on performance.
<A NAME="analysis"></A>
<H3>Analyze a Program</H3>
To analyze the <CODE>TableExample3</CODE> program included in the
<CODE>demo/jfc/Table</CODE> directory in the Java 2 platform download,
you need to generate a profile report. The simplest report to generate is
a text profile. To generate a text profile, run the application with the
<CODE>-Xhprof</CODE> parameter. In the final release of the Java 2 platform
software, this option was renamed <CODE>-Xrunhprof</CODE>. To see a list of the
currently available options run the command
</FONT>
<PRE>
java -Xrunhprof:help
Hprof usage: -Xrunhprof[:help]|[<option>=<value>, ...]
</PRE>
<TABLE BORDER=0 CELLSPACING=5 CELLPADDING=0>
<TR>
<TD><CODE>Option Name and Value</CODE></TD>
<TD><CODE>Description</CODE></TD>
<TD><CODE>Default</CODE></TD>
</TR>
<TR>
<TD><CODE>---------------------</CODE></TD>
<TD><CODE>-----------</CODE></TD>
<TD><CODE>-------</CODE></TD>
</TR>
<TR>
<TD><CODE>heap=dump|sites|all</CODE></TD>
<TD><CODE>heap profiling</CODE></TD>
<TD><CODE>all</CODE></TD>
</TR>
<TR>
<TD><CODE>cpu=samples|times|old</CODE></TD>
<TD><CODE>CPU usage</CODE></TD>
<TD><CODE>off</CODE></TD>
</TR>
<TR>
<TD><CODE>monitor=y|n</CODE></TD>
<TD><CODE>monitor contention</CODE></TD>
<TD><CODE>n</CODE></TD>
</TR>
<TR>
<TD><CODE>format=a|b</CODE></TD>
<TD><CODE>ascii or binary output</CODE></TD>
<TD><CODE>a</CODE></TD>
</TR>
<TR>
<TD><CODE>file=<file></CODE></TD>
<TD><CODE>write data to file</CODE></TD>
<TD><CODE>java.hprof(.txt for ascii)</CODE></TD>
</TR>
<TR>
<TD><CODE>net=<host>:<port></CODE></TD>
<TD><CODE>send data over a socket</CODE></TD>
<TD><CODE>write to file</CODE></TD>
</TR>
<TR>
<TD><CODE>depth=<size></CODE></TD>
<TD><CODE>stack trace depth</CODE></TD>
<TD><CODE>4</CODE></TD>
</TR>
<TR>
<TD><CODE>cutoff=<value></CODE></TD>
<TD><CODE>output cutoff point</CODE></TD>
<TD><CODE>0.0001</CODE></TD>
</TR>
<TR>
<TD><CODE>lineno=y|n</CODE></TD>
<TD><CODE>line number in traces</CODE></TD>
<TD><CODE>y</CODE></TD>
</TR>
<TR>
<TD><CODE>thread=y|n</CODE></TD>
<TD><CODE>thread in traces?</CODE></TD>
<TD><CODE>n</CODE></TD>
</TR>
<TR>
<TD><CODE>doe=y|n</CODE></TD>
<TD><CODE>dump on exit?</CODE></TD>
<TD><CODE>y</CODE></TD>
</TR>
</TABLE>
<PRE>
Example: java -Xrunhprof:cpu=samples,file=log.txt,
depth=3 FooClass
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P>
The following invocation creates a text output file that you can view
without the heap analysis tool called <CODE>java.hprof.txt</CODE>
when the program generates a stack trace or exits. A different invocation
is used to create a binary file to use with the heap analysis tool.
</FONT>
<PRE>
java -Xrunhprof TableExample3
d:\jdk12\demo\jfc\Table> java -Xrunhprof TableExample3
Dumping Java heap ... allocation sites ... done.
</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -