Java Runtime freeMemory() Method with Examples
Last Updated :
23 Oct, 2023
In Java, memory management plays a crucial role. It works on dynamic memory allocation and release. Java’s Runtime class provides a number of ways to interact with the runtime environment, including memory-related operations. In this article, look into the freeMemory() in-built method provided by JDK, which tells you how much free memory the Java Virtual Machine (JVM) at runtime of program0.
This feature provides programmers flexibility to do memory management in Runtime.
Java Runtime freeMemory()
The java.lang.Runtime class’s freeMemory() method returns the amount of free memory in the JVM. It offers information about available memory, which is useful when optimizing memory usage or monitoring Java programs.
Syntax:
public long freeMemory();
Return Type: long – the amount of free memory in bytes.
Example of Java Runtime freeMemory()
Example 1 (Basic Usage of freeMemory()):
Let’s begin with a simple example. In our Java application, we’ll utilize the freeMemory() method to determine the amount of free memory before and after allocating memory.
Java
public class GfgMemoryMonitorBasic {
public static void main(String[] args) {
Runtime unexpectedRuntime = Runtime.getRuntime();
long freeUnexpectedMemoryBefore = unexpectedRuntime.freeMemory();
System.out.println( "Unusual Free Memory Before (MB): " + (freeUnexpectedMemoryBefore / ( 1024 * 1024 )));
int [] unexpectedArray = new int [5_000_000];
long freeUnexpectedMemoryAfter = unexpectedRuntime.freeMemory();
System.out.println( "Unusual Free Memory After (MB): " + (freeUnexpectedMemoryAfter / ( 1024 * 1024 )));
long memoryConsumed = freeUnexpectedMemoryBefore - freeUnexpectedMemoryAfter;
System.out.println( "Unusual Memory Consumed (MB): " + (memoryConsumed / ( 1024 * 1024 )));
}
}
|
Output :
Unusual Free Memory Before (MB): 169
Unusual Free Memory After (MB): 162
Unusual Memory Consumed (MB): 7
Example 2 (Memory Monitoring using freeMemory()):
We’ll build a memory monitoring program in this program that continuously allocates memory chunks and keeps track of available memory.
Below is the implementation of the above method:
Java
import java.util.ArrayList;
import java.util.List;
public class MysteriousMemoryMonitor {
public static void main(String[] args) {
List<Integer[]> mysteryList = new ArrayList<>();
Runtime mysteryRuntime = Runtime.getRuntime();
while ( true ) {
long freeMysteryMemory = mysteryRuntime.freeMemory();
System.out.println( "Mysterious Free Memory (MB): " + (freeMysteryMemory / ( 1024 * 1024 )));
Integer[] mysteriousChunk = new Integer[1_000_000];
mysteryList.add(mysteriousChunk);
}
}
}
|
Output :
Mysterious Free Memory (MB): 169
Mysterious Free Memory (MB): 158
Mysterious Free Memory (MB): 148
Mysterious Free Memory (MB): 138
Mysterious Free Memory (MB): 128
Mysterious Free Memory (MB): 118
Mysterious Free Memory (MB): 108
Mysterious Free Memory (MB): 98
Mysterious Free Memory (MB): 88
Mysterious Free Memory (MB): 78
Mysterious Free Memory (MB): 68
Mysterious Free Memory (MB): 58
Mysterious Free Memory (MB): 48
Mysterious Free Memory (MB): 38
Mysterious Free Memory (MB): 28
Mysterious Free Memory (MB): 18
Mysterious Free Memory (MB): 8
Mysterious Free Memory (MB): 0
These programs explain the fundamental use of Java’s freeMemory() function for both one-time memory allocation and continual memory monitoring. You can use them to see how free memory changes as memory is allocated and released.
For tracking and optimizing memory usage in Java programs, use the freeMemory() function. You may guarantee that your programs function effectively by making decisions based on your understanding of the free memory that is available.
Share your thoughts in the comments
Please Login to comment...