![](/img/trans.png)
[英]How can I retrieve userID (INT) depending on username (VARCHAR) from mySQL in JAVA program?
[英]How can I retrieve the amount of free RAM from a Java program?
可用內存:我的目標指標。
Java :我選擇的工具。
??? : 使用后者獲得前者的好方法。
這個問題有一些很好的信息,你可能會覺得有用。
大概是這樣,使用 Java Native Interface (JNI) :
Kernel32 lib = (Kernel32) Native.loadLibrary ("kernel32",Kernel32.class);
Kernel32.MEMORYSTATUS mem = new Kernel32.MEMORYSTATUS ();
lib.GetMem(mem);
System.out.println ("Available physical memory " + mem.dwAvailPhys);
如果不求助於非便攜式或本地庫,就很難做到。
就像是
Runtime.getRuntime().freeMemory()
只會返回 JVM 可用的內存,這可能與系統范圍的可用內存不同。
這個頁面提供了一個很好的綱要。
http://blog.codebeach.com/2008/02/determine-available-memory-in-java.html
要通過執行命令free -m
來獲取free -m
RAM ,然后將其解釋如下:
Runtime runtime = Runtime.getRuntime();
BufferedReader br = new BufferedReader(
new InputStreamReader(runtime.exec("free -m").getInputStream()));
String line;
String memLine = "";
int index = 0;
while ((line = br.readLine()) != null) {
if (index == 1) {
memLine = line;
}
index++;
}
// total used free shared buff/cache available
// Mem: 15933 3153 9683 310 3097 12148
// Swap: 3814 0 3814
List<String> memInfoList = Arrays.asList(memLine.split("\\s+"));
int totalSystemMemory = Integer.parseInt(memInfoList.get(1));
int totalSystemUsedMemory = Integer.parseInt(memInfoList.get(2));
int totalSystemFreeMemory = Integer.parseInt(memInfoList.get(3));
System.out.println("Total system memory in mb: " + totalSystemMemory);
System.out.println("Total system used memory in mb: " + totalSystemUsedMemory);
System.out.println("Total system free memory in mb: " + totalSystemFreeMemory);
我知道有兩個項目已經購買了JNIWrapper並且對結果很滿意。 兩種基於 Windows 的用法。 當我將它嵌入到我們當前的項目中時,我們想知道當用戶啟動我們的應用程序 (WebStart) 時有多少可用內存,因為有很多難以調查的性能投訴(我們懷疑是 RAM 問題)。 JNIWrapper 幫助我們在啟動時收集有關可用內存、總數和 CPU 等的統計信息,因此如果用戶組抱怨,我們可以檢查我們的統計信息,看看他們是否得到了不可靠的機器。 救命。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.