3. 常用JVM配置参数

Trace跟踪参数

  • 打印GC的简要信息
    • -verbose:gc
    • -XX:+printGC
    • [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]
  • 打印GC详细信息
    • -XX:+PrintGCDetails
  • 打印CG发生的时间戳
    • -XX:+PrintGCTimeStamps
    • [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]
  • 指定GC log的位置,以文件输出:帮助开发人员分析问题
    • -Xloggc:log/gc.log
  • 每次进行一次GC后,都打印堆信息
    • -XX:+PrintHeapAtGC
  • 监控类的加载
    • -XX:+TraceClassLoading
  • 按下Ctrl+Break后,打印类的信息:分别显示:序号、实例数量、总大小、类型
    • -XX:+PrintClassHistogram


      3. 常用JVM配置参数_第1张图片
      打印类信息.png

堆的分配参数

  • 指定最大堆和最小堆

    • -Xmx –Xms
    • -Xmx20m -Xms5m 运行代码:
    1. 指定最大堆20M,最小堆5M
      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=19.375M
      free mem=4.342750549316406M
      total mem=4.875M
    
    1. 指定最大堆20M,最小堆5M,分配了1M空间给数组
      byte[] b=new byte[1*1024*1024];
      System.out.println("分配了1M空间给数组");
    
      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");
    
      //Java会尽可能维持在最小堆
      System.out.print("total mem=");
      System.out.println(Runtime.getRuntime().totalMemory()/1024.0/1024+"M");
    
    结果:
      分配了1M空间给数组
      Xmx=19.375M
      free mem=3.4791183471679688M
      total mem=4.875M
    
    1. 指定最大堆20M,最小堆5M,分配了4M空间给数组
      b=new byte[4*1024*1024];
      System.out.println("分配了4M空间给数组");
    
      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");
    
    结果:
      分配了4M空间给数组
      Xmx=19.375M
      free mem=3.5899810791015625M
      //总内存变多了
      total mem=9.00390625M
    
    
    1. 指定最大堆20M,最小堆5M,回收内存
      b=new byte[4*1024*1024];
      System.out.println("分配了4M空间给数组");
    
      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");
    
      System.gc();
    
      System.out.println("回收内存");
      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");
      
    结果:
      分配了4M空间给数组
      Xmx=19.375M
      free mem=3.5899810791015625M
      //总内存变多了
      total mem=9.00390625M
      
      回收内存
      Xmx=19.375M
      //空闲内存增多
      free mem=6.354591369628906M
      total mem=10.75390625M
    
  • 设置新生代大小

    • -Xmn
  • 新生代(eden+2*s)和老年代(不包含永久区)的比值

    • 4 表示 新生代:老年代=1:4,即年轻代占堆的1/5
    • -XX:NewRatio
  • 设置两个Survivor区和eden的比

    • 8表示 两个Survivor :eden=2:8,即一个Survivor占年轻代的1/10
    • -XX:SurvivorRatio
  • OOM时导出堆到文件

    • -XX:+HeapDumpOnOutOfMemoryError
  • 导出OOM的路径

    • -XX:+HeapDumpPath
    • -Xmx20m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/a.dump
  • 在OOM时,执行一个脚本

    • -XX:OnOutOfMemoryError
  • 总结:

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

永久区分配参数

  • 设置永久区的初始空间和最大空间
    • -XX:PermSize -XX:MaxPermSize
    • 使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM

栈的分配参数

  • 栈大小分配
    • -Xss
      • 通常只有几百K
      • 决定了函数调用的深度
      • 每个线程都有独立的栈空间
      • 局部变量、参数分配在栈上
      public class TestStackDeep {
          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();
              }
          }
      }
      
    结果:
      递归调用
      -Xss128K
      deep of calling = 701
      java.lang.StackOverflowError
    
      -Xss256K
      deep of calling = 1817
      java.lang.StackOverflowError
    

你可能感兴趣的:(3. 常用JVM配置参数)