Skip to content

zleonov/java-system-monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java System Monitor

A lightweight Java library for monitoring Java Virtual Machine (JVM) and system-level CPU and memory usage.

Overview

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.

Usage example

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

Documentation

Please refer to the Wiki for details, specifications, API examples, and FAQ.

The latest API documentation can be accessed here.

Requirements

  • Java 8 or higher

Similar libraries

  • OSHI - A free JNA-based (native) Operating System and Hardware Information library for Java.
  • Sigar - System Information Gatherer And Reporter (no longer updated).