1. java命令行參數
- 先看java命令行的參數
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
-zero to select the "zero" VM
-dcevm to select the "dcevm" VM
The default VM is server,
because you are running on a server-class machine.
-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>
Warning: this feature is deprecated and will be removed
in a future release.
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning: this feature is deprecated and will be removed
in a future release.
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 with specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions with specified granularity
-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
-splash:<imagepath>
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-1~deb9u1-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$
- 看一個實際運行的java命令:
solr@2f1fe8cc9f09:/opt/solr/server/solr-webapp/webapp$ ps aux | grep java
solr 1 0.2 13.3 3060500 272904 ? Ssl Oct08 44:03 /docker-java-home/jre/bin/java -server -Xms512m -Xmx512m -XX:NewRatio=3 -XX:SurvivorRatio=4 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ConcGCThreads=4 -XX:ParallelGCThreads=4 -XX:+CMSScavengeBeforeRemark -XX:PretenureSizeThreshold=64m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=50 -XX:CMSMaxAbortablePrecleanTime=6000 -XX:+CMSParallelRemarkEnabled -XX:+ParallelRefProcEnabled -XX:-OmitStackTraceInFastThrow -verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:/opt/solr/server/logs/solr_gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=9 -XX:GCLogFileSize=20M -Dsolr.log.dir=/opt/solr/server/logs -Djetty.port=8983 -DSTOP.PORT=7983 -DSTOP.KEY=solrrocks -Duser.timezone=UTC -Djetty.home=/opt/solr/server -Dsolr.solr.home=/opt/solr/server/solr -Dsolr.install.dir=/opt/solr -Dsun.net.inetaddr.ttl=60 -Dsun.net.inetaddr.negative.ttl=60 -Xss256k -jar start.jar --module=http
-
-Xms512m -Xmx512m
- 用於設置java堆的最小值和最大值,將最大-Xmx和最小-Xms設置為一樣可以避免堆自動擴展。
- 參考:JVM調優總結 -Xms -Xmx -Xmn -Xss等
- 參考:成為Java GC專家(5)—Java性能調優原則
-
-XX:NewRatio=3
- 用來來指定新生代和整個堆的大小比例,或者直接用–XX:NewSize來指定所需的新生代空間。如果設置了NewRatio,那么整個堆空間的1/(NewRatio +1)就是新生代空間的大小。
- 使用CMS垃圾回收時,需要設置一個充足的新生代空間。然而,當新生代空間的大小超過一個特定的水平,程序的響應能力會被降低。
-
-XX:SurvivorRatio=4
- Eden區與Survivor區的大小比值。設置為4,則兩個Survivor區與一個Eden區的比值為2:4,一個Survivor區占整個新生代的1/6。
- 參考:JVM系列三:JVM參數設置、分析
-
-XX:TargetSurvivorRatio=90
- 參考:JVM -XX: 參數介紹
- 參考:MaxTenuringThreshold 和 TargetSurvivorRatio參數說明
- 設定survivor區的目標使用率。默認50,即survivor區對象目標使用率為50%,最高90%。
- 設置survivor區的目標使用率,當使用率達到時重新調整TenuringThreshold值,讓對象盡早的去old區。
-
-XX:MaxTenuringThreshold=8
- 設置對象在新生代中最大的存活次數,最大值15,並行回收機制默認為15,CMS默認為4。每經過一次YGC,年齡加1,當survivor區的對象年齡達到TenuringThreshold時,表示該對象是長存活對象,就會直接晉升到老年代。
- 如果設置為0的話,則年輕代對象不經過Survivor區,直接進入年老代. 對於年老代比較多的應用,可以提高效率.如果將此值設置為一個較大值,則年輕代對象會在Survivor區進行多次復制,這樣可以增加對象再年輕代的存活時間,增加在年輕代即被回收的概率。該參數只有在串行GC時才有效.
-
-XX:+UseConcMarkSweepGC
- 使用CMS內存收集算法
- 啟用CMS低停頓垃圾收集器,減少FGC的暫停時間
-
-XX:+UseParNewGC
- 設置新生代為並行收集。可與CMS收集同時使用JDK5.0以上,JVM會根據系統配置自行設置,所以無需再設置此值
-
-XX:ConcGCThreads=4
- 參考:JVM實用參數(七)CMS收集器
- 標志-XX:ConcGCThreads=
(早期JVM版本也叫-XX:ParallelCMSThreads)定義並發CMS過程運行時的線程數。
-
-XX:ParallelGCThreads=4
- 並行收集器的線程數。
- 此值最好配置與處理器數目相等,同樣適用於CMS
-
-XX:+CMSScavengeBeforeRemark
- 參考:JVM GC算法 CMS 詳解(轉)
- 在CMS GC前啟動一次ygc,目的在於減少old gen對ygc gen的引用,降低remark時的開銷-----一般CMS的GC耗時 80%都在remark階段
- 開啟-XX:+CMSScavengeBeforeRemark選項,強制remark之前開始一次minor gc,減少remark的暫停時間,但是在remark之后也將立即開始又一次minor gc。
-
-XX:PretenureSizeThreshold=64m
- 對象超過多大是直接在舊生代分配
- 單位字節 新生代采用Parallel Scavenge GC時無效另一種直接在舊生代分配的情況是大的數組對象,且數組中無外部引用對象.
-
-XX:+UseCMSInitiatingOccupancyOnly
- 命令JVM不基於運行時收集的數據來啟動CMS垃圾收集周期,禁止hostspot自行觸發CMS GC。
- 只有當我們充足的理由(比如測試)並且對應用程序產生的對象的生命周期有深刻的認知時,才應該使用該標志。
-
-XX:CMSInitiatingOccupancyFraction=50
- 設定CMS在對內存占用率達到50%的時候開始GC(因為CMS會有浮動垃圾,所以一般都較早啟動GC);
- 這兩個設置一般配合使用,一般用於『降低CMS GC頻率或者增加頻率、減少GC時長』的需求;
- -XX:CMSInitiatingOccupancyFraction=70 是指設定CMS在對內存占用率達到70%的時候開始GC(因為CMS會有浮動垃圾,所以一般都較早啟動GC);
- -XX:+UseCMSInitiatingOccupancyOnly 只是用設定的回收閾值(上面指定的70%),如果不指定,JVM僅在第一次使用設定值,后續則自動調整.
-
-XX:CMSMaxAbortablePrecleanTime=6000
- 參考:Tenured 區並發垃圾回收器CMS介紹 -
-XX:+CMSParallelRemarkEnabled
- 降低標記停頓
-
-XX:+ParallelRefProcEnabled
- 參考:一步步優化JVM五:優化延遲或者響應時間(3)
- 這個選項可以用HotSpot VM的任何一種垃圾回收器上,他會是用多個的引用處理線程,而不是單個線程。這個選項不會啟用多線程運行方法的finalizer。他會使用很多線程去發現需要排隊通知的finalizable對象。
-
-XX:-OmitStackTraceInFastThrow
-
-verbose:gc
- 表示輸出虛擬機中GC的詳細情況.
- 參考:JVM啟動參數之 -verbose:gc
- 參考:jvm參數-verbose:gc和-XX:+PrintGC有區別?
-
-XX:+PrintHeapAtGC
- 打印GC前后的詳細堆棧信息
-
-XX:+PrintGCDetails
- 需要在生產環境或者壓測環境中測量這些參數下系統的表現,這時候需要打開GC日志查看具體的信息,因此加上參數:-verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/home/test/logs/gc.log
-
-XX:+PrintGCDateStamps
-
-XX:+PrintGCTimeStamps
-
-XX:+PrintTenuringDistribution
- 查看每次minor GC后新的存活周期的閾值
-
-XX:+PrintGCApplicationStoppedTime
- 打印垃圾回收期間程序暫停的時間.可與上面混合使用
-
-Xloggc:/opt/solr/server/logs/solr_gc.log
- 把相關日志信息記錄到文件以便分析.與上面幾個配合使用
-
-XX:+UseGCLogFileRotation
- 啟用GC日志文件的自動轉儲
-
-XX:NumberOfGCLogFiles=9
- GC日志文件的循環數目
-
-XX:GCLogFileSize=20M
- 控制GC日志文件的大小
- 參考:-xx:+usegclogfilerotation
- Built-in support for GC log rotation has been added to the HotSpot JVM. It is described in the RFE 6941923 and is available in: Java 6 Update 34 Java 7 Update 2 (but there is no reference to it in these release notes)。
- There are three new JVM flags that can be used to enable and configure it:
- -XX:+UseGCLogFileRotation
must be used with -Xloggc:;
-XX:NumberOfGCLogFiles=
must be >=1, default is one;
-XX:GCLogFileSize=M (or K)
default will be set to 512K.
-
-Xss256k
- 每個線程的堆棧大小
- JDK5.0以后每個線程堆棧大小為1M,以前每個線程堆棧大小為256K.更具應用的線程所需內存大小進行 調整.在相同物理內存下,減小這個值能生成更多的線程.但是操作系統對一個進程內的線程數還是有限制的,不能無限生成,經驗值在3000~5000左右。
1.2 javap反編譯工具
- 將class文件反編譯為字節碼
- 使用-XX:+PrintAssembly參數來輸出反匯編
1.3 java命令行參數使用
- -XX:+PrintGCDetails,在發生垃圾收集行為的時候打印內存回收日志。