java的虚拟机参数配置及使用

java虚拟参数设置

-XX:+PrintGC   打印gc信息
-XX:+PrintGCDetails    打印gc详细信息
-Xms1980m     初始化堆的大小
-Xmx1980m     最大堆内存大小
-XX:SurvivorRatio=2          eden 和 from\to之间的比例大小 ====>>  eden : from : to = 2 : 1 : 1
-XX:NewRatio=2           新生代与老年代的大小比例   ========>> 新生代 :老年代 = 1 :2
-Xss5m 栈的深度大小
-XX:+HeapDumpOnOutOfMemoryError   //发生gc打印发生gc信息的报告

1.堆溢出
2.栈溢出

1.内存溢出与内存泄漏区别
内存溢出,是在申请内存空间时,超出最大堆内存空间
内存泄漏,使用因为内存空间没有及时释放,长时间导致占用内存,最终导致内存溢出
内存泄漏表现:1.数据库连接长时间连接不释放 2.io操作,不关流 3.定义过多不常用的常量(比如定义过多的static修饰的关键字)

JVM设置使用垃圾收集器的参数

jdk8默认使用的是 parallel收集器

2.JVM设置使用垃圾收集器的参数:-XX:+UseParNewGC

串行的收集器

1.serial收集器
-XX:+UseSerialGC使用serial收集器

/**
 * @Authror royLuo
 * @Date 2020/4/23 22:57
 **/
/**
 * 触发gc才打印下面的信息,当内存不够触发
 * [GC (Allocation Failure) 新生代gc
 * [Full GC (Allocation Failure) 新生代+老年代 的gc
 *使用收集器 -XX:+UseSerialGC
 * */
//配置堆内存大小
public class Test001 {

    /**
    * 最大堆内存 -Xmx20m
    * 初始堆内存 -Xms5m
    * 项目一启动就会申请使用5m 即为 初始堆内存大小
    * 当代码里面运行时申请空间+初始堆申请空间 大于  最大堆内存 最报堆内存溢出
    * Byte [] bt =new Byte[15*1024*1024];(申请15m的空间大小)
    * */

    public static void main(String[] args) {
        Byte [] bt =new Byte[10*1024*1024];
        System.out.println("最大内存:"+Runtime.getRuntime().maxMemory()/1024/1024+"M");
        System.out.println("可用内存:"+Runtime.getRuntime().freeMemory()/1024/1024+"M");
        System.out.println("已经使用内存:"+Runtime.getRuntime().totalMemory() /1024/1024+"M");
    }

}

打印gc信息

SerialGC <<<<----------->>>> def new generation

Heap
 def new generation   total 5120K, used 2038K [0x00000000fec00000, 0x00000000ff2a0000, 0x00000000ff2a0000)
  eden space 3456K,  58% used [0x00000000fec00000, 0x00000000fedfd980, 0x00000000fef60000)
  from space 1664K,   0% used [0x00000000fef60000, 0x00000000fef60000, 0x00000000ff100000)
  to   space 1664K,   0% used [0x00000000ff100000, 0x00000000ff100000, 0x00000000ff2a0000)
 tenured generation   total 13696K, used 4096K [0x00000000ff2a0000, 0x0000000100000000, 0x0000000100000000)
   the space 13696K,  29% used [0x00000000ff2a0000, 0x00000000ff6a0010, 0x00000000ff6a0200, 0x0000000100000000)
 Metaspace       used 3140K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
并发的收集器
2.1ParNew收集器是Serial收集器的多线程版本

ParNew收集器的新生代并行(复制算法),老年代串性(标记压缩)
-XX:ParallelGCThreads 限制线程数量

/**
 * @Authror royLuo
 * @Date 2020/4/23 23:19
 **/
/**
* 设置新生代比例参数
 * -XX:SurvivorRatio=2  即为eden:from:to = 2:1:1
 *  eden space 21504K,  65% used [0x0000000082a00000, 0x00000000837cf968, 0x0000000083f00000)
 *   from space 10752K,   0% used [0x0000000083f00000, 0x0000000083f00000, 0x0000000084980000)
 *   to   space 10752K,   0% used [0x0000000084980000, 0x0000000084980000, 0x0000000085400000)
 *  from space 10752K * 2 = 21504k
 *  to   space 10752K * 2 = 21504k
 *
 *  使用收集器 -XX:+UseParNewGC
* */
public class Test002 {

    public static void main(String[] args) {
        byte [] b =null;
        for (int i =0;i<10;i++){
            b = new byte[1024*1024*1];
        }
    }
}

gc的信息打印

ParNewGC<<<<----------->>>> par new generation

Heap
 par new generation   total 32256K, used 14142K [0x0000000082a00000, 0x0000000085400000, 0x00000000ac6a0000)
  eden space 21504K,  65% used [0x0000000082a00000, 0x00000000837cf968, 0x0000000083f00000)
  from space 10752K,   0% used [0x0000000083f00000, 0x0000000083f00000, 0x0000000084980000)
  to   space 10752K,   0% used [0x0000000084980000, 0x0000000084980000, 0x0000000085400000)
 tenured generation   total 86016K, used 0K [0x00000000ac6a0000, 0x00000000b1aa0000, 0x0000000100000000)
   the space 86016K,   0% used [0x00000000ac6a0000, 0x00000000ac6a0000, 0x00000000ac6a0200, 0x00000000b1aa0000)
 Metaspace       used 3197K, capacity 4568K, committed 4864K, reserved 1056768K
  class space    used 339K, capacity 392K, committed 512K, reserved 1048576K
