<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

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

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

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

    public native int availableProcessors();

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

    public native long freeMemory();

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

    public native long totalMemory();           //                                //

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

    public native long maxMemory();

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

    public native void gc();

     








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

    package cn.spads.test.grammar;

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

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

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

        
    static public int runLoopTimes = 55;

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

        
    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; )
                            
    {
                                
    // 檢查系統情況
                                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();
        }

        
    /**
         * 本方法比較當前空余內存與歷史記錄最大空余內存的關系。若空余內存過小,則執行垃圾回收。
         
    */

        
    public void monitorMemory()
        
    {
            
    // 求空余內存,并計算空余內存比起最大空余內存的比例
            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.");

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

            
    // 如果內存空余率很低,則執行內存回收
            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.");
            }


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


        
    /**
         * 報告內存變化情況
         
    */

        
    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.");
        }


        
    /**
         * 記錄本次內存信息。
         
    */

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


        
    /**
         * 監測 CPU 的方法。
         
    */

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

    }





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

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

    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 每天可以投一票哦~

    本文也發表在我的其他空間。
    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 閱讀(510) 評論(0)  編輯  收藏 所屬分類: JVM
    主站蜘蛛池模板: 久久青草免费91观看| 亚洲国产精品久久久久婷婷老年| 一区二区三区观看免费中文视频在线播放| 亚洲亚洲人成综合网络| 天天拍拍天天爽免费视频| 一级毛片免费观看不卡视频| 全部在线播放免费毛片| 国产成人精品日本亚洲专一区| 亚洲av永久无码精品表情包| 亚洲成片观看四虎永久| 成年男女男精品免费视频网站| 99视频在线免费| 免费精品久久天干天干| fc2免费人成在线视频| MM1313亚洲精品无码久久| 亚洲AV无码一区二区三区牛牛| 亚洲好看的理论片电影| 亚洲人成77777在线播放网站| 日本免费一本天堂在线| 毛片免费在线观看网站| 成年黄网站色大免费全看| 日本免费人成视频在线观看| 在线观看特色大片免费网站| 一区二区免费电影| 又大又硬又粗又黄的视频免费看 | 亚洲av午夜成人片精品网站| 亚洲无线一二三四区手机| 五月婷婷亚洲综合| 无码国产亚洲日韩国精品视频一区二区三区 | 亚洲综合亚洲国产尤物| 亚洲嫩模在线观看| 久久久影院亚洲精品| 久久夜色精品国产嚕嚕亚洲av| 亚洲日韩精品无码专区网址| 亚洲日韩在线观看| 亚洲一级Av无码毛片久久精品| 亚洲精品亚洲人成在线观看下载| 亚洲国产一区二区三区| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲午夜成激人情在线影院| 77777_亚洲午夜久久多人|