一、獲取JVM內存空間
????????系統環境:WIN
????????JDK版本:1.8re
????????直接調用Runtime中相應的方法即可:
public long maxMemory()
?
Returns the maximum amount of memory that the Java virtual machine will attempt to use.?
If there is no inherent limit then the value Long.MAX_VALUE will be returned.
?
Returns:
? ? the maximum amount of memory that the virtual machine will attempt to use, measured in bytes
Since:
? ? 1.4?
????????以上為Java8的API,maxMemory( ) 返回Java虛擬機當前狀態能使用的最大內存大小。如果沒有參數限制,將會返回Long.MAX_VALUE這個值。敲黑板!Long.MAX_VALUE這個值是什么呢,這個Long.MAX_VALUE是JVM能夠在系統中可以擴展到的最大的內存大小。
public long totalMemory()
?
Returns the total amount of memory in the Java virtual machine.?
The value returned by this method may vary over time, depending on the host environment.
?
Note that the amount of memory required to hold an object of any given type may be implementation-dependent.
?
Returns:
? ? the total amount of memory currently available for current and future objects, measured in bytes.?
????????totalMemory( ) 返回Java虛擬機中的總內存。這個方法返回的值可能隨時間而變化,這取決于宿主操作系統環境和JVM的內存占用情況。 需要注意的是, Note that the amount of memory required to hold an object of any given type may be implementation-dependent 。不同依賴實現的虛擬機需要的能Hold住任何類型對象所需的內存大小都不太一樣。因為這取決于對象在JVM運行時是如何建立在內存中的,不同的虛擬機實現都不太一樣,拿最常用的HotSpot來說,一個對象包括:對象頭(Header)、實例數據(Instance Data)、對齊填充(Padding),而且在JAVA中需要檢測對象占用的內存大小,不像C中那么簡單sizeof( ) 就完事了。?
public long freeMemory()
?
Returns the amount of free memory in the Java Virtual Machine.?
Calling the gc method may result in increasing the value returned by freeMemory.
?
Returns:
? ? an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.?
????????freeMemory( ) 返回Java虛擬機中空閑內存的數量。這個空閑是相對于totalMemory來說的而不是maxMemory,調用GC方法可能會增大freeMemory的返回值。
二、獲取操作系統內存空間
????????依靠sun.management.ManagementFactoryHelper,不多說,看代碼。
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
?
/**
?* JAVA獲取JVM內存空間和物理內存空間
?* @author jiangyuqin
?*
?*/
public class MonitorInfoTest {
?
?? ?public static void main(String[] args) {
?
?? ??? ?// 虛擬機級內存情況查詢
?? ??? ?long vmFree = 0;
?? ??? ?long vmUse = 0;
?? ??? ?long vmTotal = 0;
?? ??? ?long vmMax = 0;
?? ??? ?int byteToMb = 1024 * 1024;
?? ??? ?Runtime rt = Runtime.getRuntime();
?? ??? ?vmTotal = rt.totalMemory() / byteToMb;
?? ??? ?vmFree = rt.freeMemory() / byteToMb;
?? ??? ?vmMax = rt.maxMemory() / byteToMb;
?? ??? ?vmUse = vmTotal - vmFree;
?? ??? ?System.out.println("JVM內存已用的空間為:" + vmUse + " MB");
?? ??? ?System.out.println("JVM內存的空閑空間為:" + vmFree + " MB");
?? ??? ?System.out.println("JVM總內存空間為:" + vmTotal + " MB");
?? ??? ?System.out.println("JVM總內存空間為:" + vmMax + " MB");
?
?? ??? ?System.out.println("======================================");
?? ??? ?// 操作系統級內存情況查詢
?? ??? ?OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
?? ??? ?String os = System.getProperty("os.name");
?? ??? ?long physicalFree = osmxb.getFreePhysicalMemorySize() / byteToMb;
?? ??? ?long physicalTotal = osmxb.getTotalPhysicalMemorySize() / byteToMb;
?? ??? ?long physicalUse = physicalTotal - physicalFree;
?? ??? ?System.out.println("操作系統的版本:" + os);
?? ??? ?System.out.println("操作系統物理內存已用的空間為:" + physicalFree + " MB");
?? ??? ?System.out.println("操作系統物理內存的空閑空間為:" + physicalUse + " MB");
?? ??? ?System.out.println("操作系統總物理內存:" + physicalTotal + " MB");
?? ??? ?
?? ??? ?// 獲得線程總數
?? ??? ?ThreadGroup parentThread;
?? ??? ?int totalThread = 0;
?? ??? ?for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
?? ??? ??? ??? ?.getParent() != null; parentThread = parentThread.getParent()) {
?? ??? ??? ?totalThread = parentThread.activeCount();
?? ??? ?}
?? ??? ?System.out.println("獲得線程總數:" + totalThread);
?? ?}
}
運行時加上參數:-Xms512m -Xmx1024m?
?