java.lang包下ManagementFactory类的使用

监控jvm运行信息,除了采用jvisualVM 等可视化工具,还可通过java.lang.management.*包下的ManagementFactory类来编程式打印JVM运行信息。

创建一个实体类:

public class SystemInfoBean {
    // 加载类的数量
	private int loadClazzCount;
	// 已经加载类的数量
	private long hasloadClazzCount;
	// 尚未加载类的数量
	private long hasUnloadClazzCount;
	// 堆内存信息
	private MemoryUsage heapMemoryUsage;
	// 非堆内存信息
	private MemoryUsage nonHeapMemoryUsage;
	// 操作系统的名称
	private String operateName;
	// 操作系统的进程数
	private int processListCount;
	// 操作系统的架构
	private String archName;
	// 操作系统的版本号码
	private String versionName;
	// 虚拟机的名称
	private String vmName;
	// 虚拟机的版本
	private String vmVersion;
	// 系统的供应商的名称
	private String vmVendor;
	// JVM启动时间
	private Date startTime;
	// 输入参数
	private List<String> arguments;
	// 系统参数
	private Map<String, String> systemProperties;
}
工具类获得系统线程、堆栈内存等信息
public class SystemInfoUtils {
    private SystemInfoBean infoBean = null;
    private static class SingletonClassInstance {
        private static final SystemInfoUtils instance = new SystemInfoUtils();
    }

    public static SystemInfoUtils getInstance() {
        return SingletonClassInstance.instance;
    }

    public static void main(String[] args) {
        SystemInfoUtils utils=new SystemInfoUtils();
        System.out.println(utils.getSystemInfo().getArchName());
    }
    private SystemInfoUtils() {
        infoBean = new SystemInfoBean();
// 操作系统信息
        OperatingSystemMXBean operateSystemMBean = ManagementFactory.getOperatingSystemMXBean();
        String operateName = operateSystemMBean.getName();
        infoBean.setOperateName(operateName);
        int processListCount = operateSystemMBean.getAvailableProcessors();
        infoBean.setProcessListCount(processListCount);
        String archName = operateSystemMBean.getArch();// System.getProperty("os.arch");
        infoBean.setArchName(archName);
        String versionName = operateSystemMBean.getVersion();// System.getProperty("os.version");
        infoBean.setVersionName(versionName);

// 运行时信息
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        String vmName = runtimeMXBean.getVmName();
        infoBean.setVmName(vmName);
        String vmVersion = runtimeMXBean.getVmVersion();
        infoBean.setVmVersion(vmVersion);
        infoBean.setVmVersion(System.getProperty("java.version") + " (" + vmVersion + ")");
        String vmVendor = runtimeMXBean.getVmVendor();
        infoBean.setVmVendor(vmVendor);
        long startTime = runtimeMXBean.getStartTime();
        infoBean.setStartTime(new Date(startTime));

        infoBean.setArguments(runtimeMXBean.getInputArguments());
        infoBean.setSystemProperties(runtimeMXBean.getSystemProperties());
    }

    public SystemInfoBean getSystemInfo() {
// 类信息
        ClassLoadingMXBean classLoadMXBean = ManagementFactory.getClassLoadingMXBean();
        int loadClazzCount = classLoadMXBean.getLoadedClassCount();
        infoBean.setLoadClazzCount(loadClazzCount);
        long hasloadClazzCount = classLoadMXBean.getTotalLoadedClassCount();
        infoBean.setHasloadClazzCount(hasloadClazzCount);
        long hasUnloadClazzCount = classLoadMXBean.getUnloadedClassCount();
        infoBean.setHasUnloadClazzCount(hasUnloadClazzCount);

// 内存
        MemoryMXBean memoryUsage = ManagementFactory.getMemoryMXBean();
        infoBean.setHeapMemoryUsage(memoryUsage.getHeapMemoryUsage());
        infoBean.setNonHeapMemoryUsage(memoryUsage.getNonHeapMemoryUsage());
        return infoBean;
    }
}


猜你喜欢

转载自blog.csdn.net/daybreak1209/article/details/77504620
今日推荐