掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
所謂“工欲善其事,必先利其器”,好的工具確能起到事半工倍的作用。我用到的最多的就兩個(gè)JConsole 和JProfiler 。JConsole監(jiān)控系統(tǒng)內(nèi)存變化情況,如果有內(nèi)存溢出的話,垃圾回收將會(huì)呈現(xiàn)鋸齒狀。發(fā)現(xiàn)問(wèn)題以后,使用JProfiler,在小壓力(或無(wú)壓力)的情況下監(jiān)控對(duì)象變化,定位內(nèi)存溢出原因。

JProfiler是一款Java的性能監(jiān)控工具??梢圆榭串?dāng)前應(yīng)用的對(duì)象、對(duì)象引用、內(nèi)存、CPU使用情況、線程、線程運(yùn)行情況(阻塞、等待等),同時(shí)可以查找應(yīng)用內(nèi)存使用得熱點(diǎn),即:哪個(gè)對(duì)象占用的內(nèi)存比較多;或者CPU熱點(diǎn),即:哪兒方法占用的較大得CPU資源。我使用的是4.3.2版本,以前試用過(guò)3**版本,不過(guò)那個(gè)bug比較多,容易死,4**版本穩(wěn)定多了。
有了上面那些信息對(duì)于系統(tǒng)的調(diào)優(yōu)會(huì)有很大幫助。這里提供有幾篇文章供參考:獲取、介紹,簡(jiǎn)單入門,使用JProfiler解決實(shí)際問(wèn)題。這幾篇文章基本介紹了常見東西了,下面說(shuō)點(diǎn)心得。
如圖所示,紅筆標(biāo)注部分。如果兩個(gè)都關(guān)閉的話,還是可以跑一定壓力的,同時(shí)還可以監(jiān)控對(duì)象數(shù)量。
Windows客戶端的JProfiler遠(yuǎn)程監(jiān)控Linux上的Tomcat
1.測(cè)試環(huán)境
服務(wù)器:RedHat Linux 3.4.3-9.EL4(內(nèi)核版本 2.6.9-5.EL),Tomcat5.5.20,Sun JDK 1.5.0_09,JProfiler 4.3.2 for linux(安裝包:jprofiler_linux_4_3_2.sh)
客戶端:Windows XP,JProfiler 4.3.2 for windows(安裝包:jprofiler_windows_4_3_2.exe)
2.JProfiler軟件下載地址 http://www.ej-technologies.com/
3.客戶端 JProfiler 安裝 略
4.服務(wù)器端 JProfiler 安裝:
把 jprofiler_linux_4.3.2.sh 上傳到到服務(wù)器,假設(shè)路徑為 /opt/jprofiler
- # cd /opt/jprofiler
- # chmod +x *.sh
- # ./jprofiler_linux_4.3.2.sh -c
按照提示來(lái)安裝,提示都很簡(jiǎn)單,不在多說(shuō)。安裝路徑選擇 /opt/jprofiler4
注意,這里的 -c 意思是用字符方式來(lái)安裝,如果機(jī)器上沒(méi)有 X 則加上該參數(shù).
5.客戶端連接配置
1). 運(yùn)行 JProfiler 。第一次打開會(huì)有向?qū)В雎运?/p>
2). 選擇 Session->Integration Wizard->New Remote Integratation
3). 選擇 On a remote computer;Platform of remote computer 選擇 Linux x86/AMD 64;Next
4). 輸入服務(wù)器 IP ;Next
5). 輸入服務(wù)器上的 jprofiler 的安裝路徑,如 /opt/jprofiler4 ;next
6). 選擇服務(wù)器的 JDK 環(huán)境,這里是:Sun,1.5.0,hotspot;next
7). 輸入端口:這里是默認(rèn)值 8849;next
8). 選擇啟動(dòng)模式:這里選第一種 wait for a connection from the jprofiler GUI;next
9). 這里會(huì)列出需要在服務(wù)器端做的配置:
- Integration type: [Generic application]
- Selected JVM: Sun 1.5.0 (hotspot)
- Startup mode: Wait for JProfiler GUI
(1) Please insert
-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar
into the start command of your remote application right after the java command.
(2) Please add
/opt/jprofiler4/bin/linux-x86
to the environment variable LD_LIBRARY_PATH.
A remote session named Remote application on 192.168.40.15 will be created that connects to a running instance of the remote application that is started with the modified start command.
6.服務(wù)器端的配置
(1)修改系統(tǒng)環(huán)境配置文件 /etc/profile ,增加
- JPROFILER_HOME=/opt/jprofiler4/bin/linux-x86
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JPROFILER_HOME
(2)修改TOMCAT啟動(dòng)文件catalina.sh,添加-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar 內(nèi)容到CATALINA_OPTS中;
“-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar ” 此內(nèi)容由客戶端軟件生成
CATALINA_OPTS="$CATALINA_OPTS -Xms128m -Xmx128m $JPDA_OPTS -agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar"
7.Reboot Linux and startup Tomcat using startup.sh;
The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Protocol version 23
- JProfiler> Using JVMTI
- JProfiler> 32-bit library
- JProfiler> Listening on port: 8849.
- JProfiler> Native library initialized
- JProfiler> Waiting for a connection from the JProfiler GUI
8.啟動(dòng)客戶端軟件
點(diǎn)擊jprofiler菜單 session>start center>Open Session
Available session configurations中列出了剛才配置的連接,選中使用就OK了??!
9.The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Using dynamic instrumentation
- JProfiler> Time measurement: elapsed time
- JProfiler> CPU profiling enabled
- JProfiler> Hotspot compiler enabled
- JProfiler> Starting org/apache/catalina/startup/Bootstrap
10.當(dāng)中斷JProfiler連接時(shí)
The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Disconnected. Waiting for reconnection.
- JProfiler> Listening on port: 8849.
原文鏈接:http://hi.baidu.com/xuwanbest/blog/item/605f9f3335f05e46ac4b5f07.html
【編輯推薦】

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流