`
ninggy
  • 浏览: 112894 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics