計算佔用內存大小的方法

public   class   T   
  
{   
  
public   static   void   main(String[]   args)   throws   Exception   
  
{   
  
//   創建   1000   個   String   
  String   strA[]   =   new   String[1000];   
  
long   start   =   0;   
  
long   end   =   0;   
    
  
//   先垃圾回收   
  System.gc();   
    
  start   
=   Runtime.getRuntime().freeMemory();   
  
for   (int   i   =   0;   i   <   1000;   i   ++)   
  strA[i]   
=   new   String();   
    
  
//   快要計算的時,再清理一次   
  System.gc();   
    
  end   
=   Runtime.getRuntime().freeMemory();   
    
  System.out.println(
"一個String對象佔內存:"   +   (start   -   end)/1000.0);   
    
  }
   
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章