oshi.hardware.GlobalMemory Java Examples

The following examples show how to use oshi.hardware.GlobalMemory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OshiDevice.java    From iot-java with Eclipse Public License 1.0 7 votes vote down vote up
private static JsonObject createOshiData() {
	HardwareAbstractionLayer hal = si.getHardware();
	CentralProcessor processor = hal.getProcessor();
	double loadAverage = processor.getSystemCpuLoad() * 100;

	GlobalMemory memory = hal.getMemory();
	long availableMemory = memory.getAvailable();
	long totalMemory = memory.getTotal();
	long usedMemory = totalMemory - availableMemory;
	double memoryUtilization = (usedMemory / (double) totalMemory) * 100;

	JsonObject json = new JsonObject();
	json.addProperty("memory", memoryUtilization);
	json.addProperty("cpu", loadAverage);
	return json;
}
 
Example #2
Source File: MemSwapUsageSampler.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void sample(final IMonitoringController monitoringCtr) throws SigarException {
	if (!monitoringCtr.isMonitoringEnabled() || !monitoringCtr.isProbeActivated(SignatureFactory.createMemSwapSignature())) {
		return;
	}
	final GlobalMemory globalMemory = this.hardwareAbstractionLayer.getMemory();

	final long memoryTotal = globalMemory.getTotal();
	final long memoryAvailable = globalMemory.getAvailable();
	final long memoryUsed = memoryTotal - memoryAvailable;
	final long swapTotal = globalMemory.getSwapTotal();
	final long swapUsed = globalMemory.getSwapUsed();
	final long swapFree = swapTotal - swapUsed;

	final MemSwapUsageRecord r = new MemSwapUsageRecord(monitoringCtr.getTimeSource().getTime(),
			monitoringCtr.getHostname(), memoryTotal, memoryUsed, memoryAvailable, swapTotal, swapUsed, swapFree);
	monitoringCtr.newMonitoringRecord(r);
}
 
Example #3
Source File: OshiPlatformCacheTest.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
@Test
public void getMemory() {
    OshiPlatformCache oshi = newOshiPlatformCache();
    GlobalMemory memory = oshi.getMemory();
    Assert.assertNotNull(memory);

    long avail = memory.getAvailable();
    long total = memory.getTotal();

    Assert.assertTrue(avail > -1L);
    Assert.assertTrue(total > -1L);

    print("===MEMORY ===");
    print("  Available=[%s] (%d)", FormatUtil.formatBytes(avail), avail);
    print("  Total=[%s] (%d)", FormatUtil.formatBytes(total), total);
    print("  toString=[%s]", memory.toString());
}
 
Example #4
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public long getFreeSwap() {
    if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
        com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
        return nativeOsBean.getFreeSwapSpaceSize();
    } else if (info != null) {
        GlobalMemory memory = info.getHardware().getMemory();
        return memory.getAvailable();
    }

    return -1;
}
 
Example #5
Source File: MonitorServiceImpl.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取内存信息
 *
 * @param memory /
 * @return /
 */
private Map<String, Object> getMemoryInfo(GlobalMemory memory) {
	Map<String, Object> memoryInfo = new LinkedHashMap<>();
	memoryInfo.put("total", FormatUtil.formatBytes(memory.getTotal()));
	memoryInfo.put("available", FormatUtil.formatBytes(memory.getAvailable()));
	memoryInfo.put("used", FormatUtil.formatBytes(memory.getTotal() - memory.getAvailable()));
	memoryInfo.put("usageRate", df.format((memory.getTotal() - memory.getAvailable()) / (double) memory.getTotal() * 100));
	return memoryInfo;
}
 
Example #6
Source File: MonitorServiceImpl.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取交换区信息
 *
 * @param memory /
 * @return /
 */
private Map<String, Object> getSwapInfo(GlobalMemory memory) {
	Map<String, Object> swapInfo = new LinkedHashMap<>();
	swapInfo.put("total", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
	swapInfo.put("used", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()));
	swapInfo.put("available", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal() - memory.getVirtualMemory().getSwapUsed()));
	swapInfo.put("usageRate", df.format(memory.getVirtualMemory().getSwapTotal() <= 0 ? 0 : memory.getVirtualMemory().getSwapUsed() / (double) memory.getVirtualMemory().getSwapTotal() * 100));
	return swapInfo;
}
 
Example #7
Source File: Server.java    From boot-actuator with MIT License 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #8
Source File: Server.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #9
Source File: Server.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #10
Source File: OshiPlatformCache.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the given memory metric's value.
 *
 * @param metricToCollect the metric to collect
 * @return the value of the metric
 */
public Double getMemoryMetric(ID metricToCollect) {

    GlobalMemory mem = getMemory();

    if (PlatformMetricType.MEMORY_AVAILABLE.getMetricTypeId().equals(metricToCollect)) {
        return Double.valueOf(mem.getAvailable());
    } else if (PlatformMetricType.MEMORY_TOTAL.getMetricTypeId().equals(metricToCollect)) {
        return Double.valueOf(mem.getTotal());
    } else {
        throw new UnsupportedOperationException("Invalid memory metric to collect: " + metricToCollect);
    }
}
 
Example #11
Source File: ServerServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
/**
 * 获取内存信息
 *
 * @param memory /
 * @return /
 */
