一、Thread Dump介紹

創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、羅定網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城網(wǎng)站定制開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為羅定等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
1.1什么是Thread Dump?
Thread Dump是非常有用的診斷Java應(yīng)用問題的工具。每一個Java虛擬機都有及時生成所有線程在某一點狀態(tài)的thread-dump的能力,雖然各個 Java虛擬機打印的thread dump略有不同,但是大多都提供了當前活動線程的快照,及JVM中所有Java線程的堆棧跟蹤信息,堆棧信息一般包含完整的類名及所執(zhí)行的方法,如果可能的話還有源代碼的行數(shù)。
1.2 Thread Dump特點
1. 能在各種操作系統(tǒng)下使用
2. 能在各種Java應(yīng)用服務(wù)器下使用
3. 可以在生產(chǎn)環(huán)境下使用而不影響系統(tǒng)的性能
4. 可以將問題直接定位到應(yīng)用程序的代碼行上
1.3 Thread Dump 能診斷的問題
1. 查找內(nèi)存泄露,常見的是程序里load大量的數(shù)據(jù)到緩存;
2. 發(fā)現(xiàn)死鎖線程;
1.4如何抓取Thread Dump
一般當服務(wù)器掛起,崩潰或者性能底下時,就需要抓取服務(wù)器的線程堆棧(Thread Dump)用于后續(xù)的分析. 在實際運行中,往往一次 dump的信息,還不足以確認問題。為了反映線程狀態(tài)的動態(tài)變化,需要接連多次做threaddump,每次間隔10-20s,建議至少產(chǎn)生三次 dump信息,如果每次 dump都指向同一個問題,我們才確定問題的典型性。
有很多方式可用于獲取ThreadDump, 下面列出一部分獲取方式:
操作系統(tǒng)命令獲取ThreadDump:
Windows:
1.轉(zhuǎn)向服務(wù)器的標準輸出窗口并按下Control + Break組合鍵, 之后需要將線程堆棧復(fù)制到文件中;
UNIX/ Linux:
首先查找到服務(wù)器的進程號(process id), 然后獲取線程堆棧.
1. ps –ef | grep java
2. kill -3 pid
注意:一定要謹慎, 一步不慎就可能讓服務(wù)器進程被殺死。kill -9 命令會殺死進程。
JVM 自帶的工具獲取線程堆棧:
JDK自帶命令行工具獲取PID,再獲取ThreadDump:
1. jps 或 ps –ef|grepjava (獲取PID)
2. jstack [-l ]pid | tee -a jstack.log (獲取ThreadDump)
二、java線程的狀態(tài)轉(zhuǎn)換介紹(為后續(xù)分析做準備)
2.1 新建狀態(tài)(New)
用new語句創(chuàng)建的線程處于新建狀態(tài),此時它和其他Java對象一樣,僅僅在堆區(qū)中被分配了內(nèi)存。
2.2 就緒狀態(tài)(Runnable)
當一個線程對象創(chuàng)建后,其他線程調(diào)用它的start()方法,該線程就進入就緒狀態(tài),Java虛擬機會為它創(chuàng)建方法調(diào)用棧和程序計數(shù)器。處于這個狀態(tài)的線程位于可運行池中,等待獲得CPU的使用權(quán)。
2.3 運行狀態(tài)(Running)
處于這個狀態(tài)的線程占用CPU,執(zhí)行程序代碼。只有處于就緒狀態(tài)的線程才有機會轉(zhuǎn)到運行狀態(tài)。
2.4 阻塞狀態(tài)(Blocked)
阻塞狀態(tài)是指線程因為某些原因放棄CPU,暫時停止運行。當線程處于阻塞狀態(tài)時,Java虛擬機不會給線程分配CPU。直到線程重新進入就緒狀態(tài),它才有機會轉(zhuǎn)到運行狀態(tài)。
阻塞狀態(tài)可分為以下3種:
1)位于對象等待池中的阻塞狀態(tài)(Blocked in object’s wait pool):當線程處于運行狀態(tài)時,如果執(zhí)行了某個對象的wait()方法,Java虛擬機就會把線程放到這個對象的等待池中,這涉及到“線程通信”的內(nèi)容。
2)位于對象鎖池中的阻塞狀態(tài)(Blocked in object’s lock pool):當線程處于運行狀態(tài)時,試圖獲得某個對象的同步鎖時,如果該對象的同步鎖已經(jīng)被其他線程占用,Java虛擬機就會把這個線程放到這個對象的鎖池中,這涉及到“線程同步”的內(nèi)容。
class-dump-x 提取私有庫里面的隱藏文件
class-dump
This is a command-line utility for examining the Objective-C runtime information stored in Mach-O files. It generates declarations for the classes, categories and protocols. This is the same information provided by using 'otool -ov', but presented as normal Objective-C declarations, so it is much more compact and readable.
Why use class-dump?
It's a great tool for the curious. You can look at the design of closed source applications, frameworks, and bundles. Watch the interfaces evolve between releases. Experiment with private frameworks, or see what private goodies are hiding in the AppKit. Learn about the plugin API lurking in Mail.app.
If you find class-dump useful, you can donate to help support its development. Thanks!
Download
Current version: 3.3.3 (Universal, 64 and 32 bit)
Requires Mac OS X 10.5 or later.
class-dump-3.3.3.dmg
class-dump-3.3.3.tar.gz
class-dump-3.3.3.tar.bz2
Changes - News
Contact
You can email questions and bug reports to me at class-dump@codethecode.com, or nygard at gmail.com.
Usage
class-dump 3.3.3 (64 bit)
Usage: class-dump [options] mach-o-file
where options are:
-a show instance variable offsets
-A show implementation addresses
--arch arch choose a specific architecture from a universal binary (ppc, ppc7400, ppc64, i386, x86_64, etc.)
-C regex only display classes matching regular expression
-f str find string in method name
-H generate header files in current directory, or directory specified with -o
-I sort classes, categories, and protocols by inheritance (overrides -s)
-o dir output directory used for -H
-r recursively expand frameworks and fixed VM shared libraries
-s sort classes and categories by name
-S sort methods by name
-t suppress header in output, for testing
--list-arches list the arches in the file, then exit
--sdk-root specify the SDK root path (full path, or 4.1, 4.0, 3.2, 10.6, 10.5, 3.1.3, 3.1.2, 3.1)
License
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
class-dump -H /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.1.sdk/System/Library/CoreServices/SpringBoard.app/SpringBoard
-o
~/Desktop/SpringBoard
使用:
1 將下載好的 class-dump 放入usr/local/bin 下.
如果 ‘/usr/local/bin’ 不知道在哪里,可以在terminal 下輸入 ‘open -a Finder /usr/local/bin’ 以便打開目錄.
記著 class-dump 要 使用 chmod 修改下執(zhí)行權(quán)限. 例如:在usr/local/bin 對 class-dump 修改,可以這 樣在terminal 切換到 usr/local/bin 目錄下: chmod 777 class-dump .
2 將DumpFrameworks.pl 放入任意目錄下.同樣需要修改執(zhí)行權(quán)限.
3. OK..現(xiàn)在所有的準備工作作好了. 我們在 terminal 的任意目錄下 輸入 : ./DumpFrameworks.pl
等待...
會有一個Heards 文件夾在你的主目錄下. 里面包含了 Frmeworks 和 privateFrameworks 下所有的私有 API,盡情享用吧.!
java dump heap 是分配給實例類和數(shù)組對象運行數(shù)據(jù)區(qū),所有java線程在運行期間共享heap中的數(shù)據(jù)。Java heap dump相當于java應(yīng)用在運行的時候在某個時間點上打了個快照(snapshot)。
有java dump文件生成的方式如下:
1.使用$JAVA_HOME/bin/jmap -dump來觸發(fā),eg:jmap -dump:format=b,file=/home/longhao/heamdump.out
2.使用$JAVA_HOME/bin/jcosole中的MBean,到MBeancom.sun.managementHotSpotDiagnostic操作dumpHeap中,點擊 dumpHeap按鈕。生成的dump文件在java應(yīng)用的根目錄下面。
3.在應(yīng)用啟動時配置相關(guān)的參數(shù) -XX:+HeapDumpOnOutOfMemoryError,當應(yīng)用拋出OutOfMemoryError時生成dump文件。
4.使用hprof。啟動虛擬機加入-Xrunhprof:head=site,會生成java.hprof.txt文件。該配置會導(dǎo)致jvm運行非常的慢,不適合生產(chǎn)環(huán)境。
jmap是java自帶的工具
1. 查看整個JVM內(nèi)存狀態(tài)
jmap -heap [pid]
2. 查看JVM堆中對象詳細占用情況
jmap -histo [pid]
3. 導(dǎo)出整個JVM 中內(nèi)存信息,可以利用其它工具打開dump文件分析,例如jdk自帶的visualvm工具
jmap -dump:file=文件名.dump [pid]
當服務(wù)器掛起,崩潰或者性能底下時,就需要抓取服務(wù)器的線程堆棧(Thread Dump)用于后續(xù)的分析.
Thread dump提供了當前活動的線程的快照.?它提供了JVM中所有Java線程的棧跟蹤信息
有很多方式可用于獲取Thread Dump,?一些是操作系統(tǒng)特定的命令.
操作系統(tǒng)命令獲取ThreadDump:
Windows:
1. 轉(zhuǎn)向服務(wù)器的標準輸出窗口并按下Control + Break組合鍵,?之后需要將線程堆棧復(fù)制到文件中
UNIX/ Linux
首先查找到服務(wù)器的進程號(process id),?然后獲取堆棧.
1. ps –ef??| grep java
2. kill -3 pid
注意一定要謹慎,?一步不慎就可能讓服務(wù)器進程被殺死!
JVM?自帶的工具獲取線程堆棧:
JDK自帶命令行工具獲取PID并做ThreadDump:
1. ?jps
2.jstack pid
使用JVisualVM:
Threads?標簽頁 →ThreadDump按鈕
WebLogic?自帶的獲取?thread dump的工具:
1. webLogic.Admin?工具
a.?打開命令提示符,?通過運行DOMAIN_HOME/bin/setDomain.env設(shè)置相關(guān)類路徑
b.?執(zhí)行下面的命令
java weblogic.Admin -url t3://localhost:7001 -username weblogic -password weblogic1 THREAD_DUMP
注意: Thread Dump?會打印到標準輸出,?如nohup日志或者進程窗口.
2.?使用 Admin Console
a.?登錄?Admin Console ,?點擊對應(yīng)的服務(wù)器
b.?點擊Server?à?Monitoring?àThreads
c.?點擊: Dump Thread Stack?按鈕
3.?使用WLST (WebLogic Scripting Tool)
connect(‘weblogic’,'weblogic1’,’t3://localhost:7001’)
cd(‘Servers’)
cd(‘AdminServer’)
threadDump()
disconnect()
exit()
注意:?線程堆棧將會保存在運行wlst的當前目錄下.
4.?使用utils.ThreadDumper
用法:
C:\bea\wlserver_10.3\server\libjava -cp weblogic.jar utils.ThreadDumper
Broadcast Thread dumps disabled: must specify weblogic.debug.dumpThreadAddr and
weblogic.debug.dumpThreadPort
Exception in thread "main" java.lang.IllegalArgumentException: Port out of range
:-1
at java.net.DatagramPacket.setPort(Unknown Source)
at java.net.DatagramPacket.init(Unknown Source)
at java.net.DatagramPacket.init(Unknown Source)
at utils.ThreadDumper.sendDumpMsg(ThreadDumper.java:124)
at utils.ThreadDumper.main(ThreadDumper.java:145)
5.?如果服務(wù)器是作為Windows服務(wù)的方式運行,?請運行下列命令:
WL_HOME\bin\beasvc -dump -svcname:service-name
其它一些獲取Thread Dump的工具有jrcmd, jrmc(JRockit VM自帶) ,Samurai, JProfiler等,?還可通過JMX編程的方式獲取,?如JDK自帶示例代碼:
$JAVA_HOME\demo\management\FullThreadDump
當前文章:java代碼dump java代碼讀取excel
文章轉(zhuǎn)載:http://www.chinadenli.net/article14/dooipge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站排名、靜態(tài)網(wǎng)站、軟件開發(fā)、微信公眾號、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)