RuntimeによるJavaシステムリソースの監視


ここ数日ジャワ言語(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は1台のマシンで1つのジャワ(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-1-6 Tuesdayから2012-11-20 Tuesdayまで)ブログコンテストに参加しました.ようこそ一票を投じてください~
投票先:http://blog.51cto.com/contest2012/5523233 IPごとに1日1票を投じることができますよ~
本文も私の他の空間に発表します.
CSDN : http://blog.csdn.net/shanelooli/article/details/8176938
ITeye : http://surmounting.iteye.com/blog/1724328
51CTO : http://shanelooli.blog.51cto.com/5523233/1058490