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;
}
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");
}
}
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;
}
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;
}
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();
}
Aggregations