一、获取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