Java Runtime totalMemory() Method with Examples
Last Updated :
23 Oct, 2023
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
public class GfgTotalMemoryExample {
public static void main(String[] args)
{
Runtime runtime = Runtime.getRuntime();
long totalMemory = runtime.totalMemory();
long totalMemoryInMB = totalMemory / ( 1024 * 1024 );
System.out.println( "Total Memory (bytes): "
+ totalMemory);
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
public class GfgMemoryCalculator {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long usedMemory = totalMemory - freeMemory;
long totalMemoryInMB = totalMemory / ( 1024 * 1024 );
long freeMemoryInMB = freeMemory / ( 1024 * 1024 );
long usedMemoryInMB = usedMemory / ( 1024 * 1024 );
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.
Share your thoughts in the comments
Please Login to comment...