深入JVM内核3 常用JVM配置参数

深入JVM内核 目录

Trace跟踪参数

  • -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详细信息
  • -XX:+PrintGCTimeStamps
    打印CG发生的时间戳

[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的位置,以文件输出
    帮助开发人员分析问题
  • -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
    监控类的加载
    [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]
    跟踪调试的时候 那些类被加载出来

  • -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

分别显示:序号、实例数量、总大小、类型

堆的分配参数

  • Xmx –Xms
    指定最大堆和最小堆

VM options: -Xmx20m -Xms5m 运行代码:

    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");
    }

结果:

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");
    }

结果:

Xmx=18.0M
free mem=5.074928283691406M
total mem=10.0M

总内存变多

    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");
    }

结果:

Xmx=18.0M
free mem=5.9815826416015625M
total mem=6.5M

空闲内存变多

  • -Xmn
    设置新生代大小
  • -XX:NewRatio
    新生代(eden+2*s)和老年代(不包含永久区)的比值
    4 表示 新生代:老年代=1:4,即年轻代占堆的1/5
  • -XX:SurvivorRatio
    设置两个Survivor区和eden的比
    8表示 两个Survivor :eden=2:8,即一个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
  1. 没有触发GC
  2. 全部分配在老年代

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
  1. 没有触发GC
  2. 全部分配在eden
  3. 老年代没有使用

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
  1. 进行了1次新生代GC
  2. s0 s1 太小需要老年代担保

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
  1. 进行了5次新生代GC
  2. s0 s1 增大
  • -XX:+HeapDumpOnOutOfMemoryError
    OOM时导出堆到文件
  • -XX:+HeapDumpPath
    导出OOM的路径
  • -XX:OnOutOfMemoryError
    在OOM时,执行一个脚本
    "-XX:OnOutOfMemoryError=D:/tools/jdk1.7_40/bin/printstack.bat %p“
    当程序OOM时,在D:/a.txt中将会生成线程的dump
    可以在OOM时,发送邮件,甚至是重启程序

总结

  1. 根据实际事情调整新生代和幸存代的大小
  2. 官方推荐新生代占堆的3/8
  3. 幸存代占新生代的1/10
  4. 在OOM时,记得Dump出堆,确保可以排查线程

永久区分配参数

  • -XX:PermSize -XX:MaxPermSize
    设置永久区的初始空间和最大空间
    他们表示,一个系统可以容纳多少个类型

使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM

for(int i=0;i<100000;i++){
    CglibBean bean = new CglibBean("geym.jvm.ch3.perm.bean"+i,new HashMap());
}

不断产生新的类

打开堆的Dump

堆空间实际占用非常少
但是永久区溢出 一样抛出OOM


如果堆空间没有用完也抛出了OOM,有可能是永久区导致的

栈大小分配

  • 通常只有几百K
  • 决定了函数调用的深度
  • 每个线程都有独立的栈空间
  • 局部变量、参数 分配在栈上
    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
特别感谢

深入JVM内核—原理、诊断与优化

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容