JAVA获取JVM内存空间和物理内存空间
一、獲取JVM內(nèi)存空間
????????系統(tǒng)環(huán)境:WIN
????????JDK版本:1.8re
????????直接調(diào)用Runtime中相應(yīng)的方法即可:
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虛擬機當前狀態(tài)能使用的最大內(nèi)存大小。如果沒有參數(shù)限制,將會返回Long.MAX_VALUE這個值。敲黑板!Long.MAX_VALUE這個值是什么呢,這個Long.MAX_VALUE是JVM能夠在系統(tǒng)中可以擴展到的最大的內(nèi)存大小。
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虛擬機中的總內(nèi)存。這個方法返回的值可能隨時間而變化,這取決于宿主操作系統(tǒng)環(huán)境和JVM的內(nèi)存占用情況。 需要注意的是, Note that the amount of memory required to hold an object of any given type may be implementation-dependent 。不同依賴實現(xiàn)的虛擬機需要的能Hold住任何類型對象所需的內(nèi)存大小都不太一樣。因為這取決于對象在JVM運行時是如何建立在內(nèi)存中的,不同的虛擬機實現(xiàn)都不太一樣,拿最常用的HotSpot來說,一個對象包括:對象頭(Header)、實例數(shù)據(jù)(Instance Data)、對齊填充(Padding),而且在JAVA中需要檢測對象占用的內(nèi)存大小,不像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虛擬機中空閑內(nèi)存的數(shù)量。這個空閑是相對于totalMemory來說的而不是maxMemory,調(diào)用GC方法可能會增大freeMemory的返回值。
二、獲取操作系統(tǒng)內(nèi)存空間
????????依靠sun.management.ManagementFactoryHelper,不多說,看代碼。
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
?
/**
?* JAVA獲取JVM內(nèi)存空間和物理內(nèi)存空間
?* @author jiangyuqin
?*
?*/
public class MonitorInfoTest {
?
?? ?public static void main(String[] args) {
?
?? ??? ?// 虛擬機級內(nèi)存情況查詢
?? ??? ?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內(nèi)存已用的空間為:" + vmUse + " MB");
?? ??? ?System.out.println("JVM內(nèi)存的空閑空間為:" + vmFree + " MB");
?? ??? ?System.out.println("JVM總內(nèi)存空間為:" + vmTotal + " MB");
?? ??? ?System.out.println("JVM總內(nèi)存空間為:" + vmMax + " MB");
?
?? ??? ?System.out.println("======================================");
?? ??? ?// 操作系統(tǒng)級內(nèi)存情況查詢
?? ??? ?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("操作系統(tǒng)的版本:" + os);
?? ??? ?System.out.println("操作系統(tǒng)物理內(nèi)存已用的空間為:" + physicalFree + " MB");
?? ??? ?System.out.println("操作系統(tǒng)物理內(nèi)存的空閑空間為:" + physicalUse + " MB");
?? ??? ?System.out.println("操作系統(tǒng)總物理內(nèi)存:" + physicalTotal + " MB");
?? ??? ?
?? ??? ?// 獲得線程總數(shù)
?? ??? ?ThreadGroup parentThread;
?? ??? ?int totalThread = 0;
?? ??? ?for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
?? ??? ??? ??? ?.getParent() != null; parentThread = parentThread.getParent()) {
?? ??? ??? ?totalThread = parentThread.activeCount();
?? ??? ?}
?? ??? ?System.out.println("獲得線程總數(shù):" + totalThread);
?? ?}
}
運行時加上參數(shù):-Xms512m -Xmx1024m?
?
總結(jié)
以上是生活随笔為你收集整理的JAVA获取JVM内存空间和物理内存空间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kibana可视化管理页面详细使用说明
- 下一篇: 通过Rancher安装K8s