JVM参数类型
标配参数
java -version
java -help
java -showversion
X参数
-Xint 解释执行
-Xcomp 编译执行
-Xmixed 混合执行(先编译后执行)
XX参数
-
Boolean 类型
-XX:+ 表示开启
举例: -XX:+PrintGCDetails
-XX:- 表示关闭
举例: -XX:- PrintGCDetails -
KV设值类型
-XX:属性key=属性值value
举例:
-XX:MetaspaceSize=128m //设置元空间大小
-XX:MaxTenuringThreshold=15 //多大的极限次数可以到养老区
JVM默认配置参数
- 查看jvm出厂默认设置 java -XX:+PrintFlagsInitial
H:\interview_code>java -XX:+PrintFlagsInitial
[Global flags]
uintx AdaptiveSizeDecrementScaleFactor = 4 {product}
uintx AdaptiveSizeMajorGCDecayTimeScale = 10 {product}
uintx AdaptiveSizePausePolicy = 0 {product}
uintx AdaptiveSizePolicyCollectionCostMargin = 50 {product}
uintx AdaptiveSizePolicyInitializingSteps = 20 {product}
uintx AdaptiveSizePolicyOutputInterval = 0 {product}
uintx AdaptiveSizePolicyWeight = 10 {product}
uintx AdaptiveSizeThroughPutPolicy = 0 {product}
uintx AdaptiveTimeWeight = 25 {product}
bool AdjustConcurrency = false {product}
bool AggressiveOpts = false {product}
intx AliasLevel = 3 {C2 product}
bool AlignVector = true {C2 product}
intx AllocateInstancePrefetchLines = 1 {product}
intx AllocatePrefetchDistance = -1 {product}
intx AllocatePrefetchInstr = 0 {product}
intx AllocatePrefetchLines = 3 {product}
intx AllocatePrefetchStepSize = 16 {product}
intx AllocatePrefetchStyle = 1 {product}
bool AllowJNIEnvProxy = false {product}
bool AllowNonVirtualCalls = false {product}
bool AllowParallelDefineClass = false {product}
bool AllowUserSignalHandlers = false {product}
bool AlwaysActAsServerClassMachine = false {product}
bool AlwaysCompileLoopMethods = false {product}
bool AlwaysLockClassLoader = false {product}
bool AlwaysPreTouch = false {product}
bool AlwaysRestoreFPU = false {product}
bool AlwaysTenure = false {product}
bool AssertOnSuspendWaitFailure = false {product}
bool AssumeMP = false {product}
bool PrintCommandLineFlags = false {product}
bool PrintCompilation = false {product}
bool PrintConcurrentLocks = false {manageable}
intx PrintFLSCensus = 0 {product}
intx PrintFLSStatistics = 0 {product}
bool UseGCOverheadLimit = true {product}
bool UseGCTaskAffinity = false {product}
bool UseHeavyMonitors = false {product}
bool UseInlineCaches = true {product}
bool UseInterpreter = true {product}
bool UseJumpTables = true {C2 product}
bool UseLWPSynchronization = true {product}
bool UseLargePages = false {pd product}
bool UseLargePagesInMetaspace = false {product}
bool UseLargePagesIndividualAllocation := false {pd product}
bool VerifyMergedCPBytecodes = true {product}
bool VerifySharedSpaces = false {product}
intx WorkAroundNPTLTimedWaitHang = 1 {product}
uintx YoungGenerationSizeIncrement = 20 {product}
uintx YoungGenerationSizeSupplement = 80 {product}
uintx YoungGenerationSizeSupplementDecay = 8 {product}
uintx YoungPLABSize = 4096 {product}
bool ZeroTLAB = false {product}
intx hashCode = 5 {product}
//以上截取的为部分显示内容
bool UseLargePagesInMetaspace = false {product}
bool UseLargePagesIndividualAllocation := false {pd product}
注意这两条命令的区别
= false 表明没有改过的默认值
:= false 人为修改过的值
2. 查看修改更新 -XX:+PrintFlagsFinal
运行java命令的同时打印出参数:
java -XX:+PrintFlagsFinal -Xss128k 运行的java类名
如何查看一个正在运行中的java程序,它的某个jvm参数是否开启?具体指是多少
- jps -l 查看当前进程信息
H:\interview_code>jps -l
6464 com.wu.pratice.jvm.JVMParamsDemo
2804 org.jetbrains.jps.cmdline.Launcher
10700
15964 sun.tools.jps.Jps
- 通过jinfo查看某个jvm配置参数是否开启
H:\interview_code>jinfo -flag PrintGCDetails 6464
-XX:-PrintGCDetails
- 查看当前进程所有配置参数值信息
Non-default VM flags JVM: 当前进程JVM配置参数
Command line: 自己优化配置后的参数
H:\interview_code>jinfo -flags 6464
Attaching to process ID 6464, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.121-b13
Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=4273995776 -XX:MaxNewSize=1424490496 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:+PrintGCDetails -XX:+UseC
ompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
Command line: -XX:+PrintGCDetails -javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=64081:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin -Dfile.encoding=UTF-8
常用的JVM参数
- -Xms 默认值为系统物理内存的1/64
等价于 -XX:InitialHeapSize - -Xmx 默认值为系统物理内存的1/4
等价于 -XX:MaxHeapSize - -Xss 设置单个线程栈的大小,一般默认值为512k~1024k
等价于-XX:ThreadStackSize
如果查询出来的值为0,说明是JVM默认值(不过该值依赖于不同的操作系统Linux、OS X、Oracle Solaris都为1024k) - -Xmn 设置年轻代大小
新生代的空间大小为1/3堆空间 老年代为2/3堆空间 - -XX:MetaspaceSize 设置元空间大小
元空间的本质和永久带类似,都是对JVM规范中方法区的实现。
不过元空间与永久带之间最大的区别在于:元空间并不在虚拟机中,而是使用本地内存。
因此默认情况下,元空间的大小仅受本地内存限制。 - -XX:+PrintGCDetails
输出详细GC手机日志信息
E:\soft\java\jdk1.8.0_121\bin\java -Xms10m -Xmx10m -XX:+PrintGCDetails "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=50079:E:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath "E:\soft\java\jdk1.8.0_121\jre\lib\charsets.jar;E:\soft\java\jdk1.8.0_121\jre\lib\deploy.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\localedata.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\sunec.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;E:\soft\java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;E:\soft\java\jdk1.8.0_121\jre\lib\javaws.jar;E:\soft\java\jdk1.8.0_121\jre\lib\jce.jar;E:\soft\java\jdk1.8.0_121\jre\lib\jfr.jar;E:\soft\java\jdk1.8.0_121\jre\lib\jfxswt.jar;E:\soft\java\jdk1.8.0_121\jre\lib\jsse.jar;E:\soft\java\jdk1.8.0_121\jre\lib\management-agent.jar;E:\soft\java\jdk1.8.0_121\jre\lib\plugin.jar;E:\soft\java\jdk1.8.0_121\jre\lib\resources.jar;E:\soft\java\jdk1.8.0_121\jre\lib\rt.jar;H:\BaiduNetdiskDownload\java面试\v3 尚硅谷Java面试题\03_代码\01_javase_code\interview_code\out\production\interview_code" com.wu.pratice.jvm.OutOfMemoryErrorDemo
maxMemory = 9961472字节 9MB
totalMemory = 9961472字节 9MB
[GC (Allocation Failure) [PSYoungGen: 2040K->504K(2560K)] 2040K->736K(9728K), 0.0010516 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2393K->488K(2560K)] 2625K->1340K(9728K), 0.0017160 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2462K->488K(2560K)] 3315K->2558K(9728K), 0.0020651 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2463K->504K(2560K)] 6469K->4994K(9728K), 0.0006361 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2479K->0K(2560K)] [ParOldGen: 6426K->3563K(7168K)] 8906K->3563K(9728K), [Metaspace: 3506K->3506K(1056768K)], 0.0039259 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 3563K->3563K(9728K), 0.0001818 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 3563K->3542K(7168K)] 3563K->3542K(9728K), [Metaspace: 3506K->3506K(1056768K)], 0.0049544 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:674)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at com.wu.pratice.jvm.OutOfMemoryErrorDemo.main(OutOfMemoryErrorDemo.java:24)
Heap
PSYoungGen total 2560K, used 122K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 5% used [0x00000000ffd00000,0x00000000ffd1ea20,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 3542K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 49% used [0x00000000ff600000,0x00000000ff975b48,0x00000000ffd00000)
Metaspace used 3537K, capacity 4502K, committed 4864K, reserved 1056768K
class space used 391K, capacity 394K, committed 512K, reserved 1048576K
Process finished with exit code 1
- [PSYoungGen: 2040K->504K(2560K)] 2040K->736K(9728K), 0.0010516 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
名称 | 描述 |
---|---|
GC (Allocation Failure) | GC类型 |
[PSYoungGen: 2040K->504K(2560K)] | 新生代GC前(2040K)->新生代GC前(504K)(新生代总空间大小2560K) |
2040K->736K(9728K) | YoungGC前JVM堆内存占用->YoungGC后JVM堆内存占用(JVM堆内存总大小) |
0.0010516 secs | YoungGC用户耗时 |
[Times: user=0.00 sys=0.00, real=0.00 secs] | YoungGC系统耗时,YoungGC实际耗时 |
- [Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 3563K->3542K(7168K)] 3563K->3542K(9728K), [Metaspace: 3506K->3506K(1056768K)], 0.0049544 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
名称 | 描述 |
---|---|
Full GC (Allocation Failure) | Full GC类型 |
[PSYoungGen: 0K->0K(2560K)] | 新生代GC前(0K)->新生代GC后(0K)(新生代总空间大小2560K) |
[ParOldGen: 3563K->3542K(7168K)] | 养老区GC前(3563K)->养老区GC后(3542K)(养老区总空间大小7168K) |
3563K->3542K(9728K) | GC前JVM堆内存占用->GC后JVM堆内存占用(JVM堆内存总大小) |
[Metaspace: 3506K->3506K(1056768K)] | GC前元空间内存占用->GC后GC前元空间内存占用(GC前元空间内存总大小) |
0.0049544 secs | YoungGC用户耗时 |
[Times: user=0.00 sys=0.00, real=0.01 secs] | GC系统耗时,GC实际耗时 |
- -XX:SurvivorRatio
设置新生代中eden和S0/S1空间的比例,默认比例为 8:1:1
-XX:SurvivorRatio=8,Eden:S0:S1=8:1:1
举例:
-XX:SurvivorRatio=4,Eden:S0:S1=4:1:1
SurvivorRatio值就是设置Eden区的比例占多少,S0/S1相同
Heap
PSYoungGen total 72704K, used 5851K [0x000000076b180000, 0x0000000770680000, 0x00000007c0000000)
eden space 58368K, 10% used [0x000000076b180000,0x000000076b736f68,0x000000076ea80000)
from space 14336K, 0% used [0x000000076f880000,0x000000076f880000,0x0000000770680000)
to space 14336K, 0% used [0x000000076ea80000,0x000000076ea80000,0x000000076f880000)
eden区:eden space 58368K
S0:from space 14336K
S1:to space 14336K
- -XX:NewRatio
配置年轻代与老年代在堆结构中的占比,默认-XX:NewRatio=2新生代占1,老年代占2,年轻代占整个堆的1/3
举例:
-XX:NewRatio=4 新生代占1,老年代占4,年轻代占整个堆的1/5,NewRatio值就是设置老年代的占比,剩下的1给新生代。
Heap
PSYoungGen total 43520K, used 4189K [0x000000078d100000, 0x0000000790400000, 0x00000007c0000000)
eden space 34816K, 12% used [0x000000078d100000,0x000000078d517408,0x000000078f300000)
from space 8704K, 0% used [0x000000078fb80000,0x000000078fb80000,0x0000000790400000)
to space 8704K, 0% used [0x000000078f300000,0x000000078f300000,0x000000078fb80000)
ParOldGen total 209920K, used 0K [0x00000006c1400000, 0x00000006ce100000, 0x000000078d100000)
object space 209920K, 0% used [0x00000006c1400000,0x00000006c1400000,0x00000006ce100000)
年轻代:PSYoungGen total 43520K
老年代:ParOldGen total 209920K
- -XX:MaxTenuringThreshold
默认值为15
-XX:MaxTenuringThreshold=0,则年轻代对象不经过Survivor区,直接进入年老代。对于老年代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次赋值,这样可以增加对象在年轻代存活时间,增加在年轻代即被回收的概率
在java8中MaxTenuringThreshold最大值为15,设置大于15,结果如下
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
MaxTenuringThreshold of 20 is invalid; must be between 0 and 15