Open In App

Java Runtime totalMemory() Method with Examples

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, to access the system’s properties and resources we have the Runtime class. The totalMemory() method is a method that the Runtime class gives you to get the total memory available in the JVM.

To get the total memory in your Java Virtual Machine, use totalMemory() method which returns the total memory available. The totalMemory() returns both the heap memory and the non-hashed memory. Unlike the non-haq memory which is used for the method space and internal operations of the JVM, the heaps are used for the allocation of objects and the collection of garbage. In simpler language, a totalMemory() returns the entire memory that is initially allocated for the operation of the JVM.

Examples of totalMemory() in Java

Now, let’s explore how to use the totalMemory() method in practical scenarios.

Example 1 (Basic Usage):

First, create a Runtime object for the current Java Virtual Machine(JVM). Using the totalMemory() method will retrieve the total amount of memory available in bytes. The output is in Bytes to convert that into MB(Megabytes) and divide it by (1024*1024).

Below is the implementation of the above method:

Java




// Java Program to demonstrate 
// Java Runtime totalMemory
// Method Driver class
  
// Driver Class
public class GfgTotalMemoryExample {
    // main function
    public static void main(String[] args)
    {
        // Get the runtime object
        Runtime runtime = Runtime.getRuntime();
  
        // Get the total memory available to the JVM in
        // bytes
        long totalMemory = runtime.totalMemory();
  
        // Convert total memory from bytes to megabytes (MB)
        long totalMemoryInMB = totalMemory / (1024 * 1024);
  
        // Print the total memory in bytes
        System.out.println("Total Memory (bytes): "
                           + totalMemory);
  
        // Print the total memory in megabytes (MB)
        System.out.println("Total Memory (MB): "
                           + totalMemoryInMB);
    }
}


Output:

Total Memory (bytes): 65628160
Total Memory (MB): 62

In this example, we obtain the Runtime object and then use the totalMemory() method to retrieve the total memory in bytes. We also convert the result to megabytes for a more human-readable format.

Example 2 (Using totalMemory() in Memory Calculation):

In this example we will see how to calculate used memory, to do so we will use totatMemory() and freeMemory() and then subtract both the values to get Used Memory.

Below is the implementation of the above method:

Java




// Java program to demonstrate Java Runtime 
// totalMemory method Driver class
  
// Driver Class
public class GfgMemoryCalculator {
    //main function
    public static void main(String[] args) {
        // Get the runtime object
        Runtime runtime = Runtime.getRuntime();
  
        // Get the total memory available to the JVM in bytes
        long totalMemory = runtime.totalMemory();
  
        // Get the free memory available to the JVM in bytes
        long freeMemory = runtime.freeMemory();
  
        // Calculate the used memory in bytes
        long usedMemory = totalMemory - freeMemory;
  
        // Convert memory values from bytes to megabytes (MB)
        long totalMemoryInMB = totalMemory / (1024 * 1024);
        long freeMemoryInMB = freeMemory / (1024 * 1024);
        long usedMemoryInMB = usedMemory / (1024 * 1024);
  
        // Print the total memory, free memory, and used memory in megabytes (MB)
        System.out.println("Total Memory (MB): " + totalMemoryInMB);
        System.out.println("Free Memory (MB): " + freeMemoryInMB);
        System.out.println("Used Memory (MB): " + usedMemoryInMB);
    }
}


Output:

Total Memory (MB): 8192
Free Memory (MB): 2048
Used Memory (MB): 6144

Conclusion

In this article, we looked at the totalMemory() method used to retrieve the total memory accessible to the JVM, which is necessary for monitoring and optimising Java programs. Understanding memory allocation allows you to better manage your Java programs and guarantee they run efficiently.

You may acquire insight into the memory utilisation of your Java programs and make memory management decisions by utilizing the totalMemory() function. This approach is an excellent resource for Java developers who want to design high-performance, memory-efficient apps.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads