B4J Library B4J - Oshi Monitor (CPU, RAM, GPU, Battery, M/B, display, etc)

Hello everyone,
This is a library I use all the time. My own personal monitoring app is built on it, and it lets me keep an eye on individual running processes and their resources with ease. I personally use this application to monitor CPU and RAM resources for the processes of large bespoke applications that I've developed for clients. I used to rely on another excellent B4J library, but unlike that one, this library does not require administrator privileges.

Oshi Monitor is a lightweight B4J library built on top of the OSHI hardware‑inspection framework. It gives your B4J apps real‑time access to system metrics such as CPU load, RAM usage, CPU temperature (where applicable), and per‑process resource consumption, GPU model and RAM, Battery capacity and usage state, main board model and bios version, sound card and virtual memory, OS name, version and build, plus much more.

The library runs its own background thread, raises regular update events, and includes optional smoothing, maximum value tracking, and multi-process aggregation (useful for apps like Chrome).

You can monitor the whole system or target a specific process by name, with events for CPU, RAM, temperature (where applicable), and process appearance or termination. It also exposes helper methods for listing running processes and retrieving total memory information.

This library is ideal for dashboards, diagnostics tools, performance monitors, or any B4J app that needs live system statistics with minimal overhead.

B4J library tab:
1770684595404.png


PLEASE NOTE:
To run the attached example, you will NEED to download and install Erel's jGuages library from HERE...

Monitoring all (idle) system resource usage:
1770725244632.png


Monitoring chrome 3 x 4K tabs resource usage:
1770730488147.png


Monitoring its own resources usage:
1770685453249.png


Monitoring B4J resources usage:
1770684036788.png


Selecting a currently running process:
1770684223833.png


SS_OshiMonitor

