jdk、jvm版本信息:
$ java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
程序
import java.util.List;
import java.util.LinkedList;
public class HelloGC {
public static void main(String[] args) {
System.out.println("HelloGC!");
List list = new LinkedList();
for(;;) {
byte[] b = new byte[1024*1024];
list.add(b);
}
}
}
命令:
java -Xmn10M -Xms40M -Xmx60M -XX:+PrintCommandLineFlags -XX:+PrintGCDetails HelloGC
输出:
$ java -Xmn10M -Xms40M -Xmx60M -XX:+PrintCommandLineFlags -XX:+PrintGCDetails HelloGC
-XX:InitialHeapSize=41943040 -XX:MaxHeapSize=62914560 -XX:MaxNewSize=10485760 -XX:NewSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
HelloGC!
[GC (Allocation Failure) [PSYoungGen: 8153K->696K(9216K)] 8153K->7872K(39936K), 0.0027271 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 8024K->720K(9216K)] 15200K->15064K(39936K), 0.0016375 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 8041K->744K(9216K)] 22385K->22256K(39936K), 0.0014473 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 8067K->760K(9216K)] 29580K->29440K(39936K), 0.0013977 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 760K->0K(9216K)] [ParOldGen: 28680K->29212K(46592K)] 29440K->29212K(55808K), [Metaspace: 2587K->2587K(1056768K)], 0.0084977 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 7325K->224K(9216K)] 36537K->36604K(55808K), 0.0014945 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 7542K->224K(9216K)] 43922K->43772K(55808K), 0.0012120 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 224K->0K(9216K)] [ParOldGen: 43548K->43548K(51200K)] 43772K->43548K(60416K), [Metaspace: 2587K->2587K(1056768K)], 0.0021271 secs] [Times: user=0.03 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 7321K->224K(9216K)] 50870K->50940K(60416K), 0.0012843 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 224K->0K(9216K)] [ParOldGen: 50716K->50717K(51200K)] 50940K->50717K(60416K), [Metaspace: 2587K->2587K(1056768K)], 0.0019857 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 7316K->7168K(9216K)] [ParOldGen: 50717K->50717K(51200K)] 58033K->57885K(60416K), [Metaspace: 2587K->2587K(1056768K)], 0.0023833 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 7168K->7168K(9216K)] [ParOldGen: 50717K->50705K(51200K)] 57885K->57873K(60416K), [Metaspace: 2587K->2587K(1056768K)], 0.0072273 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at HelloGC.main(HelloGC.java:9)
Heap
PSYoungGen total 9216K, used 7476K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
eden space 8192K, 91% used [0x00000000ff600000,0x00000000ffd4d3c0,0x00000000ffe00000)
from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000)
to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000)
ParOldGen total 51200K, used 50705K [0x00000000fc400000, 0x00000000ff600000, 0x00000000ff600000)
object space 51200K, 99% used [0x00000000fc400000,0x00000000ff5844f8,0x00000000ff600000)
Metaspace used 2616K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 292K, capacity 386K, committed 512K, reserved 1048576K
GC解析:
[Full GC (Allocation Failure)
//[回收类型GC/Full GC (回收原因)
[PSYoungGen: 7168K->7168K(9216K)] [ParOldGen: 50717K->50705K(51200K)] 57885K->57873K(60416K),
//[回收的分代:YoungGen新生代:回收前大小->回收后大小(新生代总空间)] [老年代:回收前大小->回收后大小(老年代总大小)]堆占用空间回收前大小->堆占用空间回收后大小(总堆大小)
[Metaspace: 2587K->2587K(1056768K)], 0.0072273 secs]
//[元空间回收前大小->元空间回收后大小(总大小)],耗时多少秒]
[Times: user=0.00 sys=0.00, real=0.01 secs]
// 用户态耗时,内核态耗,实际耗时。(因为时间太短,时间不是很精确)
heap dump解析
Heap
PSYoungGen total 8704K, used 7476K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
// 新生代 总大小为8704k(值为eden+一个survivor) 已被使用7476k 空间的起始地址,已使用空间截止地址,总空间的截止地址
eden space 8192K, 91% used [0x00000000ff600000,0x00000000ffd4d3c0,0x00000000ffe00000)
// eden区
from space 512K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000ffe80000)
// from 幸存区
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
// to 幸存区
ParOldGen total 51200K, used 50705K [0x00000000fc400000, 0x00000000ff600000, 0x00000000ff600000)
// 老年代
object space 51200K, 99% used [0x00000000fc400000,0x00000000ff5844f8,0x00000000ff600000)
// 分配的对象空间
Metaspace used 2617K, capacity 4486K, committed 4864K, reserved 1056768K
// 元空间 已使用2671k,总容量4486k,虚拟内存4864K,虚拟内存保留1056768K
class space used 292K, capacity 386K, committed 512K, reserved 1048576K
// 类信息空间
另两个很重要的命令!
java -XX:+PrintFlagsFinal -version|grep hold
//PrintFlagsFinal打印本版本虚拟机最终参数值。grep查找指定的参数
java -XX:+PrintFlagsInitial -version|grep hold
//PrintFlagsInitial打印本版本虚拟机初始化值参数
很重要的参数文档(参数可能不齐):
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html