這些日子要用爪哇語(yǔ)言(Java)做內(nèi)存數(shù)據(jù)中心。于是把用 Java 監(jiān)控運(yùn)行環(huán)境硬件資源的內(nèi)容復(fù)習(xí)了一下。爪哇類庫(kù)提供了 java.util.Runtim 類,主要負(fù)責(zé)調(diào)用爪哇虛擬機(jī)(JavaVM)外部的基層操作系統(tǒng)功能、處理基于一種叫鉤子的原理的程序、獲取系統(tǒng)資源信息以及控制調(diào)試信息生成。本文單獨(dú)利用其獲取系統(tǒng)資源信息的功能。
java.util.Runtim 類具有以下幾個(gè)方法和獲取系統(tǒng)資源信息有關(guān)。以下代碼可不是簡(jiǎn)簡(jiǎn)單單從標(biāo)準(zhǔn)類庫(kù)里邊復(fù)制出來(lái)的哦。全球目前獨(dú)此一份。

/** *//**
* 返回爪哇(Java)虛擬機(jī)可用線程數(shù)。
*
* <p>該值在特定的虛擬機(jī)調(diào)用期間可能發(fā)生更改。因此,對(duì)可用處理器數(shù)目很敏感的
* 應(yīng)用程序應(yīng)該不定期地輪詢?cè)搶傩裕⑾鄳?yīng)地調(diào)整其資源用法。</p>
*
* @return 虛擬機(jī)可用的最大處理器數(shù)目;從不小于 1
* @since 1.4
*/
public native int availableProcessors();


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


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


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


/** *//**
* 運(yùn)行垃圾回收器。
* 調(diào)用此方法意味著爪哇(Java)虛擬機(jī)做了一些努力來(lái)回收未用對(duì)象,以便能夠快速地
* 重用這些對(duì)象當(dāng)前占用的內(nèi)存。當(dāng)控制從方法調(diào)用中返回時(shí),虛擬機(jī)已經(jīng)盡最大努力回收
* 了所有丟棄的對(duì)象。
* <p>
* 名稱 <code>gc</code> 代表“垃圾回收器”。虛擬機(jī)根據(jù)需要在單獨(dú)的線程中自動(dòng)執(zhí)行
* 回收過(guò)程,甚至不用顯式調(diào)用 <code>gc</code> 方法。
* <p>
* 方法 {@link System#gc()} 是調(diào)用此方法的一種傳統(tǒng)而便捷的方式
*/
public native void gc();
我們可以看到這些都是本地方法。這意味著將 Runtime 對(duì)象遠(yuǎn)程傳遞之后,將不能得到正確執(zhí)行結(jié)果。
這些方法用起來(lái)都很簡(jiǎn)單,文檔注釋也寫得比較明白。
在高可用數(shù)據(jù)中心中,我認(rèn)為應(yīng)該根據(jù)可用 CPU 線數(shù)決定程序開(kāi)啟的線程數(shù) 。此線程數(shù)為 CPU 可用線數(shù)的某倍數(shù)。此倍數(shù)應(yīng)通過(guò)實(shí)際經(jīng)驗(yàn)所得。然后程序通過(guò)監(jiān)控 CPU 可用線數(shù),來(lái)控制線程池保留數(shù)量。
內(nèi)存的控制,我想應(yīng)該是在內(nèi)存超出警戒線時(shí)發(fā)出警報(bào) ,以向運(yùn)營(yíng)人員申請(qǐng)?jiān)黾觾?nèi)存數(shù)據(jù)中心服務(wù)器。同時(shí),應(yīng)該在內(nèi)存過(guò)滿之前,由程序執(zhí)行垃圾回收 ,以消除并無(wú)引用的老生代對(duì)象。
如果各位有對(duì)內(nèi)存數(shù)據(jù)中心的想法、建議或者質(zhì)疑,歡迎來(lái)一起討論。
下邊是我編寫的一個(gè)系統(tǒng)資源測(cè)試程序。程序里邊使用了 Runtime 類關(guān)于系統(tǒng)資源的所有方法。
package cn.spads.test.grammar;

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


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


{

/** *//**
* 此量控制程序運(yùn)行時(shí)間。此值越大,此演示程序運(yùn)行時(shí)間越長(zhǎng)。
*/
static public int runLoopTimes = 55;


/** *//**
* 此為每次檢測(cè)間隔的時(shí)間片數(shù)。此值越大,間隔時(shí)間越長(zhǎng)。
*/
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; )

{
// 檢查系統(tǒng)情況
monitor.checkAll();

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

// 每次檢查之后,會(huì)制造一些對(duì)象,記錄其中一部分,并刪除些老對(duì)象
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();
}

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

{
// 求空余內(nèi)存,并計(jì)算空余內(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)存空余率在變小,則一切正常;否則需要報(bào)告內(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);
}


/** *//**
* 報(bào)告內(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)測(cè) CPU 的方法。
*/
public void monitorCpu()

{
int cpuCount = this.context.availableProcessors();
if (cpuCount > 1) System.out.println("CPU have " + cpuCount + " processors.");
}
}
我運(yùn)行這段程序,得到結(jié)果報(bào)告。通過(guò)觀察結(jié)果報(bào)告,我得到了一些新結(jié)論。
首先, maxMemory 在一臺(tái)機(jī)器只運(yùn)行一個(gè)爪哇(Java)虛擬機(jī)的前提下,一般來(lái)說(shuō)并不會(huì)變動(dòng)。
其次,新生代內(nèi)存會(huì)很快地自動(dòng)回收,這體現(xiàn)在 freeMemory 總是不斷少量自動(dòng)增加上。
最后,爪哇(Java)虛擬機(jī)會(huì)在內(nèi)存不足時(shí),自己申請(qǐng)新的內(nèi)存。這個(gè)內(nèi)存空間也能夠根據(jù)報(bào)告大概看出一點(diǎn)原則:既不是完全通過(guò)可用內(nèi)存比例,也不是完全通過(guò)可用內(nèi)存數(shù)量。
我自己運(yùn)行的報(bào)告附在下邊
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)參加了一個(gè)博客大賽。歡迎來(lái)給我投一票~
投票地址: http://blog.51cto.com/contest2012/5523233 每個(gè) IP 每天可以投一票哦~
本文也發(fā)表在我的其他空間。
CSDN : http://blog.csdn.net/shanelooli/article/details/8176938
開(kāi)源中國(guó): http://my.oschina.net/shane1984/blog/88803
51CTO : http://shanelooli.blog.51cto.com/5523233/1058490
http://www.iteye.com/topic/1127731