在Java、J2EE大型應用中,JVM非標準參數的配置直接關係到整個系統的性能。java
JVM非標準參數指的是JVM底層的一些配置參數,這些參數在通常開發中默認便可,不須要任何配置。可是在生產環境中,爲了提升性能,每每須要調整這些參數,以求系統達到最佳新能。web
另外這些參數的配置也是影響系統穩定性的一個重要因素,相信大多數Java開發人員都見過「OutOfMemory」類型的錯誤。呵呵,這其中極可能就是JVM參數配置不當或者就沒有配置沒意識到配置引發的。bootstrap
爲了說明這些參數,還須要說說JDK中的命令行工具一些知識作鋪墊。windows
首先看如何獲取這些命令配置信息說明:服務器
假設你是windows平臺,你安裝了J2SDK,那麼如今你從cmd控制檯窗口進入J2SDK安裝目錄下的bin目錄,而後運行java命令,出現以下結果,這些就是包括java.exe工具的和JVM的全部命令都在裏面。app
-----------------------------------------------------------------------函數
D:\j2sdk15\bin>java工具
Usage: java [-options] class [args...]性能
(to execute a class)ui
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-----------------------------------------------------------------------
在控制檯輸出信息中,有個-X(注意是大寫)的命令,這個正是查看JVM配置參數的命令。
其次,用java -X 命令查看JVM的配置說明:
運行後以下結果,這些就是配置JVM參數的祕密武器,這些信息都是英文的,爲了方便閱讀,我根據本身的理解翻譯成中文了(不許確的地方還請各位博友斧正)
-----------------------------------------------------------------------
D:\j2sdk15\bin>java -X
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM (see documentation)
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare:on require using shared class data, otherwise fail.
The -X options are non-standard and subject to change without notice.
-----------------------------------------------------------------------
JVM配置參數中文說明:
-----------------------------------------------------------------------
一、-Xmixed mixed mode execution (default)
混合模式執行
二、-Xint interpreted mode execution only
解釋模式執行
三、-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
設置zip/jar資源或者類(.class文件)存放目錄路徑
三、-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
追加zip/jar資源或者類(.class文件)存放目錄路徑
四、-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
預先加載zip/jar資源或者類(.class文件)存放目錄路徑
五、-Xnoclassgc disable class garbage collection
關閉類垃圾回收功能
六、-Xincgc enable incremental garbage collection
開啓類的垃圾回收功能
七、-Xloggc:<file> log GC status to a file with time stamps
記錄垃圾回日誌到一個文件。
八、-Xbatch disable background compilation
關閉後臺編譯
九、-Xms<size> set initial Java heap size
設置JVM初始化堆內存大小
十、-Xmx<size> set maximum Java heap size
設置JVM最大的堆內存大小
十一、-Xss<size> set java thread stack size
設置JVM棧內存大小
十二、-Xprof output cpu profiling data
輸入CPU概要表數據
1三、-Xfuture enable strictest checks, anticipating future default
執行嚴格的代碼檢查,預測可能出現的狀況
1四、-Xrs reduce use of OS signals by Java/VM (see documentation)
經過JVM還原操做系統信號
1五、-Xcheck:jni perform additional checks for JNI functions
對JNI函數執行檢查
1六、-Xshare:off do not attempt to use shared class data
儘量不去使用共享類的數據
1七、-Xshare:auto use shared class data if possible (default)
儘量的使用共享類的數據
1八、-Xshare:on require using shared class data, otherwise fail.
儘量的使用共享類的數據,不然運行失敗
The -X options are non-standard and subject to change without notice.
-----------------------------------------------------------------------
怎麼用這這些參數呢?其實全部的命令行都是這麼一用,下面我就給出一個最簡單的HelloWorl的例子來演示這個參數的用法,很是的簡單。
HelloWorld.java
-----------------------------------------------
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
編譯並運行:
D:\j2sdk15\bin>javac HelloWorld.java
D:\j2sdk15\bin>java -Xms256M -Xmx512M HelloWorld
Hello World!
呵呵,這下知足了吧!
實踐:在大型系統或者應用中配置JVM參數
好比你配置IDE工具的參數,常見的有IDEA、Eclipse,這個是在一個配置文件中指定便可。
若是你要在J2EE環境中配置這些參數,那麼你須要在J2EE應用服務器或者Servlet容器相關啓動參數設置處指定,其啓動文件中來配置,Tomcat是在catalina.bat中配置,weblogic和websphere是在其餘地方,具體我就說了,相信玩過的這些大型服務器的人都知道,沒玩過的看看這篇文章,玩玩就知道了,呵呵。
另外經常有人問到jdk的一些相關命令用法,其實,當你看到這裏的時候,你應該知道如何獲取這些命令的用法了。若是你還不會,那麼,建議你去學學DOS,我是沒轍了。若是你會這些,仍是沒有看明白,那麼你趕忙學學英語吧,這樣你就能看懂了。
另外:我在最後給出經常使用的幾個Java命令行說明,以供參考:
(1)、javac
用法:javac <選項> <源文件>
其中,可能的選項包括:
-g 生成全部調試信息
-g:none 不生成任何調試信息
-g:{lines,vars,source} 只生成某些調試信息
-nowarn 不生成任何警告
-verbose 輸出有關編譯器正在執行的操做的消息
-deprecation 輸出使用已過期的 API 的源位置
-classpath <路徑> 指定查找用戶類文件的位置
-cp <路徑> 指定查找用戶類文件的位置
-sourcepath <路徑> 指定查找輸入源文件的位置
-bootclasspath <路徑> 覆蓋引導類文件的位置
-extdirs <目錄> 覆蓋安裝的擴展目錄的位置
-endorseddirs <目錄> 覆蓋簽名的標準路徑的位置
-d <目錄> 指定存放生成的類文件的位置
-encoding <編碼> 指定源文件使用的字符編碼
-source <版本> 提供與指定版本的源兼容性
-target <版本> 生成特定 VM 版本的類文件
-version 版本信息
-help 輸出標準選項的提要
-X 輸出非標準選項的提要
-J<標誌> 直接將 <標誌> 傳遞給運行時系統
(2)、jar
用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目錄] 文件名 ...
選項:
-c 建立新的存檔
-t 列出存檔內容的列表
-x 展開存檔中的命名的(或全部的〕文件
-u 更新已存在的存檔
-v 生成詳細輸出到標準輸出上
-f 指定存檔文件名
-m 包含來自標明文件的標明信息
-0 只存儲方式;未用ZIP壓縮格式
-M 不產生全部項的清單(manifest〕文件
-i 爲指定的jar文件產生索引信息
-C 改變到指定的目錄,而且包含下列文件:
若是一個文件名是一個目錄,它將被遞歸處理。
清單(manifest〕文件名和存檔文件名都須要被指定,按'm' 和 'f'標誌指定的相同順序。
示例1:將兩個class文件存檔到一個名爲 'classes.jar' 的存檔文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:用一個存在的清單(manifest)文件 'mymanifest' 將 foo/ 目錄下的全部
文件存檔到一個名爲 'classes.jar' 的存檔文件中:
jar cvfm classes.jar mymanifest -C foo/ .