JVM参数配置测试

参考:http://blog.csdn.net/witsmakemen/article/details/28600127

如果使用Eclipse开发工具,可以使用Debug configurations--->Arguments--->VM arguments 配置 输入图片说明

参数说明见另一篇博文:java虚拟机的原理

测试堆内存初始化

VM arguments配置
-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags

测试类:

 public static void main(String[] args) {
        //-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags

        //查看GC信息
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());

        byte[] b1 = new byte[1 * 1024 * 1024];
        System.out.println("分配了1M");
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());

        byte[] b2 = new byte[4 * 1024 * 1024];
        System.out.println("分配了4M");
        System.out.println("max memory:" + Runtime.getRuntime().maxMemory());
        System.out.println("free memory:" + Runtime.getRuntime().freeMemory());
        System.out.println("total memory:" + Runtime.getRuntime().totalMemory());
    }

输出

-XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
---XX:+PrintCommandLineFlags参数作用
max memory:20316160
free memory:4433040   --初始化5M
total memory:5111808  --heap总大小
[GC [DefNew: 662K->128K(1536K), 0.0040522 secs] 662K->452K(4992K), 0.0041507 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] 
分配了1M
max memory:20316160
free memory:3571336 --消耗掉1M后
total memory:5111808----heap总大小不变
[GC [DefNew: 1180K->0K(1536K), 0.0034828 secs][Tenured: 1476K->1476K(3456K), 0.0055071 secs] 1504K->1476K(4992K), [Perm : 1620K->1620K(12288K)], 0.0091993 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
分配了4M   --此时free memory 不足
max memory:20316160
free memory:3656000 --不变
total memory:9441280 --增加4M

Heap--堆详细信息
 def new generation   total 1664K, used 108K [0x04750000, 0x04910000, 0x04df0000)--新生代
  eden space 1536K,   7% used [0x04750000, 0x0476b108, 0x048d0000)--eden区
  from space 128K,   0% used [0x048d0000, 0x048d0000, 0x048f0000) --from区
  to   space 128K,   0% used [0x048f0000, 0x048f0000, 0x04910000)--to区
 tenured generation   total 7556K, used 5572K [0x04df0000, 0x05551000, 0x05b50000)--老年代
   the space 7556K,  73% used [0x04df0000, 0x05361100, 0x05361200, 0x05551000)
 compacting perm gen  total 12288K, used 1627K [0x05b50000, 0x06750000, 0x09b50000) --方法区
   the space 12288K,  13% used [0x05b50000, 0x05ce6f58, 0x05ce7000, 0x06750000)
No shared spaces configured.

测试配置新生代大小对GC影响

//1、新生代分配1M  如果分配的初始化堆太小,会频繁的引起GC,导致性能下降
-Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

//2、新生代分配7M
-Xms20m -Xmx20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

//3、配置新生代和老生代的比例
//-XX:NewRatio=老年代/新生代
-Xms20m -Xmx20m -XX:NewRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

测试代码

/**如果分配的初始化堆太小,会频繁的引起GC,导致性能下降
**/
public static void main(String[] args) {
        byte[] b = null;
		for(int i = 0 ; i <10; i ++){
			b = new byte[1*1024*1024];//连续向系统申请10MB空间
		}
    }

内存溢出测试

-Xms2m -Xmx2m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/Test03.dump

测试代码:

public static void main(String[] args) {
		//堆内存溢出
		Vector v = new Vector();
		for(int i=0; i < 5; i ++){
			v.add(new Byte[1*1024*1024]);
		}
		
	}

转载于:https://my.oschina.net/u/2283517/blog/1577454

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值