Skip to content
zhenya edited this page Aug 9, 2025 · 7 revisions

Frequently Asked Questions

Table of Contents


Do I need to use a try-with-resources statement for LazySystemMonitor?

LazySystemMonitor does not use any Closeable resources so, NO, you don't have to use it in a try-with-resources statement, but it's good practice anyway (for all Closeables).

Why does LazySystemMonitor report incorrect aggregate statistics?

Remember that LazySystemMonitor refreshes metrics on demand (each time getCpuUsage or getMemoryUsage is called). If you call these methods infrequently, your aggregate statistics will be inaccurate. Consider using BackgroundSystemMonitor instead.

Why are negative values sometimes reported for CPU usage metrics even though it's supported on my platform?

This can happen in certain cases if the metrics are not yet ready to report, usually if you call getCpuUsage immediately after the monitor is created.

Why is BackgroundSystemMonitor always returning negative values for all CPU/memory usage metrics?

Remember that you have to call start() before BackgroundSystemMonitor will start updating usage metrics.

You are using com.sun.management.OperatingSystemMXBean, isn't that an internal API that should not be relied on?

You are right that it is not part of the Public Java API. Practically speaking I have not seen a JVM where it is not available.

In Java 9 it was made accessible in the jdk.management package.

In either case, if it is not available there is a fallback mechanism to calculate process CPU metrics, while system-wide CPU metrics will be unavailable.

Clone this wiki locally