Trace跟踪参数
-verbose:gc
-XX:+PrintGC
[GC (System.gc()) 2663K->640K(125952K), 0.0097324 secs]
[Full GC (System.gc()) 640K->569K(125952K), 0.0052535 secs]
GC前用了约2663K-> GC后只用了640K(堆的大小约120M)
-XX:+ PrintGCDetails
打印GC详细信息。
[GC (System.gc()) [PSYoungGen: 2663K->616K(38400K)] 2663K->624K(125952K), 0.0036339 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) Disconnected from the target VM, address: '127.0.0.1:57922', transport: 'socket'
[PSYoungGen: 616K->0K(38400K)] [ParOldGen: 8K->569K(87552K)] 624K->569K(125952K), [Metaspace: 2830K->2830K(1056768K)], 0.0061896 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 38400K, used 333K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
eden space 33280K, 1% used [0x00000000d5d80000,0x00000000d5dd34a8,0x00000000d7e00000)
from space 5120K, 0% used [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000)
to space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
ParOldGen total 87552K, used 569K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
object space 87552K, 0% used [0x0000000081800000,0x000000008188e540,0x0000000086d80000)
Metaspace used 2839K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
-XX: + PrintGCTimeStamps
打印GC发的时间戳,自JVM启动以后以秒计量。
0.213: [GC (System.gc()) 2663K->656K(125952K), 0.0134724 secs]
0.226: [Full GC (System.gc()) 656K->569K(125952K), 0.0212011 secs]
-XX:+PrintGCDateStamps // GC发生的当前时间
2019-02-27T20:21:06.730+0800: [GC (System.gc()) 2663K->632K(125952K), 0.0055284 secs]
2019-02-27T20:21:06.736+0800: [Full GC (System.gc()) 632K->569K(125952K), 0.0045613 secs]
-XX: +PrintHeapAtGC
每次GC 后,都打印堆的详细信息。Heap before GC ,Heap after GC
{Heap before GC invocations=1 (full 0):
PSYoungGen total 38400K, used 2663K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
eden space 33280K, 8% used [0x00000000d5d80000,0x00000000d6019dd0,0x00000000d7e00000)
from space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
to space 5120K, 0% used [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000)
ParOldGen total 87552K, used 0K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
object space 87552K, 0% used [0x0000000081800000,0x0000000081800000,0x0000000086d80000)
Metaspace used 2827K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 304K, capacity 386K, committed 512K, reserved 1048576K
[GC (System.gc()) 2663K->704K(125952K), 0.0007834 secs]
Heap after GC invocations=1 (full 0):
PSYoungGen total 38400K, used 696K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
eden space 33280K, 0% used [0x00000000d5d80000,0x00000000d5d80000,0x00000000d7e00000)
from space 5120K, 13% used [0x00000000d7e00000,0x00000000d7eae010,0x00000000d8300000)
to space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
ParOldGen total 87552K, used 8K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
object space 87552K, 0% used [0x0000000081800000,0x0000000081802000,0x0000000086d80000)
Metaspace used 2827K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 304K, capacity 386K, committed 512K, reserved 1048576K
}
XX:+PrintGCApplicationStoppedTime
// 输出GC造成应用暂停的时间
Total time for which application threads were stopped: 0.0002322 seconds, Stopping threads took: 0.0001973 seconds
Total time for which application threads were stopped: 0.0000391 seconds, Stopping threads took: 0.0000119 seconds
Total time for which application threads were stopped: 0.0105439 seconds, Stopping threads took: 0.0000160 seconds
监控类的加载
-XX: +TraceClassLoading
[Opened D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Object from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.io.Serializable from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Comparable from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.String from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.Type from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Class from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
...
-XX: +PrintClassHistogram
按下Ctrl+Break后。打印类的使用情况 (怎么操作???)
输出日志
-Xloggc:logs/gc.log
指定GC log的路径输出日志文件
-XX:+PrintTenuringDistribution
了解新域的情况,了解获得使用期的对象权。(不太懂)
Desired survivor size 5242880 bytes, new threshold 7 (max 15)
堆的分配参数
-Xms:指定最小堆大小。
初始堆大小,默认为物理内存的1/64(<1GB);默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制
-Xmx:指定最大堆大小。
默认(MaxHeapFreeRatio参数可以调整)空余堆内存大于70%时,JVM会减少堆直到 -Xms的最小限制
eg:-Xmx20m -Xms5m
public static void printXmxXms(){
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
Xmx=18M // 最大可使用空间
free mem=4M // 当前未使用的空间
total mem=5M // 当前可用总mem空间
public static void printXmxXms(){
byte[] b = new byte[1024*1024];
System.out.println("分配了1M空间给数组");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
分配了1M空间给数组
Xmx=18M
free mem=3M
total mem=5M
- Java会尽可能用最小堆,将数组内存调整为4M
public static void printXmxXms(){
byte[] b = new byte[4*1024*1024];
System.out.println("分配了4M空间给数组");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
分配了4M空间给数组
Xmx=18M
free mem=5M
total mem=10M
- 分配后再回收,空闲内存会增多
public static void printXmxXms(){
byte[] b = new byte[4*1024*1024];
System.out.println("分配了4M空间给数组");
System.gc();
System.out.println("回收内存");
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
分配了4M空间给数组
回收内存
Xmx=18M
free mem=5M
total mem=10M
-Xmx 和 -Xms保持什么关系,可以让系统的性能尽可能的好?
-Xmn
:设置新生代的内存空间大小。注意:此处的大小是(eden+ 2 survivor space)。与jmap -heap中显示的New gen是不同的。整个堆大小=新生代大小 + 老生代大小 + 永久代大小。
在保证堆大小不变的情况下,增大新生代后,将会减小老生代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。
-XX:NewRatio
: 老年代(不包含永久区):新生代(eden + 2*s)。例如4,即新生代:老年代 = 1:4
,即年轻代占堆的1/5
-XX:SurvivorRatio
:新生代中Eden:Survivor容量比值,默认值为8,即两个Survivor区与一个Eden区的比值为2:8,一个Survivor区占整个年轻代的1/10。
-Xss
:每个线程的堆栈大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。应根据应用的线程所需内存大小进行适当调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右。一般小的应用, 如果栈不是很深, 应该是128k够用的,大的应用建议使用256k。这个选项对性能影响比较大,需要严格的测试。和threadstacksize选项解释很类似,官方文档似乎没有解释,在论坛中有这样一句话:"-Xss is translated in a VM flag named ThreadStackSize”一般设置这个值就可以了。
- 通常只有几百K
- 决定了函数调用的深度
- 每个线程都有独立的栈空间
- 局部变量、参数分配在栈上
// 去掉局部变量 调用层次可以更深
public static void testStackDeep(long a, long b, long c){
long e=1,f=2,g=3,h=4,i=5,k=6,q=7,x=8,y=9,z=10;
count++;
testStackDeep(a,b,c);
}
- -Xss128K结果:
deep of calling:306
java.lang.StackOverflowError
- -Xss256K结果:
deep of calling:761
java.lang.StackOverflowError
- -Xss128K 减少局部变量至 long e=1,f=2,g=3,h=4;
deep of calling:451
java.lang.StackOverflowError
-XX:PermSize
:设置永久代(perm gen)初始值。默认值为物理内存的1/64。
-XX:MaxPermSize
:设置持久代最大值。物理内存的1/4。
例子1:Xmn1m
-Xmx20m -Xms20M -Xmn1m -XX:+PrintGCDetails
byte[] b=null;
for(int i=0;i<10;i++)
b = new byte[1024*1024];
Heap
PSYoungGen total 1536K, used 928K [0x00000000ffe00000, 0x0000000100000000, 0x0000000100000000)
eden space 1024K, 90% used [0x00000000ffe00000,0x00000000ffee82f8,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 18432K, used 10240K [0x00000000fec00000, 0x00000000ffe00000, 0x00000000ffe00000)
object space 18432K, 55% used [0x00000000fec00000,0x00000000ff6000a0,0x00000000ffe00000)
Metaspace used 2840K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- 没有触发GC
- 全部分配在老年代
例子2: Xmn15m
-Xmx20m -Xms20M -Xmn15m -XX:+PrintGCDetails
Heap
PSYoungGen total 13824K, used 12025K [0x00000000ff100000, 0x0000000100000000, 0x0000000100000000)
eden space 12288K, 97% used [0x00000000ff100000,0x00000000ffcbe598,0x00000000ffd00000)
from space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
to space 1536K, 0% used [0x00000000ffd00000,0x00000000ffd00000,0x00000000ffe80000)
ParOldGen total 5120K, used 0K [0x00000000fec00000, 0x00000000ff100000, 0x00000000ff100000)
object space 5120K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ff100000)
Metaspace used 2835K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- 没有触发GC
- 全部分配在eden
- 老年代未使用
例子3: Xmn7m
-Xmx20m -Xms20M -Xmn7m -XX:+PrintGCDetails
[GC (Allocation Failure) [PSYoungGen: 5266K->488K(6656K)] 5266K->1616K(19968K), 0.0015067 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 5726K->504K(6656K)] 6854K->2680K(19968K), 0.0038591 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 6656K, used 1589K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
eden space 6144K, 17% used [0x00000000ff900000,0x00000000ffa0f748,0x00000000fff00000)
from space 512K, 98% used [0x00000000fff80000,0x00000000ffffe030,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 13312K, used 2176K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
object space 13312K, 16% used [0x00000000fec00000,0x00000000fee20020,0x00000000ff900000)
Metaspace used 2835K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- 进行了2次新生代GC
- from to 不够用,需要老年代担保,即部分存放在老年代中
例子4: -XX:SurvivorRatio=2
-Xmx20m -Xms20M -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails
调整幸存代的比例
[GC (Allocation Failure) [PSYoungGen: 3120K->1512K(5632K)] 3120K->1632K(18944K), 0.0011179 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4662K->1528K(5632K)] 4782K->1648K(18944K), 0.0009248 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4670K->1512K(5632K)] 4790K->1656K(18944K), 0.0006121 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 5632K, used 3715K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
eden space 4096K, 53% used [0x00000000ff900000,0x00000000ffb26f98,0x00000000ffd00000)
from space 1536K, 98% used [0x00000000ffd00000,0x00000000ffe7a030,0x00000000ffe80000)
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
ParOldGen total 13312K, used 144K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
object space 13312K, 1% used [0x00000000fec00000,0x00000000fec24000,0x00000000ff900000)
Metaspace used 2836K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- 对新生代进行了3次GC
- from to 空间增大
例子5:-XX:NewRatio 继续增大from to
-Xmx20m -Xms20M -Xmn7m -XX:NewRatio=1 -XX:SurvivorRatio=2 -XX:+PrintGCDetails
[GC (Allocation Failure) [PSYoungGen: 4215K->1720K(7680K)] 4215K->1728K(17920K), 0.0015462 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure)[PSYoungGen: 5914K->1720K(7680K)] 5922K->1736K(17920K), 0.0086064 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 7680K, used 5036K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 5120K, 64% used [0x00000000ff600000,0x00000000ff93d270,0x00000000ffb00000)
from space 2560K, 67% used [0x00000000ffd80000,0x00000000fff2e020,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 10240K, used 16K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 0% used [0x00000000fec00000,0x00000000fec04000,0x00000000ff600000)
Metaspace used 2840K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- GC次数降低
- 老年代中存放的数据比例很少
例子6:-XX:SurvivorRatio=4 减小幸存代大小增大eden
[GC (Allocation Failure) [PSYoungGen: 6411K->1528K(8704K)] 6411K->1664K(18944K), 0.0009235 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 8704K, used 6847K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 7168K, 74% used [0x00000000ff600000,0x00000000ffb31cd8,0x00000000ffd00000)
from space 1536K, 99% used [0x00000000ffd00000,0x00000000ffe7e020,0x00000000ffe80000)
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
ParOldGen total 10240K, used 136K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
object space 10240K, 1% used [0x00000000fec00000,0x00000000fec22000,0x00000000ff600000)
Metaspace used 2836K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 305K, capacity 386K, committed 512K, reserved 1048576K
- 新生代GC次数只有1次
- 老年代存放数据比例也很低
- 空间使用率高
-XX:+HeapDumpOnOutOfMemoryError
OOM时导出堆信息到文件
-XX:+HeapDumpPath
导出OOM的路径
-
HeapDump例子:
-Xmx20m -Xms5M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:/testHeapDump.dump
Vector vector = new Vector();
for (int i = 0;i<25;i++){
vector.add(new byte[1024*1024]);
}
java.lang.OutOfMemoryError: Java heap space
Dumping heap to D:/testHeapDump.dump ...
Disconnected from the target VM, address: '127.0.0.1:60500', transport: 'socket'
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at LookForClassLoader.testHeapDump(LookForClassLoader.java:66)
at LookForClassLoader.main(LookForClassLoader.java:22)
Heap dump file created [15981435 bytes in 0.027 secs]
通过debug工具打开即可
-XX:OnOutOfMemoryError
- 在OOM时,执行一个脚本
- -XX:OnOutOfMemoryError=/xxx/xxx.bat
- 可以在OOM时,发送邮件,甚至是重启程序
堆的分配参数-总结
- 根据实际事情调整新生代和幸存代的大小
- 官方推荐新生代占堆的3/8
- 幸存代占新生代的1/10
- 在OOM时,记得Dump出堆,确保可以排查现场问题
永久区分配参数
-XX:PermSize -XX:MaxPermSize
- 设置永久区的初初始空间和最大空间
- 表示一个系统可以容纳多少个类型
例子:使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM
打开堆的Dump
- 堆空间实际占用非常少
- 但是永久区溢出 一样抛出OOM(即如果堆空间没有用完也抛出了OOM,有可能是永久区溢出导致的)
相关了解:
-
年轻代(Young Gen)
- 总大小 已使用 [低边界(在内存中的起始位置),当前边界(当前被分配到的位置),最高边界(最高能申请到的位置)]
- 年轻代主要存放新创建的对象,内存大小相对会比较小,垃圾回收会比较频繁。年轻代分成1个Eden Space和2个Suvivor Space(命名为A和B)。当对象在堆创建时,将进入年轻代的Eden Space。垃圾回收器进行垃圾回收时,扫描Eden Space和A Suvivor Space,如果对象仍然存活,则复制到B Suvivor Space,如果B Suvivor Space已经满,则复制到Old Gen。同时,在扫描Suvivor Space时,如果对象已经经过了几次的扫描仍然存活,JVM认为其为一个持久化对象,则将其移到Old Gen。扫描完毕后,JVM将Eden Space和A Suvivor Space清空,然后交换A和B的角色(即下次垃圾回收时会扫描Eden Space和B Suvivor Space。这么做主要是为了减少内存碎片的产生。
-
0x00000000d8800000 - 0x00000000d5d80000 /1024= eden space + from space + to space =可用的容量
年轻代的total = eden space + from space
-
年老代(Tenured Gen)
- 年老代主要存放JVM认为生命周期比较长的对象(经过几次的Young Gen的垃圾回收后仍然存在),内存大小相对会比较大,垃圾回收也相对没有那么频繁(譬如可能几个小时一次)。
-
持久代(Perm Gen)
- 持久代主要存放类定义、字节码和常量等很少会变更的信息。
分代策略
JVM在程序运行过程当中,会创建大量的对象,这些对象,大部分是短周期的对象,小部分是长周期的对象,对于短周期的对象,需要频繁地进行垃圾回收以保证无用对象尽早被释放掉,对于长周期对象,则不需要频率垃圾回收以确保无谓地垃圾扫描检测。为解决这种矛盾,Sun JVM的内存管理采用分代的策略。
参考文档:https://blog.csdn.net/jisuanjiguoba/article/details/80156781