gc()测试

public class TestGC {

  /**
   * @param args
   * @author strong
   * 2011-7-18_下午04:35:28
   */
  public static void main(String[] args) {
    int count =1000000;

    long total1 = Runtime.getRuntime().totalMemory();
    long starttime1 = System.currentTimeMillis();
    for(int i=0;i<count;i++){
      String d = "dddddddddddddd";
      System.out.println(d);
    }
    long free1 =Runtime.getRuntime().freeMemory();
    long endtime1 = System.currentTimeMillis();
    Runtime.getRuntime().gc();//如果放在循环中将需要时间比较长。11ms/9094ms
   // System.gc();
    long free2 =Runtime.getRuntime().freeMemory();
    long endtime2 = System.currentTimeMillis();
    System.out.println(count+"次---gc前消耗内存:"+(total1-free1)/1024+"KB");
    System.out.println(count+"次---gc消耗内存:"+(total1-free2)/1024+"KB");
    System.out.println(count+"次---gc前消耗的时间:"+(endtime1-starttime1)+"ms");
    System.out.println(count+"次---gc消耗的时间:"+(endtime2-endtime1)+"ms");
    
    System.out.println(count+"次---消耗的时间:"+(endtime2-starttime1)+"ms");

  }

}
 
100次---gc前消耗内存:317KB
100次---gc消耗内存:134KB
100次---gc前消耗的时间:3ms
100次---gc消耗的时间:12ms
100次---消耗的时间:15ms

100次---gc前消耗内存:317KB
100次---gc消耗内存:134KB
100次---gc前消耗的时间:7ms
100次---gc消耗的时间:11ms
100次---消耗的时间:18ms


1000次---gc前消耗内存:341KB
1000次---gc消耗内存:134KB
1000次---gc前消耗的时间:106ms
1000次---gc消耗的时间:16ms
1000次---消耗的时间:122ms

1000次---gc前消耗内存:341KB
1000次---gc消耗内存:134KB
1000次---gc前消耗的时间:122ms
1000次---gc消耗的时间:10ms
1000次---消耗的时间:132ms

10000次---gc前消耗内存:1453KB
10000次---gc消耗内存:134KB
10000次---gc前消耗的时间:545ms
10000次---gc消耗的时间:12ms
10000次---消耗的时间:557ms

10000次---gc前消耗内存:1463KB
10000次---gc消耗内存:134KB
10000次---gc前消耗的时间:571ms
10000次---gc消耗的时间:30ms
10000次---消耗的时间:601ms

100000次---gc前消耗内存:11251KB
100000次---gc消耗内存:134KB
100000次---gc前消耗的时间:1839ms
100000次---gc消耗的时间:17ms
100000次---消耗的时间:1856ms

100000次---gc前消耗内存:11251KB
100000次---gc消耗内存:134KB
100000次---gc前消耗的时间:1990ms
100000次---gc消耗的时间:11ms
100000次---消耗的时间:2001ms

1000000次---gc前消耗内存:12275KB
1000000次---gc消耗内存:-2231KB
1000000次---gc前消耗的时间:20161ms
1000000次---gc消耗的时间:10ms
1000000次---消耗的时间:20171ms

1000000次---gc前消耗内存:12275KB
1000000次---gc消耗内存:-2231KB
1000000次---gc前消耗的时间:20724ms
1000000次---gc消耗的时间:16ms
1000000次---消耗的时间:20740ms
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值