這些日子要用爪哇語言(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; ++j != runLoopTimes; )
{
// 檢查系統情況
monitor.checkAll();

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

// 每次檢查之後,會制造一些對象,記錄其中一部分,並刪除些老對象
for (int i = -1; ++i != 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; ++i != 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.
-----------

檔案下載:39253835

 

本文也發表在其他空間。
CSDN : http://blog.csdn.net/shanelooli/article/details/8176938
ITeye : http://surmounting.iteye.com/blog/1724328
開源中國: http://my.oschina.net/shane1984/blog/88803

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()