How to find Max Memory, Free Memory and Total Memory in Java?


Memory management is an important aspect of any Java application. Having the knowledge of how much memory is available, how much is used and how much is free may help you to optimize your code and avoid performance issues or memory leaks. This article aims to help in finding max memory, free memory and total memory that are a crucial part of Java Heap. Note that the amount of memory allocated to a Java program depends on the environment.

Java Program to find Max Memory, Free Memory and Total Memory

Java provides the following methods and classes that are used to find max, free and total memory available in Java Heap memory:

  • Runtime Class

  • maxMemory() method

  • freeMemory() method

  • totalMemory() method

The above three methods are defined under the Runtime class. Let's discuss them one by one.

Runtime Class

It is a class that provides access to the Java runtime system, which includes information about the environment and the resources available to a Java application. One of the methods of the Runtime class is getRuntime(), which returns a singleton instance of the Runtime object.

The following syntax is used to define the object of Runtime class:

Syntax

Runtime nameOfObject = Runtime.getRuntime();

maxMemory()

This method checks and returns the maximum amount of memory that the Java virtual machine will attempt to utilize. It is used along with the instance of Runtime class. Note that its value depends on the platform on which the Java program is running. If a platform does not put any limit, in that case, it returns Long.MAX_VALUE.

Syntax

objectOfRuntime.maxMemory(); 

freeMemory()

This method checks and returns the amount of free memory available in the JVM. It is also used along with the instance of Runtime class. Its value may change over time as objects are allocated and garbage is collected periodically.

Syntax

objectOfRuntime.freeMemory(); 

totalMemory()

This method checks and returns the total amount of memory allocated to a Java Program. Like other methods, it is also used along with the instance of Runtime class. Its value may vary over time, as the JVM may expand or shrink the memory pool.

Syntax

objectOfRuntime.totalMemory(); 

Example 1

In this example, we will use the methods and classes that are discussed above to get the values of max, free and total memory in bytes.

public class Memory {
   public static void main(String[] args) {
      // define an instance of the Runtime class
      Runtime rntime = Runtime.getRuntime();
      // To get the max memory
      long maxMemory = rntime.maxMemory();
      System.out.println("Available max memory is: " + maxMemory + "bytes");
      // To get the free memory
      long freeMemory = rntime.freeMemory();
      System.out.println("Available free memory is: " + freeMemory + "bytes");
      // To get the total memory
      long totalMemory = rntime.totalMemory();
      System.out.println("Available total memory is: " + totalMemory + "bytes");
    }
}

Output

Available max memory is: 1073741824 bytes
Available free memory is: 268466176 bytes
Available total memory is: 270532608 bytes

Example 2

In the following example, we will change the code of the previous example to display the values of max, free and total memory in Megabytes.

public class Memory {
   public static void main(String[] args) {
      long mb = 1024 * 1024; // Use this to convert bytes into Megabytes
      // define an instance of the Runtime class
      Runtime rntime = Runtime.getRuntime();
      // To get the max memory
      long maxMm = rntime.maxMemory();
      System.out.println("Available max memory is: " + maxMm + " bytes");
      System.out.println("Max memory in MB: " + maxMm / mb + "MB");
      // To get the free memory
      long freeMm = rntime.freeMemory();
      System.out.println("Available free memory is: " + freeMm + " bytes");
      System.out.println("Free memory in MB: " + freeMm / mb + "MB");
      // To get the total memory
      long totalMm = rntime.totalMemory();
      System.out.println("Available total memory is: " + totalMm + " bytes");
      System.out.println("Total memory in MB: " + totalMm / mb + "MB");
   }
}

Output

Available max memory is: 1073741824 bytes
Max memory in MB: 1024MB
Available free memory is: 268466176 bytes
Free memory in MB: 256MB
Available total memory is: 270532608 bytes
Total memory in MB: 258MB

Conclusion

In this article, we have seen how to find the max memory, free memory and total memory in Java using the Runtime class. The values you may get as the output may differ from the above output as these values depend on the platform on which the Java program is running.

Updated on: 20-Jul-2023

407 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements