Java Code Examples for oshi.hardware.GlobalMemory#getTotal()

The following examples show how to use oshi.hardware.GlobalMemory#getTotal() . 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: 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));
}