Get Virtual Memory Information in Java using OSHI library

Tags: OSHI Memory

Introduction

In this tutorial, we show how to get virtual memory information of the computer in Java application with OSHI library.

Add OSHI library to the Java project

To use OSHI Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'com.github.oshi', name: 'oshi-core', version: '5.3.4'

To use OSHI Java library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>com.github.oshi</groupId>
    <artifactId>oshi-core</artifactId>
    <version>5.3.4</version>
</dependency>

For more information about the OSHI library you can visit the library repository at github.com/oshi/oshi

How to get Virtual Memory with OSHI library

To use the OSHI library we need to create an object of SystemInfo class as it provides different getters methods to access computer information.

SystemInfo systemInfo = new SystemInfo();

Then we get the HardwareAbstractionLayer object via getHardware() method.

HardwareAbstractionLayer hardware = systemInfo.getHardware();

To access the physical or virtual memory we need to get a GlobalMemory object first.

GlobalMemory globalMemory = hardware.getMemory();

Then access VirtualMemory object via getVirtualMemory() method.

VirtualMemory virtualMemory = globalMemory.getVirtualMemory();

Example 1 get summary information of virtual memory (swap file)

import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.VirtualMemory;

public class GetVirtualMemory1 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        GlobalMemory globalMemory = hardware.getMemory();
        VirtualMemory virtualMemory = globalMemory.getVirtualMemory();

        System.out.println(virtualMemory.toString());
    }
}
The output is:
Swap Used/Avail: 272.5 MiB/11.3 GiB, Virtual Memory In Use/Max=14.8 GiB/27.1 GiB

Example 2 get detail information of virtual memory

import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.VirtualMemory;
import oshi.util.FormatUtil;

public class GetVirtualMemory2 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        GlobalMemory globalMemory = hardware.getMemory();
        VirtualMemory virtualMemory = globalMemory.getVirtualMemory();

        System.out.println("Max virtual memory: " + FormatUtil.formatBytes(virtualMemory.getVirtualMax()));
        System.out.println("Virtual memory used: " + FormatUtil.formatBytes(virtualMemory.getVirtualInUse()));

        System.out.println("Total swap: " + FormatUtil.formatBytes(virtualMemory.getSwapTotal()));
        System.out.println("Swap used: " +FormatUtil.formatBytes(virtualMemory.getSwapUsed()));

        System.out.println("Pages swapped in: " + virtualMemory.getSwapPagesIn());
        System.out.println("Pages swapped out: " + virtualMemory.getSwapPagesOut());
    }
}
The output is:
Max virtual memory: 27.1 GiB
Virtual memory used: 14.8 GiB
Total swap: 11.3 GiB
Swap used: 272.5 MiB
Pages swapped in: 41686008
Pages swapped out: 9398116

Happy Coding 😊

Get Physical Memory or RAM Information in Java using OSHI library

Get Operating System Information in Java using OSHI library

Get CPU Information in Java using OSHI library

Get File System Information in Java using OSHI library