Search in sources :

Example 31 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project orientdb by orientechnologies.

the class OMemory method getPhysicalMemorySize.

/**
 * Obtains the total size in bytes of the installed physical memory on this machine.
 * Note that on some VMs it's impossible to obtain the physical memory size, in this
 * case the return value will {@code -1}.
 *
 * @return the total physical memory size in bytes or {@code -1} if the size can't be obtained.
 */
public static long getPhysicalMemorySize() {
    long osMemory = -1;
    final OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
    try {
        final Method memorySize = mxBean.getClass().getDeclaredMethod("getTotalPhysicalMemorySize");
        memorySize.setAccessible(true);
        osMemory = (Long) memorySize.invoke(mxBean);
    } catch (NoSuchMethodException e) {
        if (!OLogManager.instance().isDebugEnabled())
            OLogManager.instance().warn(OMemory.class, "Unable to determine the amount of installed RAM.");
        else
            OLogManager.instance().debug(OMemory.class, "Unable to determine the amount of installed RAM.", e);
    } catch (InvocationTargetException e) {
        if (!OLogManager.instance().isDebugEnabled())
            OLogManager.instance().warn(OMemory.class, "Unable to determine the amount of installed RAM.");
        else
            OLogManager.instance().debug(OMemory.class, "Unable to determine the amount of installed RAM.", e);
    } catch (IllegalAccessException e) {
        if (!OLogManager.instance().isDebugEnabled())
            OLogManager.instance().warn(OMemory.class, "Unable to determine the amount of installed RAM.");
        else
            OLogManager.instance().debug(OMemory.class, "Unable to determine the amount of installed RAM.", e);
    }
    return osMemory;
}
Also used : Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 32 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project hazelcast by hazelcast.

the class OSInfoCollector method forEachMetric.

@Override
public void forEachMetric(Node node, BiConsumer<PhoneHomeMetrics, String> metricsConsumer) {
    OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
    try {
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_NAME, osMxBean.getName());
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_ARCH, osMxBean.getArch());
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_VERSION, osMxBean.getVersion());
    } catch (SecurityException e) {
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_NAME, "N/A");
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_ARCH, "N/A");
        metricsConsumer.accept(PhoneHomeMetrics.OPERATING_SYSTEM_VERSION, "N/A");
    }
}
Also used : OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 33 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project hazelcast by hazelcast.

the class OperatingSystemMXBeanSupport method readLongAttribute.

/**
 * Reads a long attribute from OperatingSystemMXBean.
 *
 * @param attributeName name of the attribute
 * @param defaultValue  default value if the attribute value is null
 * @return value of the attribute
 */
public static long readLongAttribute(String attributeName, long defaultValue) {
    try {
        String methodName = "get" + attributeName;
        if (GET_FREE_PHYSICAL_MEMORY_SIZE_DISABLED && methodName.equals("getFreePhysicalMemorySize")) {
            return defaultValue;
        }
        OperatingSystemMXBean systemMXBean = OPERATING_SYSTEM_MX_BEAN;
        Method method = systemMXBean.getClass().getMethod(methodName);
        try {
            method.setAccessible(true);
        } catch (Exception e) {
            return defaultValue;
        }
        Object value = method.invoke(systemMXBean);
        if (value == null) {
            return defaultValue;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        if (value instanceof Double) {
            double v = (Double) value;
            return Math.round(v * PERCENTAGE_MULTIPLIER);
        }
        if (value instanceof Number) {
            return ((Number) value).longValue();
        }
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception ignored) {
        ignore(ignored);
    }
    return defaultValue;
}
Also used : Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 34 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project Mycat-Server by MyCATApache.

the class EnvironmentInformation method getMaxJvmHeapMemory.

/**
 * The maximum JVM heap size, in bytes.
 *
 * @return The maximum JVM heap size, in bytes.
 */
public static long getMaxJvmHeapMemory() {
    long maxMemory = Runtime.getRuntime().maxMemory();
    if (maxMemory == Long.MAX_VALUE) {
        // amount of free memory unknown
        try {
            // workaround for Oracle JDK
            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
            Method method = clazz.getMethod("getTotalPhysicalMemorySize");
            maxMemory = (Long) method.invoke(operatingSystemMXBean) / 4;
        } catch (Throwable e) {
            throw new RuntimeException("Could not determine the amount of free memory.\n" + "Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.");
        }
    }
    return maxMemory;
}
Also used : Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 35 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project Mycat-Server by MyCATApache.

the class EnvironmentInformation method getSizeOfFreeHeapMemory.

/**
 * Gets an estimate of the size of the free heap memory. The estimate may vary, depending on the current
 * level of memory fragmentation and the number of dead objects. For a better (but more heavy-weight)
 * estimate, use {@link #getSizeOfFreeHeapMemoryWithDefrag()}.
 *
 * @return An estimate of the size of the free heap memory, in bytes.
 */
public static long getSizeOfFreeHeapMemory() {
    Runtime r = Runtime.getRuntime();
    long maxMemory = r.maxMemory();
    if (maxMemory == Long.MAX_VALUE) {
        // amount of free memory unknown
        try {
            // workaround for Oracle JDK
            OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
            Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
            Method method = clazz.getMethod("getTotalPhysicalMemorySize");
            maxMemory = (Long) method.invoke(operatingSystemMXBean) / 4;
        } catch (Throwable e) {
            throw new RuntimeException("Could not determine the amount of free memory.\n" + "Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.");
        }
    }
    return maxMemory - r.totalMemory() + r.freeMemory();
}
Also used : Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)88 RuntimeMXBean (java.lang.management.RuntimeMXBean)26 Method (java.lang.reflect.Method)20 IOException (java.io.IOException)15 MemoryMXBean (java.lang.management.MemoryMXBean)11 MemoryUsage (java.lang.management.MemoryUsage)8 ThreadMXBean (java.lang.management.ThreadMXBean)8 HashMap (java.util.HashMap)8 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)7 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 Status (com.alibaba.dubbo.common.status.Status)5 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Test (org.junit.Test)5 Test (org.testng.annotations.Test)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 UnknownHostException (java.net.UnknownHostException)4 Date (java.util.Date)4 ExtendedOperatingSystemMXBeanImpl (com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl)3