首頁  >  文章  >  Java  >  java中Thread Dump怎麼使用

java中Thread Dump怎麼使用

PHPz
PHPz轉載
2023-05-01 20:19:051548瀏覽

說明

1、ThreadDump用於診斷Java應用程式的問題,可用來尋找記憶體洩漏,發現死鎖執行緒等。

2、此系統可取得執行緒、執行緒運行狀態、標識、呼叫等訊息,包括完整的類別名稱、執行方法、原始碼所在行數等。

特點

可用於各種作業系統;

可以在各種Java應用程式伺服器下使用;

可以在在不影響系統效能的情況下使用;

問題可以直接定位在應用程式的程式碼行上。

實例

public class JStack {
            public static void main(String[] args) throws Exception {
               ...
                    String pid = args[optionCount];
                    String params[];
                    if (locks) {
                        params = new String[] { "-l" };
                    } else {
                        params = new String[0];
                    }
                    runThreadDump(pid, params);
                ...
            }
            // Attach to pid and perform a thread dump
            private static void runThreadDump(String pid, String args[])
                    throws Exception {
                VirtualMachine vm = null;
                try {
                    vm = VirtualMachine.attach(pid);
                } catch (Exception x) {
                   ...
                }
                // Cast to HotSpotVirtualMachine as this is implementation specific
                // method.
                InputStream in = ((HotSpotVirtualMachine) vm)
                        .remoteDataDump((Object[]) args);
                // read to EOF and just print output
                byte b[] = new byte[256];
                int n;
                do {
                    n = in.read(b);
                    if (n > 0) {
                        String s = new String(b, 0, n, "UTF-8");
                        System.out.print(s);
                    }
                } while (n > 0);
                in.close();
                vm.detach();
            }

以上是java中Thread Dump怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除