A lightweight Java library for monitoring Java Virtual Machine (JVM) and system-level CPU and memory usage.
Java System Monitor provides a simple and efficient way to monitor CPU and memory resources in Java applications. It offers several monitoring strategies to suit different use cases, from background monitoring to on-demand resource checks.
Written in pure Java without JNI or JNA, the library is designed to be lightweight, easy to use, and performant, making it suitable for both development and production environments. It is not designed to substitute comprehensive hardware and platform-specific solutions like OSHI.
import static software.leonov.system.monitor.util.Formatter.*;
import static java.lang.System.out;
...
try (final SystemMonitor monitor = BackgroundSystemMonitor
.updateEvery(Duration.ofSeconds(1))
.onUpdate((cpu, memory) ->
out.printf("process-cpu: %s, system-cpu; %s used-memory: %s%n", formatPercent(cpu.getProcessCpuLoad()), formatPercent(cpu.getSystemCpuLoad()), formatDecimalBytes(memory.getUsedMemory())))
.onClose((cpu, memory) ->
out.printf("avg. process-cpu: %s, avg. system-cpu %s, max. used-memory: %s%n", formatPercent(cpu.getAverageProcessCpuLoad()), formatPercent(cpu.getAverageSystemCpuLoad()), formatDecimalBytes(memory.getMaxUsedMemory())))
.start()) { // Don't forget to start the monitor
... // Do work
} // Automatically close/stop monitor
Please refer to the Wiki for details, specifications, API examples, and FAQ.
The latest API documentation can be accessed here.
- Java 8 or higher