星期五, 3月 22, 2013
Java效能監控與除錯工具簡介
Java效能監控與除錯工具簡介
I.Java SE
1.HPROF profiler a.Heap Profiler(HPROF)工具是一個簡單的監視程序代理隨著JDK發行。 它是一個動態鏈接庫,透過Java虛擬機工具接口(JVM TI)與Java VM對接。 HPROF實際上是通過JVM命令列選項,在JVM啟動時動態加載的 b.指令語法: java -agentlib:hprof[=options] ToBeProfiledClass c.可以呈現JVM的 CPU使用率 記憶體堆疊分配統計 監控競爭基本資料 報告完整的記憶體堆疊傾印 報告所有監控對象與執行緒狀態 d.根據不同分析要求的類型,HPROF指示虛擬機發送相關的JVM TI事件和處理事件的數
據去進行分析信息。
2.JConsole utility
a.一個 java GUI 監視工具,可以以圖表化的形式顯示各種數據。 並可通過遠程
連接監視遠程的服務器 VM 。
b.JBoss AS組態供遠程jconsole監控
請參考 : http://blog.csdn.net/lengyuhong/article/details/6200355
jvm執行參數
-Djboss.platform.mbeanserver
-Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl
-Djava.rmi.server.hostname=127.0.0.1
-Dcom.sun.management.jmxremote.port=8950
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
3.jhat utility
a.用於對 JAVA heap 進行離線分析的工具,它可以對不同虛擬機中導出的 heap
信息文件進行分析。
b.參考指令
jhat -J-Xmx512m d:\temp\jboss_heapdump.bin
上述指令啟動後,可開啟瀏覽器鍵入網址 http://localhost:7000 檢視收集到之
相關數據
c.有哪些資訊可觀察?
All classes including platform
顯示出堆疊中所包含的所有類別
Show all members of the rootset
根集合可以引用到的成員對象
Show instance counts for all classes (including platform)
顯示所有類別實例數量(包含平台)
Show instance counts for all classes (excluding platform)
顯示所有類別實例數量(不包含平台)
Show heap hitogram
顯示堆疊實例分佈表
Show finalizer summary
Execute Object Query Language (OQL) query
執行對象查詢語句
4.jinfo utility
a.輸出並修改運行時的java 進程的 opts 。 用處比較簡單, 用於 輸出 JAVA 系統
參數 及命令行參數 。
b.參考指令
jinfo -opt pid
jinfo -flag MaxPermSize 2788
5.jmap utility
a.打印出某個java進程(使用 pid)內存內的所有 ' 對象 ' 的狀況(如:產生那些對
象,及其數量)。
b.參考指令
jmap -J-d64 -dump:format=b,file=jboss_heapdump.bin 6109
6.jps utility
a.用來查看 基於HotSpot JVM 裡面所有進程的具體狀態 , 包括進程 ID ,進程啟
動的路徑等等。
與 unix 上的 ps 類似,用來顯示本地 有權限 的java 進程,可以查看本地運行著
幾個 java 程序,並顯示他們的進程號。 使用jps 時,不需要傳遞進程號做為參
數。
jps 也可以顯示遠程系統上的 JAVA 進程,這需要遠程服務上開啟了 jstat 服務
b.參考指令
jps
7.jstack utility
a.用於打印出給定的 java 進程 ID 或 core file 或遠程調試服務的 Java 堆棧信息。
b.參考指令
jstack 4075 >> /home/ubuntu/jstack_threaddumps.log
8.jstat utility
a.是 JDK 自帶的一個輕量級小工具。 全稱 “Java Virtual Machine statistics monitoring
tool”主要利用JVM內建的指令對Java應用程序的資源和性能進行實時的命令行
的監控,包括了對Heap size和垃圾回收狀況的監控。
b.重要參考文章
http://blog.csdn.net/fenglibing/article/details/6321453
c.參考指令
jstat -gcutil vmid interval count >> /home/ubuntu/jstat_20130321.log
jstat -gcutil 7384 1s 120 >> d:/jstat_20130321.log
9.jstatd daemon
a.是一個RMI Server應用程序,提供了對JVM的創建和結束監視,也為遠程監視
工具提供了一個可以attach的接口
b.參考指令
製作安全政策檔 : tools.policy
----------------------------------------------------------------------------
grant codebase "file:${java.home}/../lib/tools.jar" {
permission java.security.AllPermission;
};
----------------------------------------------------------------------------
執行 : jstatd -p 10990 -J-Djava.security.policy=tools.policy&
II.Eclipse
1.Memory Analyzer(MAT)
a.一個基於Eclipse的內存分析工具,是一個快速、功能豐富的JAVA heap分析工
具,它可以幫助我們查找內存洩漏和減少內存消耗。
當服務器應用佔用了過多內存的時候,會遇到OutOfMemoryError。 如何快速定
位問題呢? Eclipse MAT的出現使這個問題變得非常簡單。 它能夠離線分析
dump的文件數據。
b.有哪些資訊可觀察?
記憶體內存洩露報表
自動檢查可能存在內存洩露的對象,通過報表展示存活
的對像以及為什麼他們沒有被垃圾收集
對象報表
對可疑對象的分析,如字符串是否定義重了,空的collection、
finalizer以及弱引用等
2.GCHisto工具
a.取得GC監控記錄檔,需對監測對象執行環境之jvm設定Java
b.命令列參數
-Xloggc:D:/gc.log -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails
c.源碼庫
http://java.net/projects/gchisto/sources
JConsole with JBoss AS 5.1.0 GA
環境:
JDK7+/Eclipse Indigo SR2
問題:
若專案中有客製化之MBean服務的話,JBoss5要開啟jmx遠端監聽埠號讓JConsole監控時,會導致客製化之MBean模組衝突而無法啟動
步驟:
1.可參考https://community.jboss.org/thread/153594
2.更新$JBOSS_HOME/bin/run.jar檔,請先從$JBOSS_HOME/lib/jboss-system-jmx.jar檔中,分
別取出 org.jboss.system.server.jmx.LazyMBeanServer.class 與
org.jboss.system.server.jmx.MBeanServerBuilderImpl.class 這兩個類別檔,將它們拷貝至run.jar
檔內
3.再參考http://www.blogjava.net/shapos/archive/2010/10/10/334177.html?opt=admin
4.設定java啟動參數:
-Djboss.platform.mbeanserver
-Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl
-Djava.rmi.server.hostname=127.0.0.1
-Dcom.sun.management.jmxremote.port=8950
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Xloggc:D:/gc.log -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails
ps. 上面java組態設定中,紅色部分為解決MBean衝突、藍色部分才是為建立與JConsole遠端
監控之介面,而最後一行黑色部分是設定為jvm輸出GC活動記錄而設
5.啟動JConsole開始監控JBoss
星期六, 7月 23, 2011
Java 5以上有關泛型集合拷貝操作的問題
- 緣起:
昨天辦公室裡同事在問我有關JDK5以上有關泛型集合拷貝操作的問題,他的需求是希望能保留原始集合內所有物件之內容,因此先將原始的集合先行拷貝一份至記憶體內,然後針對這份拷貝出來的集合在行修改操作,這樣一來既可以保留原來的那一份,又可以毫無顧慮地對拷貝出來的這份恣意地修改。
- 問題
但是問題是當他使用集合的拷貝方法時,如:ArrayList.clone()時,Java似乎並沒有真的在記憶體內,重新拷貝新的集合到另一快新的記憶體區塊內。導致他怎麼操作修改新的那份集合,都會連帶異動到原始集合內的資料。
- 解法
簡單地說,以Dog這個類別為例,就是要實作Cloneable這個介面的clone()方法即可。
- Dog.java
public class Dog implements Cloneable {private String kind;private String color;public Dog(String kind, String color) {super();this.kind = kind;this.color = color;}public String getKind() {return kind;}public void setKind(String kind) {this.kind = kind;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}protected Object clone() throws CloneNotSupportedException {return super.clone();}}
- ListCloneTest.java
import java.util.ArrayList;public class ListCloneTest {/*** @param args* @throws CloneNotSupportedException*/public static void main(String[] args) throws CloneNotSupportedException {// TODO Auto-generated method stubArrayListoldDogs = new ArrayList (); ArrayListnewDogs; oldDogs.add(new Dog("a", "white"));oldDogs.add(new Dog("b", "block"));newDogs = new ArrayList(oldDogs.size()); for(Dog dog : oldDogs){newDogs.add((Dog) dog.clone());}newDogs.get(0).setColor("yellow");newDogs.get(1).setKind("d");System.out.println("Old Dogs#1 : kink="+oldDogs.get(0).getKind()+" color="+oldDogs.get(0).getColor());System.out.println("New Dogs#1 : kink="+newDogs.get(0).getKind()+" color="+newDogs.get(0).getColor());System.out.println("Old Dogs#2 : kink="+oldDogs.get(1).getKind()+" color="+oldDogs.get(1).getColor());System.out.println("New Dogs#2 : kink="+newDogs.get(1).getKind()+" color="+newDogs.get(1).getColor());}}
- 輸出結果:
Old Dogs#1 : kink=a color=whiteNew Dogs#1 : kink=a color=yellowOld Dogs#2 : kink=b color=blockNew Dogs#2 : kink=d color=block
星期四, 4月 14, 2011
Oracle 10g Install on CetOS5.6
Oracle 10g Install on CetOS5.6
1 | Get Oracle 10g Installing File | |||||||
a. | Download the Complete Files | |||||||
Filename : 10201_database_linux_x86_64.cpio.gz | ||||||||
b. | Unzip the file | |||||||
$gunzip 10201_database_linux_x86_64.cpio.gz | ||||||||
c. | Extract the file | |||||||
$cpio -idmv < 10201_database_linux_x86_64.cpio | ||||||||
2 | Installation Guide | |||||||
a. | Preinstallation Tasks | |||||||
i. | Install JDK | |||||||
$vi /etc/profile JAVA_HOME=/usr/local/jdk1.6.0_24 export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:/usr/local/jdk1.6.0_24/lib:/usr/local/jdk1.6.0_24/jre/lib | ||||||||
ii. | Checking the Software Requirements | |||||||
$uname -r $rpm -qa | ||||||||
iii. | Checking the Network Setup | |||||||
1 | Configuring Name Resolution | |||||||
$vi /etc/sysconfig/network HOSTNAME=myhost.ora.mycompany.com | ||||||||
$vi /etc/hosts 10.10.0.102 myhost.ora.mycompany.com myhost | ||||||||
2 | Setting the ORACLE_HOSTNAME Environment Variable | |||||||
$vi /etc/profile ORACLE_HOSTNAME=myhost.ora.mycompany.com export ORACLE_HOSTNAME | ||||||||
iv. | Creating Required Operating System Groups and Users | |||||||
1 | Need to create several operating system groups and users | |||||||
The OSDBA group : dba The OSOPER group : oper The Oracle Inventory group : oinstall The Oracle software owner user : oracle | ||||||||
2 | Creating the Oracle Inventory Group | |||||||
$/usr/sbin/groupadd oinstall $/usr/sbin/groupadd dba $/usr/sbin/groupadd oper $/usr/sbin/useradd -g oinstall -G dba,oper oracle $/usr/sbin/usermod -g oinstall -G dba,oper oracle $passwd oracle ${password} $id nobody | ||||||||
3 | Configuring Kernel Parameters | |||||||
$/sbin/sysctl -a | grep sem $/sbin/sysctl -a | grep shm $/sbin/sysctl -a | grep file-max $/sbin/sysctl -a | grep ip_local_port_range $/sbin/sysctl -a | grep rmem_default $/sbin/sysctl -a | grep rmem_max $/sbin/sysctl -a | grep wmem_default $/sbin/sysctl -a | grep wmem_max $vi /etc/sysctl.conf kernel.shmall = 2097152 kernel.shmmax = 2147483648 kernel.shmmni = 4096 kernel.sem = 250 32000 100 128 fs.file-max = 65536 net.ipv4.ip_local_port_range = 1024 65000 net.core.rmem_default = 262144 net.core.rmem_max = 262144 net.core.wmem_default = 262144 net.core.wmem_max = 262144 Restart the system | ||||||||
4 | Setting Shell Limits for the oracle User | |||||||
$vi /etc/security/limits.conf oracle soft nproc 2047 oracle hard nproc 16384 oracle soft nofile 1024 oracle hard nofile 65536 $vi /etc/pam.d/login session required pam_limits.so $vi /etc/profile if [ $USER = "oracle" ]; then if [ $SHELL = "/bin/ksh" ]; then ulimit -p 16384 ulimit -n 65536 else ulimit -u 16384 -n 65536 fi fi | ||||||||
v. | Identifying Required Software Directories | |||||||
1 | Identifying or Creating an Oracle Base Directory | |||||||
a. | Identifying an Existing Oracle Base Directory | |||||||
$more /etc/oraInst.loc | ||||||||
b. | Creating an Oracle Base Directory | |||||||
$mkdir -p /ora/app/oracle $chown -R oracle:oinstall /ora/app/oracle $chmod -R 775 /ora/app/oracle $mkdir -p /ora/app/oracle/product/10.2.0 $chown -R oracle:oinstall /ora/app/oracle/product/10.2.0 $chmod -R 775 /ora/app/oracle/product/10.2.0 | ||||||||
vi. | Creating Directories for Oracle Database or Recovery Files | |||||||
1 | Creating Required Directories | |||||||
a. | Database files | |||||||
$mkdir /ora/app/oracle/product/10.2.0/oradata $chown -R oracle:oinstall /ora/app/oracle/product/10.2.0/oradata $chmod -R 775 /ora/app/oracle/product/10.2.0/oradata | ||||||||
b. | Recovery files | |||||||
$mkdir /ora/app/oracle/product/10.2.0/flash_recovery_area $chown -R oracle:oinstall /ora/app/oracle/product/10.2.0/flash_recovery_area $chmod -R 775 /ora/app/oracle/product/10.2.0/flash_recovery_area | ||||||||
vii. | Configuring the oracle User's Environment | |||||||
$mkdir -p /ora/app/oracle/tmp $chmod a+wr /ora/app/oracle/tmp $su - oracle $vi .bash_profile umask 022 TEMP=/ora/app/oracle/tmp TMPDIR=/ora/app/oracle/tmp export TEMP TMPDIR ORACLE_BASE=/ora/app/oracle ORACLE_HOME=/ora/app/oracle/product/10.2.0/db_1 ORACLE_SID=orcl export ORACLE_BASE ORACLE_HOME ORACLE_SID export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib $. ./.bash_profile $vi /etc/redhat-release redhat 4 $yum -y install libXp | ||||||||
b. | Installation Tasks | |||||||
$xhost + $xterm $su - oracle $export DISPLAY=:0.0 $./database/runInstall | ||||||||
3 | Database Administration | |||||||
a. | Tasks of a Database Administrator | |||||||
i. | Create and Open the Database | |||||||
Use the Database Configuration Assistant (DBCA) | ||||||||
$xhost + $xterm $su - oracle $cd /ora/app/oracle/product/10.2.0/db_1/bin $./dbca | ||||||||
ii. | Starting Up and Shutting Down | |||||||
Starting Up a Database | ||||||||
$su - oracle $cd /ora/app/oracle/product/10.2.0/db_1/bin $./sqlplus /nolog conn sys/orcl as sysdba startup force exit $./lsnrctl start $./emctl start dbconsole | ||||||||
Shutting Down a Database | ||||||||
$su - oracle $cd /ora/app/oracle/product/10.2.0/db_1/bin $./sqlplus /nolog conn sys/orcl as sysdba shutdown immediate exit $./lsnrctl stop $./emctl stop dbconsole | ||||||||
星期一, 3月 14, 2011
星期四, 1月 13, 2011
MySQL Knowlage - Memo
簡介:
在資料存取時,最重要的就是編碼問題,這是中文比英文麻煩之處MySQL 有二個與編碼相關的設定 : Character Sets(文字編碼) & Collations(連線校對)
- Character Sets(文字編碼)
是一組符號及其編碼(is a set of symbols and encodings)舉例來說:有四個字元『A, B, a, b』,這些字元即所謂的符號 (symbols),而這些符號存在資料庫有特定的編碼方式 (encodings)。譬如:'0' 代表 A, '1' 代表B。
- Collations(連線校對)
是一組用來比較上述文字編碼的規則(is a set of rules for comparing characters in a character set)譬如:上述 A 與 B 進行比較,我們知道 A <>
- Character Sets 與 Collations 的設置
預設的編碼與校對都是 Latin1如何查詢相關設定值?
- show variables like "%character%";
- show variables like "%collation%";
- MyISAM
use Table Locking mechanism was the default storage engine for the MySQL relational database management system versions prior to 5.5. The major deficiency of MyISAM is the absence of transactions support.
- InnoDB
use Row Locking mechanism is the default storage engine for MySQL as of MySQL 5.5 provides the standard ACID-compliant transaction features along with foreign key support (Declarative Referential Integrity)
- SET PASSWORD FOR 'user'@'%.domain' = PASSWORD('newpass');
2.允許遠端連線管理資料庫:
- 從遠端10.10.0.183(xxx-PC) ping該資料庫主機,會出現底下錯誤訊息:
>mysqladmin --host=10.10.0.103 -u root pingmysqladmin: connect to server at '10.10.0.103' failederror: 'Host 'xxx-PC' is not allowed to connect to this MySQL server'
- 回到資料庫主機(IP:10.10.0.103)執行底下命令,執行授權:
>mysql -u root -ppass
CREATE USER 'root'@'%' IDENTIFIED BY 'pass';
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'pass' WITH GRANT OPTION;
ps. 'root'@'%'代表管理者可以從任何一台遠端機器連線進來,若是只限定某些電腦則「%」可以換成指定IP或Hostname
>mysql -u root -p pass
REVOKE ALL PRIVILEGES ON *.* FROM root@`%`;DELETE FROM user WHERE user="root" AND host="%";FLUSH PRIVILEGES;
mysqlmy-cnf設定檔說明22.MySQL Replication
4.MySQL vs Java
MySQL與Java資料型別對應
星期日, 1月 09, 2011
Maven 2.2.1 - Memo
Use JBoss Seam Framework 2.2.0 GA - Memo
- Documentations Home : http://www.jboss.org/jbossas/docs/5-x
- Installation and Getting Started Guide : http://docs.jboss.org/jbossas/docs/Installation_And_Getting_Started_Guide/5/html/index.html
- Administration and Configuration Guide : http://docs.jboss.org/jbossas/docs/Administration_And_Configuration_Guide/5/html/index.html
- Clustering Guide : http://docs.jboss.org/jbossclustering/cluster_guide/5.1/html/index.html
- Administration Console Quick Start Guide(PDF) : http://www.jboss.org/jbossas/docs/5-x/admin-console-quick-start.html
- JBoss Messaging 1.4 User's Guide :
http://docs.jboss.org/jbossmessaging/docs/userguide-1.4.4.GA/html/
- Documentations : http://docs.jboss.org/seam/2.2.0.GA/reference/en-US/html/
- API Documentations : http://docs.jboss.org/seam/2.2.0.GA/api/index.html
- Reference Guide : http://docs.jboss.org/hibernate/core/3.3/reference/en-US/html/
- API Documentations : http://docs.jboss.org/hibernate/core/3.3/api/
- Reference Guide : http://docs.jboss.org/hibernate/annotations/3.4/reference/en/html/
- API Documentations : http://docs.jboss.org/hibernate/annotations/3.4/api/
- Reference Guide : http://docs.jboss.org/hibernate/entitymanager/3.4/reference/en/html/
- API Documentations : http://docs.jboss.org/hibernate/entitymanager/3.4/api/
- Reference Guide : http://docs.jboss.org/hibernate/validator/3.1.0/reference/en/html/
- API Documentations : http://docs.jboss.org/hibernate/validator/3.1.0/api/
- Tag Library Documentation : http://download.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/index.html
- API Documentation : http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html
- API Documentations : http://docs.jboss.org/richfaces/3.3.X/3.3.1.GA/en/apidoc/
- API Documentations : http://docs.jboss.org/richfaces/3.3.X/3.3.1.GA/en/apidoc_framework/
- RichFaces Live Demo Site :
http://livedemo.exadel.com/richfaces-demo/richfaces/actionparam.jsf - RichFaces Developer Guide :
http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/ - Tag Library Documentation Generator - Generated Documentation : http://docs.jboss.org/richfaces/latest_3_3_X/en/tlddoc/
- RichFaces Photo Album Application Guide(Example) : http://docs.jboss.org/richfaces/3.3.X/3.3.1.GA/en/realworld/html/
- Seam Framework學習重點在Configruation與Annotation、Context與Component這幾個概念上面
- Hiberante Framework學習重點在要能區分EJB3/JPA與Hibernate之間的關聯、Mapping Classes(Inheritance Mapping Strategies)、Classes Association、Transactions、Efficient Storing And Loading等等
星期六, 1月 08, 2011
Eclipse安裝JAD插件 ─ Jadclipse
Java 反編譯工具 - JAD
星期日, 1月 02, 2011
JBoss 5.0.1 相關使用情境說明二三事




星期三, 12月 29, 2010
以POI讀取Excel檔案儲存格內容
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* @author Acer
*
*/
public class HSSFCellUtil {
public static String getCellContent(HSSFCell cell, String type) {
return getCellContent(cell, type, "");
}
public static String getCellContent(HSSFCell cell, String type, String pattern) {
String content = "";
if (!(cell == null)) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
content = cell.getStringCellValue().trim();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
content = new Boolean(cell.getBooleanCellValue()).toString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date d = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
if (pattern != "" || (!pattern.equals(""))) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
content = sdf.format(d);
} else
content = d.toString();
} else {
if (type.equals("byte")) {
content = new Byte(new Double(cell
.getNumericCellValue()).byteValue()).toString();
} else if (type.equals("short")) {
content = new Short(new Double(cell
.getNumericCellValue()).shortValue())
.toString();
} else if (type.equals("int")) {
content = new Integer(new Double(cell
.getNumericCellValue()).intValue()).toString();
} else if (type.equals("long")) {
content = new Long(new Double(cell
.getNumericCellValue()).longValue()).toString();
} else if (type.equals("float")) {
content = new Float(new Double(cell
.getNumericCellValue()).floatValue())
.toString();
} else if (type.equals("double")) {
content = new Double(cell.getNumericCellValue())
.toString();
} else if (type.equals("string")) {
content = new Double(cell.getNumericCellValue())
.toString().replaceAll("\\.[0]*", "");
} else {
content = new Double(cell.getNumericCellValue())
.toString();
}
}
break;
default:
break;
}
}
return content;
}
}

