---
title: "How can I check CPU and Memory Usage in Java?"  
description: "How can I check CPU and Memory Usage in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-13  
updated: 2023-07-14  
canonical: https://www.mindstick.com/forum/159074/how-can-i-check-cpu-and-memory-usage-in-java  
category: "java"  
tags: ["java", "memory management"]  
reading_time: 2 minutes  

---

# How can I check CPU and Memory Usage in Java?

How can I [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) [CPU](https://answers.mindstick.com/qa/102430/explain-the-significance-of-a-computer-s-central-processing-unit-cpu) and [Memory Usage](https://www.mindstick.com/forum/33530/convenience-vs-init-method-memory-usage) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

Sure, here are some ways to check CPU and [memory](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss) [usage](https://www.mindstick.com/forum/155707/what-is-the-usage-of-asp-dot-net-configuration-file) in Java:

- **Using the** `OperatingSystemMXBean` **class:** The `OperatingSystemMXBean` class provides methods for getting information about the operating system, including CPU usage and memory usage. Here is an example of how to use the `OperatingSystemMXBean` class to get CPU usage:

Java

```plaintext
import java.lang.management.OperatingSystemMXBean;

public class CPUUsage {

    public static void main(String[] args) {
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        double cpuUsage = osBean.getSystemLoadAverage();
        System.out.println("CPU usage: " + cpuUsage);
    }
}
```

- **Using the** `MemoryMXBean` **class:** The `MemoryMXBean` class provides methods for getting information about the memory usage of the Java Virtual Machine (JVM). Here is an example of how to use the `MemoryMXBean` class to get memory usage:

Java

```plaintext
import java.lang.management.MemoryMXBean;

public class MemoryUsage {

    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        long usedMemory = memoryMXBean.getHeapMemoryUsage().getUsed();
        long freeMemory = memoryMXBean.getHeapMemoryUsage().getFree();
        System.out.println("Used memory: " + usedMemory);
        System.out.println("Free memory: " + freeMemory);
    }
}
```

- **Using the** `JMX` **API:** The Java Management Extensions (JMX) API provides a way to manage Java applications and components. The JMX API can be used to get information about CPU and memory usage, as well as other metrics. Here is an example of how to use the JMX API to get CPU usage:

Java

```plaintext
import javax.management.MBeanServer;
import javax.management.ObjectName;

public class CPUUsage {

    public static void main(String[] args) {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName("java.lang:type=OperatingSystem");
        Double cpuUsage = (Double) mBeanServer.getAttribute(objectName, "SystemLoadAverage");
        System.out.println("CPU usage: " + cpuUsage);
    }
}
```


---

Original Source: https://www.mindstick.com/forum/159074/how-can-i-check-cpu-and-memory-usage-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
