Open In App

Java Runtime freeMemory() Method with Examples

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

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




// Java program to Demonstrate Java Runtime freeMemory()
// Driver class
public class GfgMemoryMonitorBasic {
    //main function
    public static void main(String[] args) {
        // Get the runtime object
        Runtime unexpectedRuntime = Runtime.getRuntime();
  
        long freeUnexpectedMemoryBefore = unexpectedRuntime.freeMemory();
        System.out.println("Unusual Free Memory Before (MB): " + (freeUnexpectedMemoryBefore / (1024 * 1024)));
  
        // Allocate some unexpected memory
        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




// Java program to demonstrate Java 
// Runtime class freeMemory() function
import java.util.ArrayList;
import java.util.List;
  
//Driver class
public class MysteriousMemoryMonitor {
    //main function
    public static void main(String[] args) {
        // Create a list to store mysterious memory chunks
        List<Integer[]> mysteryList = new ArrayList<>();
  
        // Get a reference to the Java Runtime
        Runtime mysteryRuntime = Runtime.getRuntime();
  
        // Run an infinite loop
        while (true) {
            // Retrieve and print the amount of free memory in megabytes
            long freeMysteryMemory = mysteryRuntime.freeMemory();
            System.out.println("Mysterious Free Memory (MB): " + (freeMysteryMemory / (1024 * 1024)));
  
            // Add a mysterious memory chunk to the list
            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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads