常用Java虚拟机参数1

1、跟踪垃圾回收

(1)-XX:+PrintGC  只要遇到GC就会打印日志

(2)-XX:+PrintGCDetails 详细信息

(3)-XX:+PrintHeapAtGC 全面的堆信息  还显示老年区(tenured generation)和永久区 (compacting perm gen)

(4)-XX:+PrintGCTimeStamps参数  在每次GC发生时,额外输出GC发生的时间,该输出时间为虚拟机启动后的时间偏移量。

(5)-XX:+PrintGCApplicationConcurrentTime  打印应用程序的执行时间

(6)-XX:+PrintGCApplicationStoppedTime 打印应用程序由于GC而产生的停顿时间

(7)-XX:+PrintReferenceGC 跟踪系统内的软引用、弱引用、虚引用和Finallize队列

(8) -Xloggc:log/gc.log  在项目空间下的log/gc.log


[GC (Allocation Failure) [PSYoungGen: 35128K->1832K(34816K)] 73387K->40099K(122368K), 0.0040158 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 34600K->1880K(34304K)] 72867K->40147K(121856K), 0.0026985 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 34136K->128K(32256K)] 72403K->40232K(119808K), 0.0032815 secs] [Times: user=0.05 sys=0.01, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 31872K->96K(32256K)] 71976K->40224K(119808K), 0.0008551 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 31328K->96K(33280K)] 71456K->40248K(120832K), 0.0007393 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 30816K->64K(32768K)] 70968K->40224K(120320K), 0.0077761 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 


Hotspot JVM GC 垃圾收集器的默认默认开启的组合为:


  1. 使用GC日志命令行选项为:

    -XX:+PrintGCTimeStamps

    -XX:+PrintGCDetails

    -Xloggc:<filename>

  2. JVM 【-server】

    glassfish应用服务器  -server 启动 垃圾收集器默认组合方式为

    新生代:Parallel Scavenge 并行回收GC

    年老代和持久代:Parallel Old并行GC

  3. GC日志打印信息

    -XX:+PrintGCTimeStamps输出格式:

    289.556: [GC [PSYoungGen: 314113K->15937K(300928K)] 405513K->107901K(407680K), 0.0178568 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 

    293.271: [GC [PSYoungGen: 300865K->6577K(310720K)] 392829K->108873K(417472K), 0.0176464 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 

    详解:

    293.271是从jvm启动直到垃圾收集发生所经历的时间,GC表示这是一次Minor GC(新生代垃圾收集);[PSYoungGen: 300865K->6577K(310720K)] 提供了新生代空间的信息,PSYoungGen,表示新生代使用的是多线程垃圾收集器Parallel Scavenge。300865K表示垃圾收集之前新生代占用空间,6577K表示垃圾收集之后新生代的空间。新生代又细分为一个Eden区和两个Survivor区,Minor GC之后Eden区为空,6577K就是Survivor占用的空间。

    括号里的310720K表示整个年轻代的大小。

    392829K->108873K(417472K),表示垃圾收集之前(392829K)与之后(108873K)Java堆的大小(总堆417472K,堆大小包括新生代和年老代)

    由新生代和Java堆占用大小可以算出年老代占用空间,如,Java堆大小417472K,新生代大小310720K那么年老代占用空间是417472K-310720K=106752k;垃圾收集之前老年代占用的空间为392829K-300865K=91964k 垃圾收集之后老年代占用空间108873K-6577K=102296k.

    0.0176464 secs表示垃圾收集过程所消耗的时间。

     [Times: user=0.06 sys=0.00, real=0.01 secs] 提供cpu使用及时间消耗,user是用户模式垃圾收集消耗的cpu时间,实例中垃圾收集器消耗了0.06秒用户态cpu时间,sys是消耗系统态cpu时间,real是指垃圾收集器消耗的实际时间。

  4. -XX:+PrintGCDetails输出格式:

    293.289: [Full GC [PSYoungGen: 6577K->0K(310720K)] 

    [PSOldGen: 102295K->102198K(134208K)] 108873K->102198K(444928K) 

    [PSPermGen: 59082K->58479K(104192K)], 0.3332354 secs] 

    [Times: user=0.33 sys=0.00, real=0.33 secs] 

    说明:

    Full GC表示执行全局垃圾回收

    [PSYoungGen: 6577K->0K(310720K)] 提供新生代空间信息,解释同上

    [PSOldGen: 102295K->102198K(134208K)]提供了年老代空间信息;

    108873K->102198K(444928K)整个堆空间信息;

    [PSPermGen: 59082K->58479K(104192K)]提供了持久代空间信息;

  5. JVM【-client】

    glassfish应用服务器 默认 -client启动 垃圾收集器默认组合方式为

    新生代:Serial串行GC  (新生代使用-XX:+UseSerialGC收集器)

    年老代和持久代:Serial Old 串行GC

  6. GC日志打印信息:

     

    -XX:+PrintGCTimeStamps输出格式:

    9.007: [GC 9.007: [DefNew: 13526K->1060K(14400K), 0.0041350 secs] 44692K->32226K(46108K), 0.0041767 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 

    9.259: [GC 9.259: [DefNew: 13924K->1089K(14400K), 0.0042760 secs] 45090K->32650K(46108K), 0.0043143 secs] [Times: user=0.02 sys=0.00, real=0.02 secs] 

    说明:

    DefNew表示新生代使用Serial串行GC垃圾收集器,defNew提供新生代空间信息;

    DefNewGeneration 就是 default new generation

  7. -XX:+PrintGCDetails输出格式:

    9.348: [GC 9.348: [DefNew: 13953K->976K(14400K), 0.0040943 secs]9.352: [Tenured: 32163K->32220K(32220K), 0.1182207 secs] 45514K->32338K(46620K), [Perm : 30467K->30467K(65536K)], 0.1224318 secs] [Times: user=0.12 sys=0.00, real=0.13 secs] 

    DefNew:表示新生代使用Serial串行GC垃圾收集器,defNew提供新生代空间信息;

    Tenured:提供年老代空间信息;

    45514K->32338K(46620K):整个堆空间大小信息;

    Perm :提供持久代空间信息;

2、类加载、卸载的跟踪

(1)-verbose:class     跟踪类的加载和卸载,也可以

(2)-XX:+TraceClassLoading  单独使用 跟踪类的加载

(3)-XX:+TraceClassUnloading  跟踪累的卸载  

(4)-XX:+PrintVMOptions  在程序运行时,打印虚拟机受到的命令行显式参数

VM option '+PrintClassHistogram'
VM option '+PrintVMOptions'
VM option '+PrintCommandLineFlags'


(5)-XX:+PrintCommandLineFlags  打印传递给虚拟机的显式和隐式参数

-XX:InitialHeapSize=132529216 -XX:MaxHeapSize=2120467456 -XX:+PrintClassHistogram -XX:+PrintCommandLineFlags -XX:+PrintVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 


(6)-XX:+PrintFlagsFinal   打印所有的系统参数的值

3、堆的配置参数

(1)最大堆空间 -Xmx20m 

(2)最小堆空间-Xms5m

     在实际工作中,也可以直接将初始-Xms与最大堆-Xmx设置相等。这样的好处是可以减少程序运行时进行垃圾回收次数,从而提高程序的性能。

/**
 * -Xmx20m -Xms5m -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseSerialGC
 * @author Geym
 *
 */
public class HeapAlloc {
    public static void main(String[] args) {
        System.out.print("maxMemory=");
        System.out.println(Runtime.getRuntime().maxMemory()+" bytes");
        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()+" bytes");
        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()+" bytes");
        
        byte[] b=new byte[1*1024*1024];
        System.out.println("分配了1M空间给数组");
        
        System.out.print("maxMemory=");
        System.out.println(Runtime.getRuntime().maxMemory()+" bytes");
        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()+" bytes");
        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()+" bytes");
        
        b=new byte[4*1024*1024];
        System.out.println("分配了4M空间给数组");
        
        System.out.print("maxMemory=");
        System.out.println(Runtime.getRuntime().maxMemory()+" bytes");
        System.out.print("free mem=");
        System.out.println(Runtime.getRuntime().freeMemory()+" bytes");
        System.out.print("total mem=");
        System.out.println(Runtime.getRuntime().totalMemory()+" bytes");
    }
}

-XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
maxMemory=20316160 bytes
free mem=5251048 bytes
total mem=6094848 bytes
[GC (Allocation Failure) [DefNew: 824K->192K(1856K), 0.0012302 secs] 824K->515K(5952K), 0.0012630 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
分配了1M空间给数组
maxMemory=20316160 bytes
free mem=4483944 bytes
total mem=6094848 bytes
[GC (Allocation Failure) [DefNew: 1249K->0K(1856K), 0.0010233 secs][Tenured: 1539K->1539K(4096K), 0.0013594 secs] 1573K->1539K(5952K), [Metaspace: 2556K->2556K(1056768K)], 0.0024208 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
分配了4M空间给数组
maxMemory=20316160 bytes
free mem=4552728 bytes
total mem=10358784 bytes
Heap
 def new generation   total 1920K, used 51K [0x00000000fec00000, 0x00000000fee10000, 0x00000000ff2a0000)
  eden space 1728K,   2% used [0x00000000fec00000, 0x00000000fec0cec0, 0x00000000fedb0000)
  from space 192K,   0% used [0x00000000fedb0000, 0x00000000fedb0000, 0x00000000fede0000)
  to   space 192K,   0% used [0x00000000fede0000, 0x00000000fede0000, 0x00000000fee10000)
 tenured generation   total 8196K, used 5635K [0x00000000ff2a0000, 0x00000000ffaa1000, 0x0000000100000000)
   the space 8196K,  68% used [0x00000000ff2a0000, 0x00000000ff820fb8, 0x00000000ff821000, 0x00000000ffaa1000)
 Metaspace       used 2563K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 274K, capacity 386K, committed 512K, reserved 1048576K

4、新生代的配置

(1)-Xmn1m  设置 新生代的大小   新生代的大小一般设置为整个堆空间的1/3 到1/4左右

(2)-XX:SurvivorRatio 用来设置新生代中eden空间和from/to空间的比例关系

设置eden区与survivor区的比例

-XX:SurvivorRatio=eden/from=eden/to

(3)-XX:NewRatio=老年代/新生代

设置老年代与新生代 的比例

/**
 * -Xmx20m -Xms20m -Xmn1m   -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
 * -Xmx20m -Xms20m -Xmn7m   -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC
 * -Xmx20m -Xms20m -Xmn15m  -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+UseSerialGC
 * 
 * -Xmx20M -Xms20M -XX:NewRatio=2  -XX:+PrintGCDetails
 * 
 * @author Geym
 */
public class NewSizeDemo {
    public static void main(String[] args) {
       byte[] b=null;
       for(int i=0;i<10;i++)
           b=new byte[1*1024*1024];
    }
}
  -Xmx20m -Xms20m -Xmn1m   -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

[GC (Allocation Failure) [DefNew: 512K->255K(768K), 0.0010323 secs] 512K->428K(20224K), 0.0010723 secs] [Times: user=0.02 sys=0.00, real=0.00 secs] 
Heap
 def new generation   total 768K, used 496K [0x00000000fec00000, 0x00000000fed00000, 0x00000000fed00000)
  eden space 512K,  46% used [0x00000000fec00000, 0x00000000fec3c158, 0x00000000fec80000)
  from space 256K,  99% used [0x00000000fecc0000, 0x00000000fecffff8, 0x00000000fed00000)
  to   space 256K,   0% used [0x00000000fec80000, 0x00000000fec80000, 0x00000000fecc0000)
 tenured generation   total 19456K, used 10412K [0x00000000fed00000, 0x0000000100000000, 0x0000000100000000)
   the space 19456K,  53% used [0x00000000fed00000, 0x00000000ff72b0a8, 0x00000000ff72b200, 0x0000000100000000)
 Metaspace       used 2560K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 274K, capacity 386K, committed 512K, reserved 1048576K


  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值