2.2parallel收集器(jdk8默认使用)

XX:+UseParallelGC(jdk8默认使用)

/**
 * @Authror royLuo
 * @Date 2020/4/25 13:22
 **/
/**
 * -XX:+UseParallelGC     parallel收集器
 * **/
public class Test005 {

    public static void main(String[] args) {
        System.out.println("****************");
    }

}

gc信息
Heap
 PSYoungGen      total 37888K, used 3979K [0x00000000d6380000, 0x00000000d8d80000, 0x0000000100000000)
  eden space 32768K, 12% used [0x00000000d6380000,0x00000000d6762ca8,0x00000000d8380000)
  from space 5120K, 0% used [0x00000000d8880000,0x00000000d8880000,0x00000000d8d80000)
  to   space 5120K, 0% used [0x00000000d8380000,0x00000000d8380000,0x00000000d8880000)
 ParOldGen       total 86016K, used 0K [0x0000000082a00000, 0x0000000087e00000, 0x00000000d6380000)
  object space 86016K, 0% used [0x0000000082a00000,0x0000000082a00000,0x0000000087e00000)
 Metaspace       used 3162K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 344K, capacity 388K, committed 512K, reserved 1048576K
2.3cms收集器

XX:+UseConcMarkSweepGC

/**
 * @Authror royLuo
 * @Date 2020/4/24 0:11
 **/
/**
 * -XX:NewRatio=2  新生代 :老年代 = 1 :2
 *    新生代
 *   eden space 34432K,  43% used [0x0000000082a00000, 0x00000000838ba2c0, 0x0000000084ba0000)
 *   from space 4288K,   0% used [0x0000000084ba0000, 0x0000000084ba0000, 0x0000000084fd0000)
 *   to   space 4288K,   0% used [0x0000000084fd0000, 0x0000000084fd0000, 0x0000000085400000)
 *   老年代
 *    tenured generation   total 86016K, used 0K [0x00000000ac6a0000, 0x00000000b1aa0000, 0x0000000100000000)
 *    the space 86016K,   0% used [0x00000000ac6a0000, 0x00000000ac6a0000, 0x00000000ac6a0200, 0x00000000b1aa0000)
 *     计算:
 *     (  eden+ from+ to ) * 2 = tenured generation
 *
 *     使用的收集器:-XX:+UseConcMarkSweepGC   cms收集器
 *  **/
public class Test003 {

    public static void main(String[] args) {
        byte [] b =null;
        for (int i =0;i<10;i++){
            b = new byte[1024*1024*1];
        }
    }

}
gc的信息打印

par new generation && concurrent mark-sweep generation

Heap
 par new generation   total 38720K, used 14375K [0x0000000082a00000, 0x0000000085400000, 0x00000000ac6a0000)
  eden space 34432K,  41% used [0x0000000082a00000, 0x0000000083809c90, 0x0000000084ba0000)
  from space 4288K,   0% used [0x0000000084ba0000, 0x0000000084ba0000, 0x0000000084fd0000)
  to   space 4288K,   0% used [0x0000000084fd0000, 0x0000000084fd0000, 0x0000000085400000)
 concurrent mark-sweep generation total 86016K, used 0K [0x00000000ac6a0000, 0x00000000b1aa0000, 0x0000000100000000)
 Metaspace       used 3309K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 355K, capacity 388K, committed 512K, reserved 1048576K
2.4g1收集器

XX:+UseG1GC

/**
 * @Authror royLuo
 * @Date 2020/4/25 12:09
 **/

/**
 * -Xss5m  栈的大小设置
 * 默认10000+的栈深度
 *
 * 收集器:-XX:+UseG1GC     g1收集器
 **/
public class Test004 {

   private static int count;

    public static void count() {
        try {
            count++;
//            count();

        } catch (Throwable e) {  //要最高异常才能捕获到
            System.out.println("最大深度"+count);
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        count();

    }
}
gc的信息打印

garbage-first heap

Heap
 garbage-first heap   total 129024K, used 2048K [0x0000000082a00000, 0x000000008a800000, 0x0000000100000000)
  region size 1024K, 3 young (3072K), 0 survivors (0K)
 Metaspace       used 3195K, capacity 4568K, committed 4864K, reserved 1056768K
  class space    used 339K, capacity 392K, committed 512K, reserved 1048576K

3.结论

1.垃圾回收次数和设置堆内存大小无关,只和初始内存存在关系
2.初始内存会影响吞吐量
3.堆的初始值和最大值一定要一致,并且初始堆越大吞吐量就会越高

jdk可视化工具

1.jconsole

2.visualVm

可以用于检测死锁

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值