跟踪参数
1)-XX:+printGC a: -verbose:gc - [GC 4690 ->374K(15872K),0.0001606secs] 清除前堆大小 清除后 容量 2)-XX:+PrintGCDetails 打印GC详细信息 -XX:+PrintGCTimeStamps 打印GC发生的时间戳 3)指定GC位置 -Xloggc:log/gc.log //当前目录下的log目录下的文件中 4) -XX:+PrintHeapAtGc 每次一次GC后都打印堆信息 5)-XX:+TraceClassLoading 监控类的加载 6)-XX:+PrintClassHistogram -按下ctrl + Break 后 打印类的信息 -分别显示: 序号、实例数量、总大小、类型###堆的分配参数
1)-Xms20m -Xms5m 指定最大堆20m 最小堆5m 2)-Xmn15m 设置新生代大小15m 3)-XX:NewRatio 设置新生代(eden + 2 * s)和老年代(不包含永久区)的比值 如 4 则表示 新生代:老年代 1:4 即年轻代占堆的 1/5 4)-XX:SurvivorRato 设置两个Survivor区和eden的比 如 8 则表示 2个Survivor:eden = 2:8 5)-XX:+HeapDumpOnOutMemmoryError oom时导出堆到文件 6)-XX:+HeapDumpPath 导出OOM的路径 如 -Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpPath=d:/a.dump 7) -XX:OnOutOfMemoryError 在OOM时,执行一个脚本 如-XX:OnOutOfMemoryEoor=D:/tools/jdk1.7_40/bin/printstack.bat %p printstack.bat:D:/tools/jdk1.7_40/bin/jstack -F 1%###永久区分配参数
1)-XX:PermSize 永久区初始空间 2)-XX:MaxPermSize 永久区最大空间###栈大小分配
-Xss 通常只有几百k,决定了函数调用的深度,每个线程都有独立的栈空间,局部变量、参数分配在栈上。
案例
//JVM参数 -Xmx20m -Xms5m (最大堆内存20m,最小堆内存5m)
public static void main(String[] args) {
byte[] bArr=new byte[1 * 1024 * 1024]; //1m内存
//byte[] bArr=new byte[4 * 1024 * 1024]; //4m内存
System.out.println(" heap is 1 M");
//System.gc(); //手动GC
System.out.print("Xmx = ");
System.out.print(Runtime.getRuntime().maxMemory()/1024.0/1024 + "M");
System.out.println();
System.out.print("free mem = ");
System.out.print(Runtime.getRuntime().freeMemory()/1024.0/1024 + "M");
System.out.println();
System.out.print("total mem =");
System.out.print(Runtime.getRuntime().totalMemory()/1024.0/1024 + "M");
}
//输出1
//heap is 1 M
//Xmx = 18.0M 最大可用内存
//free mem = 3.626312255859375M 空闲内存
//输出2 //开启4m内存
//heap is 4 M
//Xmx = 18.0M
//free mem = 4.5825958251953125M
//total mem =10.0M 增多
//输出3 开启4M 后 手动gc
//heap is 1 M
//Xmx = 18.0M
//free mem = 5.04815673828125M 空闲内存增多
//total mem =10.0M
//JVM参数 -Xmx20m -Xms20m -Xmn1m -XX:+PrintGCDetails
public static void main(String[] args) {
byte [] bArr = null ;
for( int i = 0 ; i < 10 ; i ++) {
bArr=new byte[1 * 1024 * 1024];
}
//jvm参数 -Xmx20m -Xms20m -Xmn1m -XX:+PrintGCDetails
//输出:新生代GC频繁,对象较大,直接进入老年大,老年代内存大。
//[GC (Allocation Failure) [PSYoungGen: 512K->504K(1024K)] 512K->504K(19968K), 0.0009776 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
//[GC (Allocation Failure) [PSYoungGen: 1007K->504K(1024K)] 1007K->632K(19968K), 0.0015814 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
//[GC (Allocation Failure) [PSYoungGen: 1008K->504K(1024K)] 1136K->786K(19968K), 0.0008114 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
//[GC (Allocation Failure) [PSYoungGen: 1016K->504K(1024K)] 1298K->890K(19968K), 0.0008174 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
//[GC (Allocation Failure) [PSYoungGen: 1016K->504K(1024K)] 7546K->7090K(19968K), 0.0015194 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
//Heap
//Java HotSpot(TM) 64-Bit Server VM warning: NewSize (1536k) is greater than the MaxNewSize (1024k). A new max generation size of 1536k will be used.
//PSYoungGen total 1024K, used 549K [0x00000000ffe80000, 0x0000000100000000, 0x0000000100000000)
//eden space 512K, 8% used [0x00000000ffe80000,0x00000000ffe8b400,0x00000000fff00000)
//from space 512K, 98% used [0x00000000fff00000,0x00000000fff7e010,0x00000000fff80000)
//to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
//ParOldGen total 18944K, used 10682K [0x00000000fec00000, 0x00000000ffe80000, 0x00000000ffe80000)
//object space 18944K, 56% used [0x00000000fec00000,0x00000000ff66e970,0x00000000ffe80000)
//Metaspace used 3448K, capacity 4494K, committed 4864K, reserved 1056768K
//class space used 380K, capacity 386K, committed 512K, reserved 1048576K
}
//调整jvm参数-Xmx20m -Xms20m -Xmn15m(修改为15m) -XX:+PrintGCDetails
//相同效果:-Xmx20m -Xms20m -XX:NewRatio=1(新生代老年代比)-XX:SurvivorRatio=2 -XX:+PrintGCDetails
//输出:新生代大,新生代发生一次GC,对象在新生代积累后GC,进入老年代的内存占比少。
[GC (Allocation Failure) [PSYoungGen: 12254K->1528K(13824K)] 12254K->1962K(18944K), 0.0032452 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 13824K, used 2798K [0x00000000ff100000, 0x0000000100000000, 0x0000000100000000)
eden space 12288K, 10% used [0x00000000ff100000,0x00000000ff23d8a0,0x00000000ffd00000)
from space 1536K, 99% used [0x00000000ffd00000,0x00000000ffe7e040,0x00000000ffe80000)
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
ParOldGen total 5120K, used 434K [0x00000000fec00000, 0x00000000ff100000, 0x00000000ff100000)
object space 5120K, 8% used [0x00000000fec00000,0x00000000fec6c8a0,0x00000000ff100000)
Metaspace used 3464K, capacity 4494K, committed 4864K, reserved 1056768K
class space used 382K, capacity 386K, committed 512K, reserved 1048576K
//JVM参数 -Xmx20m -Xms20m -Xmn7m(调整为7m) -XX:SurvivorRatio=2(调整存活区比例) -XX:+PrintGCDetails
//输出:调整新生代与存活区为1:1,GC相对增加
[GC (Allocation Failure) [PSYoungGen: 3660K->971K(5632K)] 3660K->2003K(18944K), 0.0023069 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4121K->1528K(5632K)] 5153K->2856K(18944K), 0.0010785 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 4793K->1504K(5632K)] 6121K->2936K(18944K), 0.0007354 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 5632K, used 4717K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
eden space 4096K, 78% used [0x00000000ff900000,0x00000000ffc23430,0x00000000ffd00000)
from space 1536K, 97% used [0x00000000ffd00000,0x00000000ffe78040,0x00000000ffe80000)
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
ParOldGen total 13312K, used 1432K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
object space 13312K, 10% used [0x00000000fec00000,0x00000000fed66010,0x00000000ff900000)
Metaspace used 3454K, capacity 4494K, committed 4864K, reserved 1056768K
class space used 380K, capacity 386K, committed 512K, reserved 1048576K
//JVM参数 -Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/a.dump
public static void main(String[] args) {
Vector vector=new Vector();
for( int i = 0 ; i < 30 ; i ++) {
vector.add(new byte[1 * 1024 * 1024]);
}
}
//输出 jvm内存逃逸生成的内存dump文件分析
java.lang.OutOfMemoryError: Java heap space
Dumping heap to d:/a.dump ...
Heap dump file created [16420586 bytes in 0.065 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.mybatis.datasources.common.jvm.TestJvm2.main(TestJvm2.java:13)
“`