本文整理汇总了Java中java.lang.management.OperatingSystemMXBean类的典型用法代码示例。如果您正苦于以下问题:Java OperatingSystemMXBean类的具体用法?Java OperatingSystemMXBean怎么用?Java OperatingSystemMXBean使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OperatingSystemMXBean类属于java.lang.management包,在下文中一共展示了OperatingSystemMXBean类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendLoad
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
private void sendLoad() {
long freeRam = 0;
double cpuLoad = 0;
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
method.setAccessible(true);
if (method.getName().startsWith("get")
&& Modifier.isPublic(method.getModifiers())) {
Object value = 0;
try {
value = method.invoke(operatingSystemMXBean);
} catch (Exception e) {
e.printStackTrace();
}
if (method.getName().equals("getFreePhysicalMemorySize"))
freeRam = (long) value;
if (method.getName().equals("getSystemCpuLoad"))
cpuLoad = (double) value;
}
}
Daemon.getInstance().getClient().getChannel().writeAndFlush(new PacketServerLoad(freeRam, cpuLoad));
}
示例2: start
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void start() {
if (started) throw new IllegalStateException("Already Started.");
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
Runtime r = Runtime.getRuntime();
int mb = 0x100000;
lastSystemTime = System.nanoTime();
lastProcessCpuTime = calculateProcessCpuTime(os);
Async.task("AsyncInfoMonitorThread", () -> {
threadCount = thread.getThreadCount();
availableProcessors = r.availableProcessors();
freeMemory = Runtime.getRuntime().freeMemory() / mb;
maxMemory = Runtime.getRuntime().maxMemory() / mb;
totalMemory = Runtime.getRuntime().totalMemory() / mb;
cpuUsage = calculateCpuUsage(os);
vpsCPUUsage = getVpsCPUUsage(os);
vpsFreeMemory = calculateVPSFreeMemory(os);
vpsMaxMemory = calculateVPSMaxMemory(os);
vpsUsedMemory = vpsMaxMemory - vpsFreeMemory;
}, 1);
started = true;
}
示例3: getOsInfo
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void getOsInfo() {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
//os general info
String osName = osBean.getName();
String osVersion = osBean.getVersion();
String osArch = osBean.getArch();
//CPU
int cores = osBean.getAvailableProcessors();
String cpuName = System.getenv("PROCESSOR_IDENTIFIER");
//only on *nix systems
double loadAvg = osBean.getSystemLoadAverage();
if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
getExtendedOsInfo((com.sun.management.OperatingSystemMXBean) osBean);
}
}
示例4: call
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
@Override
public String call() {
Map<String, String> memoryMap = new HashMap<>();
OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
double cpu = osMxBean.getSystemLoadAverage();
memoryMap.put("cpuLoad", String.format(doubleStringFormatter, round(cpu, doubleRoundPlaces)));
ThreadMXBean threadmxBean = ManagementFactory.getThreadMXBean();
int threadCount = threadmxBean.getThreadCount();
memoryMap.put("cpuRunningThreads", String.valueOf(threadCount));
MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memHeapUsage = memBean.getHeapMemoryUsage();
MemoryUsage nonHeapUsage = memBean.getNonHeapMemoryUsage();
memoryMap.put("heapInit", String.valueOf(memHeapUsage.getInit()));
memoryMap.put("heapMax", String.valueOf(memHeapUsage.getMax()));
memoryMap.put("heapCommit", String.valueOf(memHeapUsage.getCommitted()));
memoryMap.put("heapUsed", String.valueOf(memHeapUsage.getUsed()));
memoryMap.put("nonHeapInit", String.valueOf(nonHeapUsage.getInit()));
memoryMap.put("nonHeapMax", String.valueOf(nonHeapUsage.getMax()));
memoryMap.put("nonHeapCommit", String.valueOf(nonHeapUsage.getCommitted()));
memoryMap.put("nonHeapUsed", String.valueOf(nonHeapUsage.getUsed()));
return memoryMap.toString();
}
示例5: getFreePhysicalMemorySize
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* Returns free physical memory in MB. If it is run in a 32-bit JVM it may return only a maximum
* of 2GB even if more memory is available.
* <p>
* <strong>Attention:</strong> The returned value is dependant on the OS. For example on Linux
* you might see less memory than there is actually available due to the OS using it for disk
* caching. That memory is not actually blocked because the OS would release it if requested,
* however this method will return the free memory which does NOT contain the ram used for disk
* caching. Use with care!
* </p>
*
* @return the free phsycial memory in MB or <code>-1</code> if we cannot determine it
* @since 6.0.004
*
*/
public static Long getFreePhysicalMemorySize() {
OperatingSystemMXBean operatingSystemBean = getOperatingSystemBean();
long memory = 0L;
// if the system bean is an implementation by sun, we are almost done
try {
memory = ((com.sun.management.OperatingSystemMXBean) operatingSystemBean).getFreePhysicalMemorySize();
} catch (Throwable t) { // NOPMD
// fallback due to sun implementation not being available
// in this case we behave as before 6.0.004 where we did not take free memory into
// account
return -1L;
}
memory /= TENTH_POWER_OF_TWO; // kbyte
memory /= TENTH_POWER_OF_TWO; // mbyte
return memory;
}
示例6: getMaxJvmHeapMemory
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* 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;
}
示例7: getSizeOfFreeHeapMemory
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* 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();
}
示例8: bindTo
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
@Override
public void bindTo(MeterRegistry registry) {
Runtime runtime = Runtime.getRuntime();
Gauge.builder("system.cpu.count", runtime, Runtime::availableProcessors)
.tags(tags)
.description("The number of processors available to the Java virtual machine")
.register(registry);
OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
if(operatingSystemBean != null && operatingSystemBean.getSystemLoadAverage() >= 0) {
Gauge.builder("system.load.average.1m", operatingSystemBean, OperatingSystemMXBean::getSystemLoadAverage)
.tags(tags)
.description("The sum of the number of runnable entities queued to available processors and the number " +
"of runnable entities running on the available processors averaged over a period of time")
.register(registry);
}
}
示例9: logSystemInfo
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void logSystemInfo() {
try {
OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
Log.info("Operating system"
+ " name: " + operatingSystemBean.getName()
+ " version: " + operatingSystemBean.getVersion()
+ " architecture: " + operatingSystemBean.getArch());
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
Log.info("Java runtime"
+ " name: " + runtimeBean.getVmName()
+ " vendor: " + runtimeBean.getVmVendor()
+ " version: " + runtimeBean.getVmVersion());
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
Log.info("Memory limit"
+ " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb"
+ " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb");
Log.info("Character encoding: "
+ System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset());
} catch (Exception error) {
Log.warning("Failed to get system info");
}
}
示例10: getOSMemory
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
private static long getOSMemory(String methodName, String warning) {
long ram = 0;
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
Method m;
try {
m = operatingSystemMXBean.getClass().getDeclaredMethod(methodName);
m.setAccessible(true);
Object value = m.invoke(operatingSystemMXBean);
if (value != null) {
ram = Long.valueOf(value.toString()) / 1024 / 1024;
} else {
OneClientLogging.logger.warn(warning);
ram = 1024;
}
} catch (Exception e) {
OneClientLogging.error(e);
}
return ram;
}
示例11: readCPUUsage
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
public static Map<String, Double> readCPUUsage(OperatingSystemMXBean osMBean) {
Map<String, Double> m = new LinkedHashMap<String, Double>();
Double procCPU = (Double) ReflectionHelper.invoke("com.sun.management.OperatingSystemMXBean", osMBean,
"getProcessCpuLoad", null, null);
Double systemCPU = (Double) ReflectionHelper.invoke("com.sun.management.OperatingSystemMXBean", osMBean,
"getSystemCpuLoad", null, null);
if (procCPU == null) {
procCPU = getProcessCpuUtilization();
systemCPU = -1D;
}
DecimalFormat formatter = new DecimalFormat("00.0");
m.put("cpu_p", Double.valueOf(formatter.format(procCPU * 100)));
m.put("cpu_s", Double.valueOf(formatter.format(systemCPU * 100)));
return m;
}
示例12: getProcessCpuUtilization
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* obtain current process cpu utilization if jdk version is 1.6 by-hongqiangwei
*/
@SuppressWarnings("restriction")
public static double getProcessCpuUtilization() {
com.sun.management.OperatingSystemMXBean osMBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
long processCpuTime1 = osMBean.getProcessCpuTime();
long runtime1 = runtimeMXBean.getUptime();
ThreadHelper.suspend(50);
long processCpuTime2 = osMBean.getProcessCpuTime();
long runtime2 = runtimeMXBean.getUptime();
long deltaProcessTime = processCpuTime2 - processCpuTime1;
long deltaRunTime = (runtime2 - runtime1) * 1000000L;
int cpuNumber = Runtime.getRuntime().availableProcessors();
double cpuUtilization = (double) deltaProcessTime / (deltaRunTime * cpuNumber);
return cpuUtilization;
}
示例13: readCPUUsage
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
protected void readCPUUsage(MonitorElementInstance inst) {
OperatingSystemMXBean osMBean = ManagementFactory.getOperatingSystemMXBean();
Double procCPU = (Double) ReflectionHelper.invoke("com.sun.management.OperatingSystemMXBean", osMBean,
"getProcessCpuLoad", null, null);
Double systemCPU = (Double) ReflectionHelper.invoke("com.sun.management.OperatingSystemMXBean", osMBean,
"getSystemCpuLoad", null, null);
if (procCPU == null) {
procCPU = JVMToolHelper.getProcessCpuUtilization();
systemCPU = -1D;
}
inst.setValue("cpu_p", Double.valueOf(formatter.format(procCPU * 100)));
inst.setValue("cpu_s", Double.valueOf(formatter.format(systemCPU * 100)));
}
示例14: start
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
public static void start() {
if (started) throw new IllegalStateException("Already Started.");
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
Runtime r = Runtime.getRuntime();
int mb = 0x100000;
lastSystemTime = System.nanoTime();
lastProcessCpuTime = calculateProcessCpuTime(os);
Async.task("AsyncInfoMonitorThread", () -> {
threadCount = thread.getThreadCount();
availableProcessors = r.availableProcessors();
freeMemory = Runtime.getRuntime().freeMemory() / mb;
maxMemory = Runtime.getRuntime().maxMemory() / mb;
totalMemory = Runtime.getRuntime().totalMemory() / mb;
cpuUsage = calculateCpuUsage(os);
vpsCPUUsage = getVpsCPUUsage(os);
vpsFreeMemory = calculateVPSFreeMemory(os);
vpsMaxMemory = calculateVPSMaxMemory(os);
vpsUsedMemory = vpsMaxMemory - vpsFreeMemory;
}, 1);
started = true;
}
示例15: scaleForAvailableMemory
import java.lang.management.OperatingSystemMXBean; //导入依赖的package包/类
/**
* Scale the value with the available memory. If 1 GB of RAM is available,
* the value is returned, if 2 GB are available, then twice the value, and
* so on.
*
* @param value the value to scale
* @return the scaled value
*/
public static int scaleForAvailableMemory(int value) {
long maxMemory = Runtime.getRuntime().maxMemory();
if (maxMemory != Long.MAX_VALUE) {
// we are limited by an -XmX parameter
return (int) (value * maxMemory / (1024 * 1024 * 1024));
}
try {
OperatingSystemMXBean mxBean = ManagementFactory
.getOperatingSystemMXBean();
// this method is only available on the class
// com.sun.management.OperatingSystemMXBean, which mxBean
// is an instance of under the Oracle JDK, but it is not present on
// Android and other JDK's
Method method = Class.forName(
"com.sun.management.OperatingSystemMXBean").
getMethod("getTotalPhysicalMemorySize");
long physicalMemorySize = ((Number) method.invoke(mxBean)).longValue();
return (int) (value * physicalMemorySize / (1024 * 1024 * 1024));
} catch (Exception e) {
// ignore
}
return value;
}