ManagementFactory用法
监控JVM信息的工具有JConsole。而ManagementFactory是一个可以获取JVM线程、内存、编译等信息的一个工厂类。
ManagementFactory使用:
public class JvmInfo {
private static final long MB = 1024 * 1024;
public static void printAll() {
//打印系统信息
System.out.println("===========打印系统信息==========");
printOperatingSystemInfo();
//打印编译信息
System.out.println("===========打印编译信息==========");
printCompilationInfo();
//打印类加载信息
System.out.println("===========打印类加载信息==========");
printClassLoadingInfo();
//打印运行时信息
System.out.println("===========打印运行时信息==========");
printRuntimeInfo();
//打印内存管理器信息
System.out.println("===========打印内存管理器信息==========");
printMemoryManagerInfo();
//打印垃圾回收信息
System.out.println("===========打印垃圾回收信息==========");
printGarbageCollectorInfo();
//打印vm内存
System.out.println("===========打印vm内存信息==========");
printMemoryInfo();
//打印vm各内存区信息
System.out.println("===========打印vm各内存区信息==========");
printMemoryPoolInfo();
//打印线程信息
System.out.println("===========打印线程==========");
printThreadInfo();
}
public static void printOperatingSystemInfo(){
OperatingSystemMXBean system = ManagementFactory.getOperatingSystemMXBean();
//相当于System.getProperty("os.name").
System.out.println("系统名称:"+system.getName());
//相当于System.getProperty("os.version").
System.out.println("系统版本:"+system.getVersion());
//相当于System.getProperty("os.arch").
System.out.println("操作系统的架构:"+system.getArch());
//相当于 Runtime.availableProcessors()
System.out.println("可用的内核数:"+system.getAvailableProcessors());
if(isSunOsMBean(system)){
long totalPhysicalMemory = getLongFromOperatingSystem(system,"getTotalPhysicalMemorySize");
long freePhysicalMemory = getLongFromOperatingSystem(system, "getFreePhysicalMemorySize");
long usedPhysicalMemorySize =totalPhysicalMemory - freePhysicalMemory;
System.out.println("总物理内存(M):"+totalPhysicalMemory/MB);
System.out.println("已用物理内存(M):"+usedPhysicalMemorySize/MB);
System.out.println("剩余物理内存(M):"