JavaSysMon is a lightweight, cross-platform Java library designed to manage OS processes and track real-time system performance metrics like CPU and memory usage. It serves as an OS-independent wrapper, acting similarly to a programmable version of the UNIX top command across different environments. Core Architecture and Mechanics
Native Integration: It is written in a combination of C and Java. The library includes pre-compiled native binaries packaged directly inside a single JAR file, abstracting away complex JNI/JNA manual configurations.
Platform Support: It natively supports Windows, Linux, macOS, and Solaris (x86).
Resource Impact: The footprint is exceptionally minimal. Because it queries the underlying OS kernel parameters directly via C, it avoids heavy JVM overhead during tracking intervals. Key Tracking Capabilities 1. CPU Metrics Tracking
JavaSysMon captures data via snapshots using the CpuTimes object. It records absolute CPU time values, requiring developers to calculate deltas between two points in time to capture accurate usage percentages. Total idle time vs. active time User-mode processing time Kernel-mode processing time Comprehensive system-wide CPU utilization 2. Memory Metrics Tracking
Unlike typical Java management beans (like MemoryMXBean) which focus strictly on the JVM Heap/Non-Heap space, JavaSysMon tracks physical, system-wide memory. Total physical memory available on the host machine Real-time free physical memory
Resident memory size (RSS) allocated to individual processes 3. Process Management
Beyond passive tracking, the library allows active system interaction.
Process Tree Traversal: Use a ProcessVisitor to inspect parent-child process hierarchies.
Process Termination: Forcibly kill specific OS processes directly from Java code based on PID. Implementation Blueprint
To use JavaSysMon, include the dependency via the Maven Central Repository and instantiate the core monitor object:
import com.jezhumble.javasysmon.JavaSysMon; import com.jezhumble.javasysmon.CpuTimes; public class SystemTracker { public static void main(String[] args) throws InterruptedException { JavaSysMon monitor = new JavaSysMon(); // 1. Memory Monitoring long totalMemory = monitor.physical().getTotalBytes(); long freeMemory = monitor.physical().getFreeBytes(); System.out.println(“Free Memory: ” + (freeMemory / 1024 / 1024) + “ MB / ” + (totalMemory / 1024 / 1024) + “ MB”); // 2. CPU Monitoring (Requires a delta calculation between two snapshots) CpuTimes initialSnapshot = monitor.cpuTimes(); Thread.sleep(1000); // Wait 1 second to sample active usage float cpuUsage = monitor.cpuTimes().getCpuUsage(initialSnapshot); System.out.println(“Real-time CPU Usage: ” + (cpuUsage100) + “%”); } } Use code with caution. Strategic Alternatives
While the jezhumble/javasysmon project established this paradigm, it has been largely unmaintained for several years. Modern setups typically favor:
OSHI (Operating System and Hardware Information): A highly active, pure-Java JNA-based library for comprehensive hardware/OS reporting.
Java System Monitor (zleonov): A modern, pure-Java lightweight alternative that offers integrated background monitoring loops and pre-built formatting utilities.
ProcessHandle API: For basic process tracking and termination, native features introduced in Java 9+ provide built-in alternatives without requiring external dependencies.
Are you looking to implement this tracking for local application logging, or are you building a dashboard infrastructure? I can provide tailored configuration examples or modern alternative frameworks depending on your tech stack. com.jezhumble.javasysmon (Java System Monitor)
Leave a Reply