當前位置: 首頁>>代碼示例>>Java>>正文


Java OperatingSystemMXBean類代碼示例

本文整理匯總了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));
}
 
開發者ID:CentauriCloud,項目名稱:CentauriCloud,代碼行數:25,代碼來源:LoadTimer.java

示例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;
}
 
開發者ID:Mantaro,項目名稱:MantaroRPG,代碼行數:25,代碼來源:AsyncInfoMonitor.java

示例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);
    }
}
 
開發者ID:games647,項目名稱:Java-Snippets,代碼行數:18,代碼來源:EnvironmentInfo.java

示例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();
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:25,代碼來源:MetricsServoRegistry.java

示例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;
}
 
開發者ID:transwarpio,項目名稱:rapidminer,代碼行數:35,代碼來源:SystemInfoUtilities.java

示例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;
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:26,代碼來源:EnvironmentInformation.java

示例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();
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:28,代碼來源:EnvironmentInformation.java

示例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);
    }
}
 
開發者ID:micrometer-metrics,項目名稱:micrometer,代碼行數:19,代碼來源:ProcessorMetrics.java

示例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");
    }
}
 
開發者ID:bamartinezd,項目名稱:traccar-service,代碼行數:27,代碼來源:Log.java

示例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;
}
 
開發者ID:HearthProject,項目名稱:OneClient,代碼行數:21,代碼來源:OperatingSystem.java

示例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;
    }
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:22,代碼來源:JVMToolHelper.java

示例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;
}
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:26,代碼來源:JVMToolHelper.java

示例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)));
    }
 
開發者ID:uavorg,項目名稱:uavstack,代碼行數:17,代碼來源:JVMStateCapHandler.java

示例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;
}
 
開發者ID:natanbc,項目名稱:GabrielBot,代碼行數:25,代碼來源:AsyncInfoMonitor.java

示例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;
}
 
開發者ID:vdr007,項目名稱:ThriftyPaxos,代碼行數:32,代碼來源:Utils.java


注:本文中的java.lang.management.OperatingSystemMXBean類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。