少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          這些日子要用爪哇語言(Java)做內(nèi)存數(shù)據(jù)中心。于是把用 Java 監(jiān)控運行環(huán)境硬件資源的內(nèi)容復習了一下。爪哇類庫提供了 java.util.Runtim 類,主要負責調(diào)用爪哇虛擬機(JavaVM)外部的基層操作系統(tǒng)功能、處理基于一種叫鉤子的原理的程序、獲取系統(tǒng)資源信息以及控制調(diào)試信息生成。本文單獨利用其獲取系統(tǒng)資源信息的功能。

          java.util.Runtim 類具有以下幾個方法和獲取系統(tǒng)資源信息有關(guān)。以下代碼可不是簡簡單單從標準類庫里邊復制出來的哦。全球目前獨此一份。

          /**
           * 返回爪哇(Java)虛擬機可用線程數(shù)。
           *
           * <p>該值在特定的虛擬機調(diào)用期間可能發(fā)生更改。因此,對可用處理器數(shù)目很敏感的
           * 應用程序應該不定期地輪詢該屬性,并相應地調(diào)整其資源用法。</p>
           *
           * 
          @return  虛擬機可用的最大處理器數(shù)目;從不小于 1
           * 
          @since 1.4
           
          */

          public native int availableProcessors();

          /**
           * 返回爪哇(Java)虛擬機中的空閑內(nèi)存量。調(diào)用 <code>gc</code> 方法可能導致
           * <code>freeMemory</code> 返回值的增加。
           *
           * 
          @return  供將來分配對象使用的當前可用內(nèi)存的近似總量,以字節(jié)為單位。
           
          */

          public native long freeMemory();

          /**
           * 返回爪哇(Java)虛擬機中的內(nèi)存總量。此方法返回的值可能隨時間的推移而變化,這
           * 取決于主機環(huán)境。
           * <p>
           * 注意,保存一個給定類型的對象所需的內(nèi)存量可能取決于實現(xiàn)方式。
           *
           * 
          @return  目前為當前和后續(xù)對象提供的內(nèi)存總量,以字節(jié)為單位。
           
          */

          public native long totalMemory();           //                                //

          /**
           * 返回爪哇(Java)虛擬機能夠嘗試使用的最大內(nèi)存量。如果內(nèi)存本身沒有限制,則
           * 返回值 {
          @link java.lang.Long#MAX_VALUE} 。 </p>
           *
           * 
          @return  虛擬機能夠嘗試使用的最大內(nèi)存量,以字節(jié)為單位。
           * 
          @since 1.4
           
          */

          public native long maxMemory();

          /**
           * 運行垃圾回收器。
           * 調(diào)用此方法意味著爪哇(Java)虛擬機做了一些努力來回收未用對象,以便能夠快速地
           * 重用這些對象當前占用的內(nèi)存。當控制從方法調(diào)用中返回時,虛擬機已經(jīng)盡最大努力回收
           * 了所有丟棄的對象。
           * <p>
           * 名稱 <code>gc</code> 代表“垃圾回收器”。虛擬機根據(jù)需要在單獨的線程中自動執(zhí)行
           * 回收過程,甚至不用顯式調(diào)用 <code>gc</code> 方法。
           * <p>
           * 方法 {
          @link System#gc()} 是調(diào)用此方法的一種傳統(tǒng)而便捷的方式
           
          */

          public native void gc();

           








          我們可以看到這些都是本地方法。這意味著將 Runtime 對象遠程傳遞之后,將不能得到正確執(zhí)行結(jié)果。
          這些方法用起來都很簡單,文檔注釋也寫得比較明白。
          在高可用數(shù)據(jù)中心中,我認為應該根據(jù)可用 CPU 線數(shù)決定程序開啟的線程數(shù) 。此線程數(shù)為 CPU 可用線數(shù)的某倍數(shù)。此倍數(shù)應通過實際經(jīng)驗所得。然后程序通過監(jiān)控 CPU 可用線數(shù),來控制線程池保留數(shù)量。
          內(nèi)存的控制,我想應該是在內(nèi)存超出警戒線時發(fā)出警報 ,以向運營人員申請增加內(nèi)存數(shù)據(jù)中心服務器。同時,應該在內(nèi)存過滿之前,由程序執(zhí)行垃圾回收 ,以消除并無引用的老生代對象。
          如果各位有對內(nèi)存數(shù)據(jù)中心的想法、建議或者質(zhì)疑,歡迎來一起討論。
          下邊是我編寫的一個系統(tǒng)資源測試程序。程序里邊使用了 Runtime 類關(guān)于系統(tǒng)資源的所有方法。

          package cn.spads.test.grammar;

          import java.util.LinkedList;
          import java.util.Random;

          /**
           * 本類用于示范使用 Runtime 檢查系統(tǒng)運行情況。
           * 將 Runtime 作為可變成員,是為多系統(tǒng)公用檢查預留的設(shè)計。
           * 
          @author    Shane Loo Li
           
          */

          public class PerformanceMonitor
          {
              
          /**
               * 此量控制程序運行時間。此值越大,此演示程序運行時間越長。
               
          */

              
          static public int runLoopTimes = 55;

              
          /**
               * 此為每次檢測間隔的時間片數(shù)。此值越大,間隔時間越長。
               
          */

              
          static public int waitTime = 1500000;

              
          static public void main(String[] arguments) throws Exception
              
          {
                  Runtime context 
          = Runtime.getRuntime();
                  
          final PerformanceMonitor monitor = new PerformanceMonitor(context);
                  
          final LinkedList<String> pretendedMemory = new LinkedList<String>();
                  
          new Thread(
                          
          new Runnable()
                          
          {
                              
          public void run()
                              
          {
                                  
          for (int j = -1++!= runLoopTimes; )
                                  
          {
                                      
          // 檢查系統(tǒng)情況
                                      monitor.checkAll();

                                      
          // 每次檢查運行情況之后,都會休息 1000 個時間片
                                      for (int i = -1++!= waitTime; ) Thread.yield();

                                      
          // 每次檢查之后,會制造一些對象,記錄其中一部分,并刪除些老對象
                                      for (int i = -1++!= 20000; )
                                      
          {
                                          StringBuilder builder 
          = new StringBuilder();
                                          Random ran 
          = new Random();
                                          
          for (int index = -1++index != 100; )
                                              builder.append((
          char) (ran.nextInt(26+ 64));
                                          String garbage 
          = new String(builder.toString());
                                          garbage 
          = garbage.substring(garbage.length());
                                          pretendedMemory.add(builder.toString());
                                      }

                                      
          int deleteCount = new Random().nextInt(15000);
                                      
          for (int i = -1++!= deleteCount; )
                                          pretendedMemory.removeFirst();

                                      System.out.println(
          "-----------");
                                  }

                              }

                          }

                      ).start();
              }


              
          private Runtime context;
              
          private double maxFreeMemory;

              
          private long lastFreeMemory;
              
          private long lastTotalMemory;
              
          private long lastMaxMemory;

              
          private double lastMemoryRate;

              
          public PerformanceMonitor(Runtime context)
              
          {
                  
          this.context = context;
              }


              
          public void checkAll()
              
          {
                  
          this.monitorMemory();
                  
          this.monitorCpu();
              }

              
          /**
               * 本方法比較當前空余內(nèi)存與歷史記錄最大空余內(nèi)存的關(guān)系。若空余內(nèi)存過小,則執(zhí)行垃圾回收。
               
          */

              
          public void monitorMemory()
              
          {
                  
          // 求空余內(nèi)存,并計算空余內(nèi)存比起最大空余內(nèi)存的比例
                  long freeMemory = this.context.freeMemory();
                  
          if (freeMemory > this.maxFreeMemory)
                      
          this.maxFreeMemory = Long.valueOf(freeMemory).doubleValue();
                  
          double memoryRate = freeMemory / this.maxFreeMemory;
                  System.out.println(
          "There are " + memoryRate * 100 + "% free memory.");

                  
          // 如果內(nèi)存空余率在變小,則一切正常;否則需要報告內(nèi)存變化情況
                  if (memoryRate >= this.lastMemoryRate) this.reportMemoryChange();

                  
          // 如果內(nèi)存空余率很低,則執(zhí)行內(nèi)存回收
                  if (freeMemory / this.maxFreeMemory < 0.3)
                  
          {
                      System.out.print(
          "System will start memory Garbage Collection.");
                      System.out.println(
          " Now we have " + freeMemory / 1000 + " KB free memory.");
                      
          this.context.gc();
                      System.out.println(
          "After the Garbage Collection, we have "
                              
          + this.context.freeMemory() / 1000 + " KB free memory.");
                  }


                  
          // 記錄內(nèi)存信息
                  this.recordMemoryInfo(memoryRate);
              }


              
          /**
               * 報告內(nèi)存變化情況
               
          */

              
          private void reportMemoryChange()
              
          {
                  System.out.print(
          "Last freeMemory = " + this.lastFreeMemory / 1000 + " KB,");
                  System.out.println(
          " now it is " + this.context.freeMemory() / 1000 + " KB.");
                  System.out.print(
          "Last totalMemory = " + this.lastTotalMemory / 1000 + " KB,");
                  System.out.println(
          " now it is " + this.context.totalMemory() / 1000 + " KB.");
                  System.out.print(
          "Last maxMemory = " + this.lastMaxMemory / 1000 + " KB,");
                  System.out.println(
          " now it is " + this.context.maxMemory() / 1000 + " KB.");
              }


              
          /**
               * 記錄本次內(nèi)存信息。
               
          */

              
          private void recordMemoryInfo(double memoryRate)
              
          {
                  
          this.lastFreeMemory = this.context.freeMemory();
                  
          this.lastMaxMemory = this.context.maxMemory();
                  
          this.lastTotalMemory = this.context.totalMemory();
                  
          this.lastMemoryRate = memoryRate;
              }


              
          /**
               * 監(jiān)測 CPU 的方法。
               
          */

              
          public void monitorCpu()
              
          {
                  
          int cpuCount = this.context.availableProcessors();
                  
          if (cpuCount > 1) System.out.println("CPU have " + cpuCount + " processors.");
              }

          }





          我運行這段程序,得到結(jié)果報告。通過觀察結(jié)果報告,我得到了一些新結(jié)論。
          首先, maxMemory 在一臺機器只運行一個爪哇(Java)虛擬機的前提下,一般來說并不會變動。
          其次,新生代內(nèi)存會很快地自動回收,這體現(xiàn)在 freeMemory 總是不斷少量自動增加上。
          最后,爪哇(Java)虛擬機會在內(nèi)存不足時,自己申請新的內(nèi)存。這個內(nèi)存空間也能夠根據(jù)報告大概看出一點原則:既不是完全通過可用內(nèi)存比例,也不是完全通過可用內(nèi)存數(shù)量。

          我自己運行的報告附在下邊

          There are 100.0% free memory.
          Last freeMemory 
          = 0 KB, now it is 124371 KB.
          Last totalMemory 
          = 0 KB, now it is 126353 KB.
          Last maxMemory 
          = 0 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          79.91675736339026% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          82.1353751773145% free memory.
          Last freeMemory 
          = 99921 KB, now it is 102695 KB.
          Last totalMemory 
          = 126353 KB, now it is 126353 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          100.0% free memory.
          Last freeMemory 
          = 102695 KB, now it is 140522 KB.
          Last totalMemory 
          = 126353 KB, now it is 159383 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          82.4930651724349% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          65.90519105111919% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          88.50612783993465% free memory.
          Last freeMemory 
          = 92611 KB, now it is 124371 KB.
          Last totalMemory 
          = 159383 KB, now it is 159383 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          71.68576727159902% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          54.86540670326339% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          100.0% free memory.
          Last freeMemory 
          = 77098 KB, now it is 172330 KB.
          Last totalMemory 
          = 159383 KB, now it is 225443 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          86.18976806966614% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          72.37916940114857% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          58.56890497502629% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          46.292794574206056% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          92.98754812452182% free memory.
          Last freeMemory 
          = 79776 KB, now it is 160245 KB.
          Last totalMemory 
          = 225443 KB, now it is 225443 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          79.17694945600425% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          65.36668502988196% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          51.556035296554015% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          37.745845146519564% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          23.935246478001996% free memory.
          System will start memory Garbage Collection. Now we have 
          41247 KB free memory.
          After the Garbage Collection, we have 
          312897 KB free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          100.0% free memory.
          Last freeMemory 
          = 312897 KB, now it is 292267 KB.
          Last totalMemory 
          = 380108 KB, now it is 380108 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          91.1770148111291% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          84.11856206174096% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          75.29548107031924% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          68.2370940141088% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          59.414100613590705% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          52.35564786420257% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          43.53256687278083% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          34.70958168390994% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          27.6511289345218% free memory.
          System will start memory Garbage Collection. Now we have 
          80815 KB free memory.
          After the Garbage Collection, we have 
          281843 KB free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          89.37604181852511% free memory.
          Last freeMemory 
          = 281843 KB, now it is 261217 KB.
          Last totalMemory 
          = 380108 KB, now it is 380108 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          80.55442250030752% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          73.49712485596086% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          64.67547542837015% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          57.618177784023494% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          48.79652835643279% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          41.739230712086126% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          32.91758128449542% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          24.095931856904713% free memory.
          System will start memory Garbage Collection. Now we have 
          70424 KB free memory.
          After the Garbage Collection, we have 
          258135 KB free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          81.32306278893708% free memory.
          Last freeMemory 
          = 258135 KB, now it is 237681 KB.
          Last totalMemory 
          = 380108 KB, now it is 380108 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          72.5752415442342% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          65.57687068029719% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          56.828918049238894% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          49.83056634581206% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          41.08271772895181% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          32.33487732373877% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          25.33650645980177% free memory.
          System will start memory Garbage Collection. Now we have 
          74050 KB free memory.
          After the Garbage Collection, we have 
          229217 KB free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          71.73676393326296% free memory.
          Last freeMemory 
          = 229217 KB, now it is 209663 KB.
          Last totalMemory 
          = 380108 KB, now it is 380108 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------
          There are 
          63.37379248412019% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          55.01088399093938% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          46.64788243242348% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          38.28488087390758% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          31.59450152482077% free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          23.231593031639967% free memory.
          System will start memory Garbage Collection. Now we have 
          67898 KB free memory.
          After the Garbage Collection, we have 
          203384 KB free memory.
          CPU have 
          8 processors.
          -----------
          There are 
          61.86563040788292% free memory.
          Last freeMemory 
          = 203384 KB, now it is 180813 KB.
          Last totalMemory 
          = 380108 KB, now it is 380108 KB.
          Last maxMemory 
          = 1875378 KB, now it is 1875378 KB.
          CPU have 
          8 processors.
          -----------




          我最近(2012-11-6 Tuesday 至 2012-11-20 Tuesday)參加了一個博客大賽。歡迎來給我投一票~
          投票地址: http://blog.51cto.com/contest2012/5523233 每個 IP 每天可以投一票哦~

          本文也發(fā)表在我的其他空間。
          CSDN : http://blog.csdn.net/shanelooli/article/details/8176938
          開源中國: http://my.oschina.net/shane1984/blog/88803
          51CTO : http://shanelooli.blog.51cto.com/5523233/1058490



          http://www.iteye.com/topic/1127731




















          posted on 2012-11-14 13:01 abin 閱讀(514) 評論(0)  編輯  收藏 所屬分類: JVM
          主站蜘蛛池模板: 轮台县| 正定县| 渭源县| 枣庄市| 铜川市| 聂拉木县| 兴业县| 华容县| 遂溪县| 绥芬河市| 莱阳市| 濮阳市| 来宾市| 盐边县| 拉萨市| 池州市| 安徽省| 鱼台县| 平阳县| 循化| 潢川县| 来安县| 平原县| 潍坊市| 甘德县| 平阴县| 桃园县| 花垣县| 巨鹿县| 新疆| 吴忠市| 双城市| 淮阳县| 镇巴县| 罗城| 宣化县| 蓬莱市| 新化县| 隆昌县| 浠水县| 宜良县|