JvmVersion(JvmInfo jvmInfo) {
   version = jvmInfo.version();
   vmName = jvmInfo.vmName();
   vmVersion = jvmInfo.vmVersion();
   vmVendor = jvmInfo.vmVendor();
 }
Example #2
0
  static {
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

    // returns the <process id>@<host>
    long pid;
    String xPid = runtimeMXBean.getName();
    try {
      xPid = xPid.split("@")[0];
      pid = Long.parseLong(xPid);
    } catch (Exception e) {
      pid = -1;
    }
    JvmInfo info = new JvmInfo();
    info.pid = pid;
    info.startTime = runtimeMXBean.getStartTime();
    info.version = System.getProperty("java.version");
    info.vmName = runtimeMXBean.getVmName();
    info.vmVendor = runtimeMXBean.getVmVendor();
    info.vmVersion = runtimeMXBean.getVmVersion();
    info.mem = new Mem();
    info.mem.heapInit =
        memoryMXBean.getHeapMemoryUsage().getInit() < 0
            ? 0
            : memoryMXBean.getHeapMemoryUsage().getInit();
    info.mem.heapMax =
        memoryMXBean.getHeapMemoryUsage().getMax() < 0
            ? 0
            : memoryMXBean.getHeapMemoryUsage().getMax();
    info.mem.nonHeapInit =
        memoryMXBean.getNonHeapMemoryUsage().getInit() < 0
            ? 0
            : memoryMXBean.getNonHeapMemoryUsage().getInit();
    info.mem.nonHeapMax =
        memoryMXBean.getNonHeapMemoryUsage().getMax() < 0
            ? 0
            : memoryMXBean.getNonHeapMemoryUsage().getMax();
    try {
      Class<?> vmClass = Class.forName("sun.misc.VM");
      info.mem.directMemoryMax = (Long) vmClass.getMethod("maxDirectMemory").invoke(null);
    } catch (Exception t) {
      // ignore
    }
    info.inputArguments =
        runtimeMXBean
            .getInputArguments()
            .toArray(new String[runtimeMXBean.getInputArguments().size()]);
    try {
      info.bootClassPath = runtimeMXBean.getBootClassPath();
    } catch (UnsupportedOperationException e) {
      // oracle java 9
      info.bootClassPath = System.getProperty("sun.boot.class.path");
      if (info.bootClassPath == null) {
        // something else
        info.bootClassPath = "<unknown>";
      }
    }
    info.classPath = runtimeMXBean.getClassPath();
    info.systemProperties = Collections.unmodifiableMap(runtimeMXBean.getSystemProperties());

    List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    info.gcCollectors = new String[gcMxBeans.size()];
    for (int i = 0; i < gcMxBeans.size(); i++) {
      GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
      info.gcCollectors[i] = gcMxBean.getName();
    }

    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    info.memoryPools = new String[memoryPoolMXBeans.size()];
    for (int i = 0; i < memoryPoolMXBeans.size(); i++) {
      MemoryPoolMXBean memoryPoolMXBean = memoryPoolMXBeans.get(i);
      info.memoryPools[i] = memoryPoolMXBean.getName();
    }

    try {
      @SuppressWarnings("unchecked")
      Class<? extends PlatformManagedObject> clazz =
          (Class<? extends PlatformManagedObject>)
              Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
      Class<?> vmOptionClazz = Class.forName("com.sun.management.VMOption");
      PlatformManagedObject hotSpotDiagnosticMXBean = ManagementFactory.getPlatformMXBean(clazz);
      Method vmOptionMethod = clazz.getMethod("getVMOption", String.class);
      Method valueMethod = vmOptionClazz.getMethod("getValue");

      try {
        Object onError = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnError");
        info.onError = (String) valueMethod.invoke(onError);
      } catch (Exception ignored) {
      }

      try {
        Object onOutOfMemoryError =
            vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "OnOutOfMemoryError");
        info.onOutOfMemoryError = (String) valueMethod.invoke(onOutOfMemoryError);
      } catch (Exception ignored) {
      }

      try {
        Object useCompressedOopsVmOption =
            vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseCompressedOops");
        info.useCompressedOops = (String) valueMethod.invoke(useCompressedOopsVmOption);
      } catch (Exception ignored) {
      }

      try {
        Object useG1GCVmOption = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "UseG1GC");
        info.useG1GC = (String) valueMethod.invoke(useG1GCVmOption);
      } catch (Exception ignored) {
      }

      try {
        Object initialHeapSizeVmOption =
            vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "InitialHeapSize");
        info.configuredInitialHeapSize =
            Long.parseLong((String) valueMethod.invoke(initialHeapSizeVmOption));
      } catch (Exception ignored) {
      }

      try {
        Object maxHeapSizeVmOption = vmOptionMethod.invoke(hotSpotDiagnosticMXBean, "MaxHeapSize");
        info.configuredMaxHeapSize =
            Long.parseLong((String) valueMethod.invoke(maxHeapSizeVmOption));
      } catch (Exception ignored) {
      }
    } catch (Exception ignored) {

    }

    INSTANCE = info;
  }