Get Physical Memory or RAM Information in Java using OSHI library

Introduction

In this tutorial, we are going to explore how to use the OSHI library to get information about the physical memory (RAM) information of the computer in a Java application. By using the API provided by OSHI Java library we will write programs to show memory of the computer and information of all physical devices located on the computer.

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 use OSHI to get Physical Memory Information

To use the OSHI library ro get hardware information of the computer we need to instantiate an object of SystemInfo class.

SystemInfo systemInfo = new SystemInfo();

And call method getHardware() to get HardwareAbstractionLayer that can be used to access hardware information.

HardwareAbstractionLayer hardware = systemInfo.getHardware();

Then we use the getMemory() method to receive the object of GlobalMemory class which can be used to get physical memory information.

GlobalMemory globalMemory = hardware.getMemory();

Example 1 Get Information of the Computer Memory

In the following example Java application we use GlobalMemory class to show summary information of physical memory of the computer where the application is running on.

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

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

        System.out.println(globalMemory.toString());
    }
}
The output is:
Available: 6.7 GiB/15.8 GiB

Example 2 Get Details Information of the Computer Memory

The following Java example we access details of physical memory information. We also calculate to get used memory by subtracting the total and available memory of the computer.

The return values from OSHI library in bytes value so we can convert it to readable String by using static method FormatUtil.formatBytes().

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

public class GetRAMInfo2 {
    public static void main(String... args) {
        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        GlobalMemory globalMemory = hardware.getMemory();
        
        long usedMemory = globalMemory.getTotal() - globalMemory.getAvailable();

        System.out.println("Total memory: " + FormatUtil.formatBytes(globalMemory.getTotal()));
        System.out.println("Available memory: " + FormatUtil.formatBytes(globalMemory.getAvailable()));
        System.out.println("Used memory: " + FormatUtil.formatBytes(usedMemory));
    }
}
The output is:
Total memory: 15.8 GiB
Available memory: 6.7 GiB
Used memory: 9.2 GiB

Example 3 Get List of Physical Memory Device of the Computer

The following Java example to show how to list all physical memory devices located on the computer.

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

import java.util.List;

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

        List<PhysicalMemory> physicalMemories = globalMemory.getPhysicalMemory();
        for (PhysicalMemory physicalMemory : physicalMemories) {
            System.out.println(physicalMemory.toString());
        }
    }
}
The output is:
Bank label: BANK 2, Capacity: 16 GiB, Clock speed: 2.7 GHz, Manufacturer: Samsung, Memory type: DDR4

Example 4 Get Details of Physical Memory Bank of the Computer

In the below Java example we are going to access the details of memory devices. The FormatUtil.formatHertz() static method in the example can be used to show value in hertz to a more readable String.

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

import java.util.List;

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

        List<PhysicalMemory> physicalMemories = globalMemory.getPhysicalMemory();
        for (PhysicalMemory physicalMemory : physicalMemories) {
            System.out.println("Manufacturer: " + physicalMemory.getManufacturer());
            System.out.println("Memory type: " + physicalMemory.getMemoryType());
            System.out.println("Bank/slot label: " + physicalMemory.getBankLabel());
            System.out.println("Capacity: " + FormatUtil.formatBytes(physicalMemory.getCapacity()));
            System.out.println("Clock speed: " + FormatUtil.formatHertz(physicalMemory.getClockSpeed()));
        }
    }
}
The output is:
Manufacturer: Samsung
Memory type: DDR4
Bank/slot label: BANK 2
Capacity: 16 GiB
Clock speed: 2.7 GHz

Happy Coding 😊

Get Virtual Memory 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

Tags: