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
// 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.
Similar Reads
TimeUnit toDays() method in Java with Examples The toDays() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Days, since midnight UTC on the 1st January 1970. It is equivalent to DAYS.convert(duration, this). Syntax: public long toDays(long duration) Parameters: This method accepts a mandatory
2 min read
TimeUnit toNanos() method in Java with Examples The toNanos() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of NanoSeconds, since midnight UTC on the 1st January 1970.Syntax: public long toNanos(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration
2 min read
TimeUnit toHours() method in Java with Examples The toHours() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Hours, since midnight UTC on the 1st January 1970. Syntax: public long toHours(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration in mi
2 min read
TimeUnit toMicros() method in Java with Examples The toMicros() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of MicroSeconds, since midnight UTC on the 1st January 1970. Syntax: public long toMicros(long duration) Parameters: This method accepts a mandatory parameter duration which is the durat
2 min read
Period toTotalMonths() method in Java with Examples The toTotalMonths() method of Period Class is used to obtain the total number of Months in the given period. It returns a long value depicting the same. Syntax: public long toTotalMonths() Parameters: This method does not accepts any parameter. Returns: This function returns the long value which is
2 min read
TimeUnit toMinutes() method in Java with Examples The toMinutes() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Minutes, since midnight UTC on the 1st January 1970. Syntax: public long toMinutes(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration
2 min read
TimeUnit toSeconds() method in Java with Examples The toSeconds() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Seconds, since midnight UTC on the 1st January 1970. Syntax: public long toSeconds(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration
2 min read
TimeUnit toMillis() method in Java with Examples The toMillis() method of TimeUnit Class is used to get the time represented by the TimeUnit object, as the number of Seconds, since midnight UTC on the 1st January 1970. Syntax: public long toMillis(long duration) Parameters: This method accepts a mandatory parameter duration which is the duration i
2 min read
Year until() Method in Java with Examples until() method of the Year class used to calculate the amount of time between two Year objects using TemporalUnit. The start and end points are this and the specified Year passed as a parameter. The result will be negative if the end is before the start. The calculation returns a whole number, repre
2 min read
OffsetTime until() Method in Java with Examples until() method of the OffsetTime class used to calculate the amount of time between two OffsetTime objects using TemporalUnit. The start and end points are this and the specified OffsetTime passed as a parameter. The result will be negative if the end is before the start. The calculation returns a w
2 min read