private Map<String, Object> getMemoryInfo(GlobalMemory memory) {
    Map<String, Object> memoryInfo = new LinkedHashMap<>();
    memoryInfo.put("total", FormatUtil.formatBytes(memory.getTotal()));
    memoryInfo.put("available", FormatUtil.formatBytes(memory.getAvailable()));
    memoryInfo.put("used", FormatUtil.formatBytes(memory.getTotal() - memory.getAvailable()));
    memoryInfo.put("usageRate", df.format((memory.getTotal() - memory.getAvailable()) / (double) memory.getTotal() * 100));
    return memoryInfo;
}
 
Example #12
Source File: ServerServiceImpl.java    From DimpleBlog with Apache License 2.0 5 votes vote down vote up
/**
 * 获取交换区信息
 *
 * @param memory /
 * @return /
 */
private Map<String, Object> getSwapInfo(GlobalMemory memory) {
    Map<String, Object> swapInfo = new LinkedHashMap<>();
    swapInfo.put("total", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
    swapInfo.put("used", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()));
    swapInfo.put("available", FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal() - memory.getVirtualMemory().getSwapUsed()));
    swapInfo.put("usageRate", df.format(memory.getVirtualMemory().getSwapUsed() / (double) memory.getVirtualMemory().getSwapTotal() * 100));
    return swapInfo;
}
 
Example #13
Source File: Server.java    From LuckyFrameClient with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * �����ڴ���Ϣ
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #14
Source File: Server.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #15
Source File: Server.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #16
Source File: NativeManager.java    From LagMonitor with MIT License 5 votes vote down vote up
public long getFreeSwap() {
    if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
        com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
        return nativeOsBean.getFreeSwapSpaceSize();
    } else if (info != null) {
        GlobalMemory memory = info.getHardware().getMemory();
        return memory.getAvailable();
    }

    return -1;
}
 
Example #17
Source File: Server.java    From RuoYi-Vue with MIT License 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #18
Source File: ServerInfo.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory)
{
    mem.setTotal(Arith.div(memory.getTotal(), (1024 * 1024 * 1024), 2));
    mem.setUsed(Arith.div(memory.getTotal() - memory.getAvailable(), (1024 * 1024 * 1024), 2));
    mem.setFree(Arith.div(memory.getAvailable(), (1024 * 1024 * 1024), 2));
    mem.setUsage(Arith.mul(Arith.div(memory.getTotal() - memory.getAvailable(), memory.getTotal(), 4), 100));
}
 
Example #19
Source File: OSUtils.java    From datax-web with MIT License 5 votes vote down vote up
/**
 * get total physical memory size
 * <p>
 * Keep 2 decimal
 *
 * @return available Physical Memory Size, unit: G
 */
public static double totalMemorySize() {
    GlobalMemory memory = hal.getMemory();
    double availablePhysicalMemorySize = memory.getTotal() / 1024.0 / 1024 / 1024;

    DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
    df.setRoundingMode(RoundingMode.HALF_UP);
    return Double.parseDouble(df.format(availablePhysicalMemorySize));
}
 
Example #20
Source File: SystemResourcesMetricsInitializer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void instantiateMemoryMetrics(MetricGroup metrics, GlobalMemory memory) {
	metrics.<Long, Gauge<Long>>gauge("Available", memory::getAvailable);
	metrics.<Long, Gauge<Long>>gauge("Total", memory::getTotal);
}
 
Example #21
Source File: SystemResourcesMetricsInitializer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void instantiateSwapMetrics(MetricGroup metrics, GlobalMemory memory) {
	metrics.<Long, Gauge<Long>>gauge("Used", memory::getSwapUsed);
	metrics.<Long, Gauge<Long>>gauge("Total", memory::getSwapTotal);
}
 
Example #22
Source File: Server.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #23
Source File: Server.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #24
Source File: SystemHardwareInfo.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #25
Source File: Server.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #26
Source File: Server.java    From Shiro-Action with MIT License 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #27
Source File: Server.java    From spring-boot-demo with MIT License 4 votes vote down vote up
/**
 * 设置内存信息
 */
private void setMemInfo(GlobalMemory memory) {
    mem.setTotal(memory.getTotal());
    mem.setUsed(memory.getTotal() - memory.getAvailable());
    mem.setFree(memory.getAvailable());
}
 
Example #28
Source File: SystemResourcesMetricsInitializer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void instantiateSwapMetrics(MetricGroup metrics, GlobalMemory memory) {
	metrics.<Long, Gauge<Long>>gauge("Used", memory::getSwapUsed);
	metrics.<Long, Gauge<Long>>gauge("Total", memory::getSwapTotal);
}
 
Example #29
Source File: SystemResourcesMetricsInitializer.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void instantiateMemoryMetrics(MetricGroup metrics, GlobalMemory memory) {
	metrics.<Long, Gauge<Long>>gauge("Available", memory::getAvailable);
	metrics.<Long, Gauge<Long>>gauge("Total", memory::getTotal);
}
 
Example #30
Source File: SystemResourcesMetricsInitializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static void instantiateSwapMetrics(MetricGroup metrics, GlobalMemory memory) {
	metrics.<Long, Gauge<Long>>gauge("Used", memory::getSwapUsed);
	metrics.<Long, Gauge<Long>>gauge("Total", memory::getSwapTotal);
}