一、JVisualVM
JVisualVM 是 Oracle 自帶的基於軟件開發工具包的可視化工具,它可以檢測 Java 虛擬機上運行的應用程序,並提供一個 GUI 應用程序來檢查和分析一些信息。
JVisualVM 可以監測 CPU 和堆內存的使用情況,並分析內存泄漏,線程死鎖等問題。它還可以分析正在運行的應用程序的線程和 JVM 信息。同時它還提供了 JMX 和 MBeans 的支持,並在面板中提供了信息欄、CPU 使用率欄、內存信息欄等實用功能。
JVisualVM 在指定端口號的情況下與遠程主機進行連接。JVisualVM 除了原生的功能外,還支持眾多插件,包括 Memory Leak Detector、Threads Inspector、Synchronization Profiler、Profiling故障等。
下面是用 JVisualVM 分析一個 Java 程序的示例:
<%!DOCTYPE html>
<html>
<head>
</head>
<body>
<%!
public class FibonacciTest {
private static Map fibCache = new HashMap();
public static void main(String[] args) {
Scanner scanner = new Scanner( System.in );
System.out.print( "Please input the index of fibonacci sequence: ");
int index = scanner.nextInt();
long startTime = System.currentTimeMillis();
long result = fibonacciCached(index);
String format = String.format("Fibonacci(%d) = %d", index, result);
System.out.println(format );
long endTime = System.currentTimeMillis();
String timeFormat = String.format("程序耗時:%d 毫秒", endTime - startTime);
System.out.println(timeFormat );
}
private static long fibonacciCached(int index) {
if (index <= 1) {
return index;
}
if (fibCache.containsKey(index)) {
return fibCache.get(index);
}
else {
long fibonacciValue = fibonacciCached(index - 1) + fibonacciCached(index - 2);
fibCache.put(index, fibonacciValue);
return fibonacciValue;
}
}
}
%>
<a href="http://www.visualvm.com/div/media/screencasts/VisualVM.wmv">下載JVisualVM演示視頻</a>
</body>
</html>
二、Flight Recorder
Flight Recorder 是 Java SE Advanced 和 Java SE Suite 的組件之一,它是一種用於生產線程程監控和管理的工具,它可以在生產環境中為您提供精細的實時監控,包括 CPU 使用率、內存使用率、GC 次數等,並提供詳細的性能跟蹤和分析。你可以在任何時候啟動和停止 Flight Recorder,它可以記錄任何數量的數據。
Flight Recorder 運行在 Java 虛擬機內置的 Java Mission Control 里。Java Mission Control 是一套生產管理套件,它包含了除 Flight Recorder 以外的線程程分析工具,例如工具箱工具、線程程分析器、執行器、堆內存瀏覽器等等。
下面是一個使用 Flight Recorder 分析 Java 應用程序的示例:
<%!DOCTYPE html>
<html>
<head>
</head>
<body>
<%!
public class FibonacciTest {
private static Map fibCache = new HashMap();
public static void main(String[] args) {
System.setProperty("com.sun.management.jmxremote.port", "9999");
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
long startTime = System.currentTimeMillis();
int index = 70;
long result = fibonacciCached(index);
long endTime = System.currentTimeMillis();
System.out.println("fib(" + index + ")=" + result + ",耗時(ms)=" + (endTime - startTime));
try {
Thread.sleep(3600 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static long fibonacciCached(int index) {
if (index <= 1) {
return index;
}
if (fibCache.containsKey(index)) {
return fibCache.get(index);
}
else {
long fibonacciValue = fibonacciCached(index - 1) + fibonacciCached(index - 2);
fibCache.put(index, fibonacciValue);
return fibonacciValue;
}
}
}
%>
<a href="http://www.oracle.com/technetwork/java/javaseproducts/flight-recorder-software-1846631.html">了解更多信息</a>
</body>
</html>
三、Java Profiler
Java Profiler 可以為開發人員提供應用程序的低級執行信息,用於分析複雜的應用程序和系統問題。Java Profiler 包括 CPU 分析器、內存分析器、線程程分析器和執行器分析器。
Java Profiler 可以在應用程序運行期間記錄執行信息。這樣可以分析 JNI 調用、線程程競爭、對象分配、Java 反射、網絡和 I/O 阻塞、GC 等問題。
Java Profiler 還提供自定義目標,您可以通過自定義目標追蹤日誌,文件,得到執行的詳細信息,然後將它們導出到 Excel 工作簿或 HTML 格式的報表中。
下面是 Java Profiler 的一個示例。
<%!DOCTYPE html>
<html>
<head>
</head>
<body>
<%!
public class FibonacciTest {
private static Map fibCache = new HashMap();
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int index = 40;
long result = fibonacciCached(index);
long endTime = System.currentTimeMillis();
System.out.println("fib(" + index + ")=" + result + ",耗時(ms)=" + (endTime - startTime));
}
private static long fibonacciCached(int index) {
if (index <= 1) {
return index;
}
if (fibCache.containsKey(index)) {
return fibCache.get(index);
}
else {
long fibonacciValue = fibonacciCached(index - 1) + fibonacciCached(index - 2);
fibCache.put(index, fibonacciValue);
return fibonacciValue;
}
}
}
%>
<a href="https://www.yourkit.com/java/profiler/">了解更多信息</a>
</body>
</html>
四、JProfiler
JProfiler 是一款全功能的基於 Java 的性能分析工具。它支持多種 Java 應用程序和服務器,以及與標準 JMX 客戶端和 JMX AI 進行交互。該工具有助於開發人員診斷性能問題,分析性能瓶頸,並優化應用程序。
JProfiler 可以連續執行多種調試和測量選項,包括調用跟蹤、熱點跟蹤、Heap Walker、內存區域等。在運行多個調試選項後,JProfiler 還可以幫助您識別與 J2EE 規範相衝突的問題。該工具的 UI 顯示呈層級結構,顯示任何可用性能分析選項。
下面是使用 JProfiler 分析 Java 程序的示例:
<%!DOCTYPE html>
<html>
<head>
</head>
<body>
<%!
public class FibonacciTest {
private static Map fibCache = new HashMap();
public static void main(String[] args) throws InterruptedException {
int index = 50;
long startTime = System.currentTimeMillis();
long result = fibonacciCached(index);
long endTime = System.currentTimeMillis();
System.out.println(String.format("FIB(%d) is %d, took %d ms", index, result, endTime - startTime));
Thread.sleep(1000000);
}
private static long fibonacciCached(int index) {
if (index <= 1) {
return index;
}
if (fibCache.containsKey(index)) {
return fibCache.get(index);
}
else {
long fibonacciValue = fibonacciCached(index - 1) + fibonacciCached(index - 2);
fibCache.put(index, fibonacciValue);
return fibonacciValue;
}
}
}
%>
<a href="https://www.ej-technologies.com/products/jprofiler/overview.html">了解更多信息</a>
</body>
</html>
原創文章,作者:NVTFU,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/370734.html