Author:
Peter Simpson
Version: 1.1
  • OshiMonitor
    OshiMonitor library for B4J.
    Uses the OSHI library to monitor CPU, RAM, and Temperature.
    • Events:
      • BatteryCharging (Value As Boolean)
      • BatteryWatts (Value As Double)
      • CpuTemp (Value As Double)
      • CpuUsage (Value As Double)
      • ProcessCpuUsage (Value As Double)
      • ProcessFound (Name As String)
      • ProcessRamUsage (Value As Double)
      • ProcessRamUsageMB (Value As Double)
      • ProcessTerminated (Name As String)
      • RamUsage (Value As Double)
      • RamUsageMB (Value As Double)
      • Updated (Stats As Map)
    • Fields:
      • Debug As Boolean
        Enables diagnostic logging.
        When true internal debug messages are written to the B4J log.
    • Functions:
      • GetBatteryCharging As Boolean
        Returns true if the battery is currently charging, false otherwise.
      • GetBatteryCurrentCapacity As Double
        Returns the current battery capacity in mAh.
      • GetBatteryDesignCapacity As Double
        Returns the battery design capacity in mAh.
      • GetBatteryWatts As Double
        Returns the current power usage in watts as reported by the battery controller.
        Note: When plugged in, this reflects the charge rate; when on battery,
        this reflects the total system power draw.
      • GetBiosVersion As String
        Returns the system BIOS or firmware version string.
      • GetCpuVoltage As Double
        Returns the current CPU voltage in volts.
      • GetCurrentPowerWatts As Double
        Returns the current power usage in watts as reported by the battery controller.
        Note: When plugged in, this reflects the charge rate; when on battery,
        this reflects the total system power draw.
      • GetDisplays As List
        Returns a list containing information about connected displays.
      • GetDriveLatency (index As Int) As Double
        Returns the average I/O latency in milliseconds for a specific drive index.
        Latency = (Total Time Spent / Total Operations)
      • GetFanSpeeds As Int()
        Returns an array containing the current speed of each system fan in RPM.
      • GetGraphicsCards As List
        Returns a List containing the name and video memory size of each graphics card.
      • GetInterfaceTrafficSpeed (index As Int) As Double()
        Returns a custom result object (or array) with current traffic speeds
        in Bytes per second for a specific interface.
      • GetLogicalDrives As List
        Returns a list containing space and mount point information for logical drives.
      • GetLogicalProcessorCount As Int
        Returns the total number of logical processors (threads).
      • GetMostActiveInterfaceIndex As Int
        Returns the index of the network interface with the highest combined
        Traffic (received + sent) since system boot.
      • GetMotherboardModel As String
        Returns the motherboard or baseboard model name.
      • GetNetworkInDrops (index As Int) As Long
        Returns the number of inbound packets dropped for a specific interface index.
      • GetNetworkInErrors (index As Int) As Long
        Returns the number of inbound errors for a specific interface index.
      • GetNetworkInterfaceNames As java.util.List
        Returns a list of names of all network interfaces.
      • GetNetworkIPv4Addresses As List
        Returns a list containing the IPv4 addresses for each active network interface.
      • GetNetworkIPv6Addresses As List
        Returns a list containing the IPv6 addresses for each active network interface.
      • GetNetworkOutErrors (index As Int) As Long
        Returns the number of outbound errors for a specific interface index.
      • GetOSBuild As String
        Returns the exact build number of the current operating system.
      • GetOSName As String
        Returns the full family and description name of the operating system.
      • GetPhysicalCoreCount As Int
        Returns the number of physical CPU cores.
      • GetProcessDetails (processName As String) As Map
        Returns a map containing detailed information about a specific process by its name.
      • GetProcessorLoad As Double()
        Returns an array of CPU load percentages for each individual logical processor.
      • GetProcessorName As String
        Returns the processor name from the system.
      • GetProcessPeakMemory (processName As String) As Long
        Returns the peak resident set size (physical memory) used by a process in bytes.
      • GetRefreshRate As Int
        Returns the current refresh rate of the primary display in Hz.
        Returns 0 if the refresh rate is unknown or cannot be determined.
      • GetSoundCards As List
        Returns a list containing the names of active sound cards or audio interfaces.
      • GetSwapTotal As Long
        Returns the total swap file size in bytes.
      • GetSwapUsed As Long
        Returns the currently used swap file size in bytes.
      • GetSystemModel As String
        Returns the physical computer system model name.
      • GetSystemUptimeHours As Double
        Returns the system uptime in hours.
      • GetSystemUptimeMinutes As Double
        Returns the system uptime in minutes.
      • GetSystemUptimeSeconds As Long
        Returns the system uptime in seconds.
      • GetTotalBytesForInterface (index As Int) As Long
        Returns the total bytes (received + sent) for a specific network interface.
      • GetTotalBytesRead As Long
        Returns total bytes read across all disks since system boot.
      • GetTotalBytesReceived As Long
        Returns the number of bytes received across all network interfaces.
      • GetTotalBytesSent As Long
        Returns the number of bytes sent across all network interfaces.
      • GetTotalBytesWritten As Long
        Returns total bytes written across all disks since system boot.
      • GetTrackedPeakMemory (processName As String) As Long
        Returns the highest combined resident set size (physical memory) used by all instances
        of a specific process name since the application started monitoring.
      • GetUsbDevices As List
        Returns a list containing the names of connected USB devices.
      • GetVirtualMemoryTotal As Long
        Returns the total virtual memory in bytes.
      • GetVirtualMemoryUsed As Long
        Returns the currently used virtual memory in bytes.
      • HasCpuTemp As Boolean
        Returns true if CPU temperature monitoring is supported.
      • Initialize (EventName As String)
        Initializes the monitor and prepares OSHI hardware access.
      • IsCharging As Boolean
        Checks if the primary battery is currently charging.
        Return true if charging, false otherwise.
      • IsInitialized As Boolean
        Returns true if the object has been initialized and not yet killed.
      • IsLaptopByBattery As Boolean
        Returns true if the system appears to be a laptop based on battery presence.
      • Kill
        Kills the ba object reference to free memory and stops the thread cleanly.
        After calling this, IsInitialized will return false.
      • ListProcesses As List
        Returns a List of unique running process names, sorted A-Z.
      • ResetMaximums
        Resets the tracked maximum CPU and RAM values for the system to zero.
      • ResetProcessMaximums
        Resets the tracked maximum CPU and RAM values for the target process to zero.
      • Start
        Starts the background monitoring thread.
        Raises Updated, CpuUsage, CpuTemp and RamUsage events at the configured interval.
      • Stop
        Stops the monitoring thread.
        The object remains initialized and can be restarted with Start.
    • Properties:
      • CpuTemp As Double [read only]
        Returns CPU temperature in C.
      • CpuUsage As Double [read only]
        Returns CPU usage as a percentage.
      • IntervalMs As Int [write only]
        Sets the update interval in milliseconds.
        Values lower than 100ms are clamped to 100ms.
      • MaxCpuUsage As Double [read only]
        Returns the highest CPU usage recorded since start or reset.
      • MaxProcessCpuUsage As Double [read only]
        Returns the highest Process CPU usage recorded since the process was targeted or reset.
      • MaxProcessRamUsage As Double [read only]
        Returns the highest Process RAM usage percentage recorded.
      • MaxProcessRamUsageMB As Double [read only]
        Returns the highest Process RAM usage in megabytes recorded.
      • MaxRamUsage As Double [read only]
        Returns the highest RAM usage percentage recorded since start or reset.
      • MaxRamUsageMB As Double [read only]
        Returns the highest RAM usage in megabytes recorded since start or reset.
      • PowerUsageWatts As Double [read only]
        Gets the current power usage in watts.
        Return Watts being used or supplied.
      • ProcessCpuUsage As Double [read only]
        Returns CPU usage for the configured target process as a percentage.
      • ProcessRamUsage As Double [read only]
        Returns RAM usage for the configured target process as a percentage of total system RAM.
      • ProcessRamUsageMB As Double [read only]
        Returns RAM usage for the configured target process in megabytes.
      • RamTotal As Long [read only]
        Returns total physical memory in bytes.
      • RamTotalGB As Double [read only]
        Returns total physical memory in gigabytes.
      • RamTotalMB As Double [read only]
        Returns total physical memory in megabytes.
      • RamUsage As Double [read only]
        Returns RAM usage as a percentage.
      • RamUsageMB As Double [read only]
        Returns used RAM in megabytes.
      • TargetProcessName As String
        Returns the current target process name.
        Returns an empty string if no process is configured.

PLEASE NOTE:
TO RUN THE ATTACHED EXAMPLE, YOU NEED TO DOWNLOAD THE THIRD-PARTY JAVA DEPENDENCIES LINKED BELOW, AS WELL AS USING THE ATTACHED POST LIBRARY.

CLICK HERE - Download Extra Libraries <<<<<<<<<<<<<<<<<<<<<<<<

D = 148 + 132

Enjoy...
 

Attachments

  • Oshi Monitor.zip
    25.4 KB · Views: 48
  • Oshi Monitor Lib.zip
    16.7 KB · Views: 53
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Update V1.10 coming soon :cool:

Initial startup stats
Is Laptop (Battery Check): true
System Model: ASUS Vivobook S 15 M5506WA_M5506WA
RAM Total: 33413771264 bytes
RAM Total: 31.12 GB
RAM Total: 31865.86 MB
RAM Usage: 45.57%
CPU Temperature Support: false
Battery Charging: false
Battery Current Capacity: 55610.0 mAh
Battery Design Capacity: 75002.0 mAh
Battery Power Usage: 0.0 W
Current Power Usage: 0.0 W
System Uptime: 2349092.0 seconds
System Uptime: 39151.53 minutes
System Uptime: 652.53 hours
Total Bytes Read: 2850183098368 bytes
Total Bytes Written: 1927137607168 bytes
Total Bytes Received: 6242574065 bytes
Total Bytes Sent: 383275679 bytes
Motherboard Model: M5506WA
BIOS Version: _ASUS_ - 1072009
OS Name: Microsoft Windows 11 (Home) build 26200
OS Build Number: 26200
Graphics Card: AMD Radeon(TM) 880M Graphics
Fan Speed Support: false
Disk 0 Latency: 0.15 ms
Processor Name: AMD Ryzen AI 9 365 w/ Radeon 880M
CPU Info: 10 Physical Cores, 20 Logical Processors
Core 0 Usage: 66.12%
Core 1 Usage: 56.49%
Core 2 Usage: 1.52%
Core 3 Usage: 0%
Core 4 Usage: 6.3%
Core 5 Usage: 3.25%
Core 6 Usage: 0%
Core 7 Usage: 0%
Core 8 Usage: 0%
Core 9 Usage: 0%
Core 10 Usage: 0%
Core 11 Usage: 1.53%
Core 12 Usage: 25.83%
Core 13 Usage: 38.7%
Core 14 Usage: 22.6%
Core 15 Usage: 12.9%
Core 16 Usage: 56.4%
Core 17 Usage: 46.85%
Core 18 Usage: 4.78%
Core 19 Usage: 3.2%
Core 0: 66.1%
----------------------------------------
[/ISPOILER]

Updates to a couple of other libraries coming shortly too...
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Hello everyone,
I've updated the Oshi Monitor library. I've added lots of extra methods and features, including laptop specific metrics (battery status and watt power state), drive information, CPU and GPU details, mainboard information with BIOS version, expanded RAM statistics, USB information, virtual memory, swap file data, individual CPU core information, and much more. There’s plenty for you to explore, so give it a try yourself.

