Garbage Collection 一直在执行? 还是需要时才启动?

来源:互联网 发布:查看端口号是否被占用 编辑:程序博客网 时间:2024/05/21 10:39
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  (作者 : 艾群科技 萧松瀛)
  
  内存对於 Java 来说,往往是非常珍贵的资源,虽然现在的内存很
便宜,随随便便几百 MB 不过几千块台币,但是如果我们能够掌握一些记
忆体的状况,那么就不会写出 memory monster 的程式,我想,如果您使
用 J2ME 或是其他内存不够的周边,那么我想你就可以深深的体会出这
句话的意义。而 Java 与内存,我们不得不再度提到 Garbage Collect
ion ,事实上,许多人可能会以为 Garbage Collection 是有需要的时候
执行,实际上并不是这样,需要记住的是  Garbage Collection (以下
简称  GC) 是另外一个执行绪在执行的动作,也就是当你程式在执行的同
时,实际上  GC 也一直在监测目前的各种状况。
  
 想要了解这个,我们可以先从 java (java.exe) 的参数来下手。当我们
下 java -help 之後,可以看到  -verbose[:class|gc|jni] 这个参数,
没错,这个参数可以让我们看到一些更详细的状况。首先,我写了底下这
个程式 
  
  public class testGC
  {
      public static void main(String argv[])
      {
          StringBuffer sb = new StringBuffer();
          int MAX=Integer.parseInt(argv[0]);
          for(int i = 0 ; i &lt;=MAX ; i++)
          {
             sb.append(&quot;test&quot;).append(i).append(&quot;
 test&quot;);
              if(i % (MAX/2)== 0)
              {
                 System.out.println(&quot;===Starting System.gc(
 )===&quot;);
                  System.gc();
                 System.out.println(&quot;===Finishing System.gc
 ()===&quot;);
              }
          }
      }
  }
  
   在 Compile 结束之後,我直接用 java testGC 20000,结果如下:
  
  C:javamp&gt; java testGC 20000
  ===Starting System.gc()===
  ===Finishing System.gc()===
  ===Starting System.gc()===
  ===Finishing System.gc()===
  ===Starting System.gc()===
  ===Finishing System.gc()===
  
   但是现在我们加上另外一个参数  -verbose:gc ,让命令列成为 
  
  java -verbose:gc testGC 20000 结果如下:
  
  C:javamp&gt; java -verbose:gc testGC 20000
  ===Starting System.gc()===
 [GC[0: 367K-&gt; 190K(1984K)][1: 190K-&gt; 190K(1984K)], 0.0454
 848 secs]
  ===Finishing System.gc()===
  [GC[0: 1206K-&gt; 694K(1984K)], 0.0009384 secs]
  ===Starting System.gc()===
 [GC[0: 879K-&gt; 694K(1984K)][1: 694K-&gt; 478K(1984K)], 0.0336
 423 secs]
  ===Finishing System.gc()===
  [GC[0: 1565K-&gt; 1054K(1984K)], 0.0006216 secs]
  ===Starting System.gc()===
 [GC[0: 1167K-&gt; 1054K(1984K)][1: 1054K-&gt; 766K(1984K)], 0.0
 375078 secs]
  ===Finishing System.gc()===
  
  以 [ 开头, ] 结尾的是 GC 的执行状况,表示的确,在 System.gc()
  後,他有执行,我们可以看到他很均匀的插在我们的每一行输出中间,
没关系,我们把参数由  20000 调到 50000,再执行一次,结果如下
  
  C:javamp&gt; java -verbose:gc testGC 50000
  ===Starting System.gc()===
 [GC[0: 367K-&gt; 190K(1984K)][1: 190K-&gt; 190K(1984K)], 0.0457
 804 secs]
  ===Finishing System.gc()===
  [GC[0: 1206K-&gt; 694K(1984K)], 0.0009297 secs]
  [GC[0: 1782K-&gt; 1270K(1984K)], 0.0005685 secs]
  [GC[0: 1782K-&gt; 1270K(1984K)], 0.0006135 secs]
  [GC[1: 1277K-&gt; 766K(1984K)], 0.0353595 secs]
  ===Starting System.gc()===
 [GC[0: 2009K-&gt; 1918K(3140K)][1: 1918K-&gt; 1342K(3140K)], 0.
 0435416 secs]
  ===Finishing System.gc()===
  [GC[0: 1853K-&gt; 1342K(3140K)], 0.0005931 secs]
  [GC[0: 1854K-&gt; 1342K(3140K)], 0.0006395 secs]
  [GC[1: 1644K-&gt; 1341K(3140K)], 0.0480494 secs]
  ===Starting System.gc()===
 [GC[0: 3881K-&gt; 3645K(5448K)][1: 3645K-&gt; 2493K(5448K)], 0.
 0462025 secs]
  ===Finishing System.gc()===
  
 这时候很明显的,会发现,即使不执行 System.gc(); 也会看到 [GC 开
头的字眼,这表示  GC 会随时的监测目前的状况。 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击