Trace跟蹤參數(shù)
- -verbose:gc
- -XX:+printGC
可以打印GC的簡要信息
[GC 4790K->374K(15872K), 0.0001606 secs]
[GC 4790K->374K(15872K), 0.0001474 secs]
[GC 4790K->374K(15872K), 0.0001563 secs]
[GC 4790K->374K(15872K), 0.0001682 secs]
- -XX:+PrintGCDetails
打印GC詳細(xì)信息 - -XX:+PrintGCTimeStamps
打印CG發(fā)生的時(shí)間戳
例
[GC[DefNew: 4416K->0K(4928K), 0.0001897 secs] 4790K->374K(15872K), 0.0002232 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
- -XX:+PrintGCDetails的輸出
Heap
def new generation total 13824K, used 11223K [0x27e80000, 0x28d80000, 0x28d80000)
eden space 12288K, 91% used [0x27e80000, 0x28975f20, 0x28a80000)
from space 1536K, 0% used [0x28a80000, 0x28a80000, 0x28c00000)
to space 1536K, 0% used [0x28c00000, 0x28c00000, 0x28d80000)
tenured generation total 5120K, used 0K [0x28d80000, 0x29280000, 0x34680000)
the space 5120K, 0% used [0x28d80000, 0x28d80000, 0x28d80200, 0x29280000)
compacting perm gen total 12288K, used 142K [0x34680000, 0x35280000, 0x38680000)
the space 12288K, 1% used [0x34680000, 0x346a3a90, 0x346a3c00, 0x35280000)
ro space 10240K, 44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
rw space 12288K, 52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
- Xloggc:log/gc.log
指定GC log的位置辜膝,以文件輸出
幫助開發(fā)人員分析問題 - -XX:+PrintHeapAtGC
每次一次GC后,都打印堆信息
{Heap before GC invocations=0 (full 0):
def new generation total 3072K, used 2752K [0x33c80000, 0x33fd0000, 0x33fd0000)
eden space 2752K, 100% used [0x33c80000, 0x33f30000, 0x33f30000)
from space 320K, 0% used [0x33f30000, 0x33f30000, 0x33f80000)
to space 320K, 0% used [0x33f80000, 0x33f80000, 0x33fd0000)
tenured generation total 6848K, used 0K [0x33fd0000, 0x34680000, 0x34680000)
the space 6848K, 0% used [0x33fd0000, 0x33fd0000, 0x33fd0200, 0x34680000)
compacting perm gen total 12288K, used 143K [0x34680000, 0x35280000, 0x38680000)
the space 12288K, 1% used [0x34680000, 0x346a3c58, 0x346a3e00, 0x35280000)
ro space 10240K, 44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
rw space 12288K, 52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
[GC[DefNew: 2752K->320K(3072K), 0.0014296 secs] 2752K->377K(9920K), 0.0014604 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap after GC invocations=1 (full 0):
def new generation total 3072K, used 320K [0x33c80000, 0x33fd0000, 0x33fd0000)
eden space 2752K, 0% used [0x33c80000, 0x33c80000, 0x33f30000)
from space 320K, 100% used [0x33f80000, 0x33fd0000, 0x33fd0000)
to space 320K, 0% used [0x33f30000, 0x33f30000, 0x33f80000)
tenured generation total 6848K, used 57K [0x33fd0000, 0x34680000, 0x34680000)
the space 6848K, 0% used [0x33fd0000, 0x33fde458, 0x33fde600, 0x34680000)
compacting perm gen total 12288K, used 143K [0x34680000, 0x35280000, 0x38680000)
the space 12288K, 1% used [0x34680000, 0x346a3c58, 0x346a3e00, 0x35280000)
ro space 10240K, 44% used [0x38680000, 0x38af73f0, 0x38af7400, 0x39080000)
rw space 12288K, 52% used [0x39080000, 0x396cdd28, 0x396cde00, 0x39c80000)
}
-XX:+TraceClassLoading
監(jiān)控類的加載
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
跟蹤調(diào)試的時(shí)候 那些類被加載出來-XX:+PrintClassHistogram
按下Ctrl+Break后,打印類的信息:
num #instances #bytes class name
----------------------------------------------
1: 890617 470266000 [B
2: 890643 21375432 java.util.HashMap$Node
3: 890608 14249728 java.lang.Long
4: 13 8389712 [Ljava.util.HashMap$Node;
5: 2062 371680 [C
6: 463 41904 java.lang.Class
分別顯示:序號、實(shí)例數(shù)量婚脱、總大小、類型
堆的分配參數(shù)
- Xmx –Xms
指定最大堆和最小堆
VM options: -Xmx20m -Xms5m 運(yùn)行代碼:
public static void main(String[] args) {
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
}
結(jié)果:
Xmx=18.0M
free mem=4.660301208496094M
total mem=5.5M
public static void main(String[] args) {
byte[] b= new byte[4 * 1024 * 1024];
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
}
結(jié)果:
Xmx=18.0M
free mem=5.074928283691406M
total mem=10.0M
總內(nèi)存變多
public static void main(String[] args) {
System.gc();
System.out.print("Xmx=");
System.out.println(Runtime.getRuntime().maxMemory()/1024.0/1024+"M");
System.out.print("free mem=");
System.out.println(Runtime.getRuntime().freeMemory()/1024.0/1024+"M");
System.out.print("total mem=");
System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
}
結(jié)果:
Xmx=18.0M
free mem=5.9815826416015625M
total mem=6.5M
空閑內(nèi)存變多
- -Xmn
設(shè)置新生代大小 - -XX:NewRatio
新生代(eden+2*s)和老年代(不包含永久區(qū))的比值
4 表示 新生代:老年代=1:4抓艳,即年輕代占堆的1/5 - -XX:SurvivorRatio
設(shè)置兩個(gè)Survivor區(qū)和eden的比
8表示 兩個(gè)Survivor :eden=2:8洪橘,即一個(gè)Survivor占年輕代的1/10
public static void main(String[] args) {
byte[] b=null;
for(int i=0;i<10;i++)
b=new byte[1*1024*1024];
}
VM options: -Xmx20m -Xms20m -Xmn3m -XX:+PrintGCDetails
輸出:
Heap
PSYoungGen total 2560K, used 1490K [0x00000007bfd00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 2048K, 72% used [0x00000007bfd00000,0x00000007bfe748a8,0x00000007bff00000)
from space 512K, 0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 17408K, used 10240K [0x00000007bec00000, 0x00000007bfd00000, 0x00000007bfd00000)
object space 17408K, 58% used [0x00000007bec00000,0x00000007bf6000a0,0x00000007bfd00000)
Metaspace used 3054K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 334K, capacity 388K, committed 512K, reserved 1048576K
- 沒有觸發(fā)GC
- 全部分配在老年代
VM options: -Xmx20m -Xms20m -Xmn15m -XX:+PrintGCDetails
輸出:
Heap
PSYoungGen total 13824K, used 11966K [0x00000007bf100000, 0x00000007c0000000, 0x00000007c0000000)
eden space 12288K, 97% used [0x00000007bf100000,0x00000007bfcaf8d0,0x00000007bfd00000)
from space 1536K, 0% used [0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000)
to space 1536K, 0% used [0x00000007bfd00000,0x00000007bfd00000,0x00000007bfe80000)
ParOldGen total 5120K, used 0K [0x00000007bec00000, 0x00000007bf100000, 0x00000007bf100000)
object space 5120K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bf100000)
Metaspace used 3051K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 334K, capacity 388K, committed 512K, reserved 1048576K
- 沒有觸發(fā)GC
- 全部分配在eden
- 老年代沒有使用
VM options: -Xmx20m -Xms20m -Xmn15m -XX:+PrintGCDetails
輸出
[GC (Allocation Failure) [PSYoungGen: 5797K->496K(6656K)] 6861K->2604K(19968K), 0.0011469 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 6656K, used 1622K [0x00000007bf900000, 0x00000007c0000000, 0x00000007c0000000)
eden space 6144K, 18% used [0x00000007bf900000,0x00000007bfa19ab8,0x00000007bff00000)
from space 512K, 96% used [0x00000007bff80000,0x00000007bfffc010,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 13312K, used 2108K [0x00000007bec00000, 0x00000007bf900000, 0x00000007bf900000)
object space 13312K, 15% used [0x00000007bec00000,0x00000007bee0f030,0x00000007bf900000)
Metaspace used 2947K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 325K, capacity 388K, committed 512K, reserved 1048576K
- 進(jìn)行了1次新生代GC
- s0 s1 太小需要老年代擔(dān)保
VM options: -Xmx20m -Xms20m -Xmn6m -XX:SurvivorRatio=2 -XX:+PrintGCDetails
[GC (Allocation Failure) [PSYoungGen: 2357K->1520K(4608K)] 2357K->1560K(18944K), 0.0010973 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 3658K->1504K(4608K)] 3698K->1552K(18944K), 0.0009730 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 3621K->1504K(4608K)] 3669K->1576K(18944K), 0.0004732 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 3604K->1520K(4608K)] 3676K->1608K(18944K), 0.0004172 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 3623K->1520K(4608K)] 3711K->1616K(18944K), 0.0004710 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 4608K, used 2619K [0x00000007bfa00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 3072K, 35% used [0x00000007bfa00000,0x00000007bfb12e48,0x00000007bfd00000)
from space 1536K, 98% used [0x00000007bfd00000,0x00000007bfe7c020,0x00000007bfe80000)
to space 1536K, 0% used [0x00000007bfe80000,0x00000007bfe80000,0x00000007c0000000)
ParOldGen total 14336K, used 96K [0x00000007bec00000, 0x00000007bfa00000, 0x00000007bfa00000)
object space 14336K, 0% used [0x00000007bec00000,0x00000007bec18000,0x00000007bfa00000)
Metaspace used 2929K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 319K, capacity 388K, committed 512K, reserved 1048576K
- 進(jìn)行了5次新生代GC
- s0 s1 增大
- -XX:+HeapDumpOnOutOfMemoryError
OOM時(shí)導(dǎo)出堆到文件 - -XX:+HeapDumpPath
導(dǎo)出OOM的路徑 - -XX:OnOutOfMemoryError
在OOM時(shí),執(zhí)行一個(gè)腳本
"-XX:OnOutOfMemoryError=D:/tools/jdk1.7_40/bin/printstack.bat %p“
當(dāng)程序OOM時(shí)荠割,在D:/a.txt中將會生成線程的dump
可以在OOM時(shí)妹卿,發(fā)送郵件,甚至是重啟程序
總結(jié)
- 根據(jù)實(shí)際事情調(diào)整新生代和幸存代的大小
- 官方推薦新生代占堆的3/8
- 幸存代占新生代的1/10
- 在OOM時(shí)蔑鹦,記得Dump出堆夺克,確保可以排查線程
永久區(qū)分配參數(shù)
- -XX:PermSize -XX:MaxPermSize
設(shè)置永久區(qū)的初始空間和最大空間
他們表示嚎朽,一個(gè)系統(tǒng)可以容納多少個(gè)類型
使用CGLIB等庫的時(shí)候铺纽,可能會產(chǎn)生大量的類,這些類哟忍,有可能撐爆永久區(qū)導(dǎo)致OOM
for(int i=0;i<100000;i++){
CglibBean bean = new CglibBean("geym.jvm.ch3.perm.bean"+i,new HashMap());
}
不斷產(chǎn)生新的類
打開堆的Dump
堆空間實(shí)際占用非常少
但是永久區(qū)溢出 一樣拋出OOM
如果堆空間沒有用完也拋出了OOM狡门,有可能是永久區(qū)導(dǎo)致的
棧大小分配
- 通常只有幾百K
- 決定了函數(shù)調(diào)用的深度
- 每個(gè)線程都有獨(dú)立的椣萸蓿空間
- 局部變量、參數(shù) 分配在棧上
private static int count=0;
public static void recursion(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++;
recursion(a,b,c);
}
public static void main(String args[]){
try{
recursion(0L,0L,0L);
}catch(Throwable e){
System.out.println("deep of calling = "+count);
e.printStackTrace();
}
}
VM options: -Xss160K
deep of calling = 235
java.lang.StackOverflowError
...
VM options: -Xss280K
deep of calling = 662
java.lang.StackOverflowError