Screenshot shows my current browser information
6 tabs open, 5 playing videos, 1 on b4x.com.
1784904229199.png


Initial startup stats
Is Laptop (Battery Check): true
System Model: ASUS Vivobook S 15 M5506WA_M5506WA
RAM Total: 33413771264 bytes
RAM Total: 31.12 GB
RAM Total: 31865.86 MB
RAM Usage: 54.13%
CPU Temperature Support: false
Battery Charging: false
Battery Current Capacity: 69953.0 mAh
Battery Design Capacity: 75002.0 mAh
Battery Power Usage: 0.0 W
Current Power Usage: 0.0 W
System Uptime: 19431.0 seconds
System Uptime: 323.85 minutes
System Uptime: 5.4 hours
Total Bytes Read: 400923246592 bytes
Total Bytes Written: 30886478336 bytes
Total Bytes Received: 1056050663 bytes
Total Bytes Sent: 117732350 bytes
Motherboard Model: M5506WA
BIOS Version: _ASUS_ - 1072009
OS Name: Microsoft Windows 11 (Home) build 26200
OS Build Number: 26200
Graphics Card: AMD Radeon(TM) 880M Graphics
Fan Speed Support: false
Logical Drive: Local Fixed Disk (C:) (561 GB free of 929 GB)
Logical Drive: Local Fixed Disk (G:) (396 GB free of 402 GB)
Display Current Refresh Rate: 120 Hz
Display: Manuf. ID=SDC, Product ID=4180, Digital, Serial=00000000, ManufDate=7/2021, EDID v1.4
34 x 19 cm (13.4 x 7.5 in)
Preferred Timing: Clock 587MHz, Active Pixels 2880x1620
Preferred Timing: Clock 587MHz, Active Pixels 2880x1620
Manufacturer Data: 0000000F00FF0978FF093C28800200000000
Unspecified Text: ATNA56AC02-1 PreferredClockMHz=586.80 MHz
USB Device: AMD USB 3.10 eXtensible Host Controller - 1.20 (Microsoft)
USB Device: AMD USB 3.10 eXtensible Host Controller - 1.20 (Microsoft)
USB Device: AMD USB 3.10 eXtensible Host Controller - 1.20 (Microsoft)
USB Device: AMD USB 3.10 eXtensible Host Controller - 1.20 (Microsoft)
Sound Card: Advanced Micro Devices AMD High Definition Audio Device
Sound Card: Realtek Semiconductor Corp. Realtek High Definition Audio
Sound Card: Microsoft Microsoft Bluetooth A2dp Source
Sound Card: AMD AMD Streaming Audio Device
Sound Card: Microsoft Microsoft Bluetooth A2dp Source
Sound Card: Microsoft Microsoft Bluetooth A2dp Sink
Sound Card: Microsoft Microsoft Bluetooth A2dp Source
CPU Voltage: 0.0 V
Virtual Memory Total: 33.12 GB
Virtual Memory Used: 22.42 GB
Swap Total: 2.0 GB
Swap Used: 0.13 GB
IPv4: wlan2: 192.168.0.189
IPv6: eth0: fe80:0:0:0:9ef1:6b7d:e17:ef58
IPv6: wlan0: fe80:0:0:0:3482:63a:99c5:ceb0
IPv6: wlan1: fe80:0:0:0:2c6:9898:f574:db4a
IPv6: wlan2: fe80:0:0:0:2eda:3100:97a5:aaab
Disk 0 Latency: 0.3 ms
----------------------------------------
Processor Name: AMD Ryzen AI 9 365 w/ Radeon 880M
CPU Info: 10 Physical Cores, 20 Logical Processors
Core 0 Usage: 19.98%
Core 1 Usage: 7.68%
Core 2 Usage: 6.2%
Core 3 Usage: 7.68%
Core 4 Usage: 9.25%
Core 5 Usage: 10.83%
Core 6 Usage: 7.68%
Core 7 Usage: 4.72%
Core 8 Usage: 7.78%
Core 9 Usage: 6.11%
Core 10 Usage: 4.54%
Core 11 Usage: 3.05%
Core 12 Usage: 4.63%
Core 13 Usage: 6.11%
Core 14 Usage: 6.19%
Core 15 Usage: 3.05%
Core 16 Usage: 16.86%
Core 17 Usage: 9.34%
Core 18 Usage: 4.72%
Core 19 Usage: 13.88%
Core 0: 20%
----------------------------------------

Enjoy...
 
Top