How to set minimum and maximum heap size in java

The heap area is one of the various memory areas that exist in the JVM. Every JVM has a heap area available. The Heap area will be created when the JVM starts. Objects and corresponding instance variables will be stored in the heap area. Every array in java is just an object, so the array will also be stored in the heap area. The heap area can be accessed by multiple threads, so data storage in the heap memory is not thread-safe. The heap area does not have to be continuous.

Program to display heap memory statistics:

Java applications can communicate with the VM by using runtime objects. The runtime class exists in the java.lang package, which is a singleton class. We can create a runtime object as follows:

Runtime r= Runtime.getRuntime();

Once we get the runtime object we will call the subsequent methods thereon object :

  • maxMemory() : t returns the amount of bytes of max memory allocated to the heap.
  • totalMemory() : It returns the amount of bytes of the total memory allocated to the heap.
  • freeMemory() :  It returns the amount of bytes of free memory present in the heap

 

// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;

// driver class
class heapMemory {

	// main method
	public static void main(String[] args)
	{
		// creating runtime time object
		Runtime r = Runtime.getRuntime();

		// displaying max memory of heap in bytes
		System.out.println("Max memory"
						+ " " + r.maxMemory());

		// displaying initial memory in bytes
		System.out.println("Initial memory"
						+ " " + r.totalMemory());

		// displaying free memory in bytes
		System.out.println("Free memory"
						+ " " + r.freeMemory());

		// displaying consume memory in bytes
		System.out.println(
			"Consume memory"
			+ " " + (r.totalMemory() - r.freeMemory()));
	}
}

Output

Max memory 134217728
Initial memory 134217728
Free memory 132286176
Consume memory 1931552

Heap memory is limited memory, but according to our requirements, we can set the maximum and minimum heap size, that is, we can increase or decrease the heap size according to our requirements. We can do this by using the following java command at runtime.

1. -Xmx to set maximum heap size (max memory)

                java -Xmx512m heapMemory

This command sets the maximum heap size as 512Mb.

 
// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;

// driver class
class heapMemory {

	// main method
	public static void main(String[] args)
	{
		double mb = 1000000;

		// creating runtime time object
		Runtime r = Runtime.getRuntime();

		// displaying max memory of heap in Mb
		System.out.println("Max memory"
						+ " " + r.maxMemory() / mb);

		// displaying initial memory in Mb
		System.out.println("Initial memory"
						+ " " + r.totalMemory() / mb);

		// displaying free memory in Mb
		System.out.println("Free memory"
						+ " " + r.freeMemory() / mb);

		// displaying consume memory in Mb
		System.out.println(
			"Consume memory"
			+ " "
			+ (r.totalMemory() - r.freeMemory()) / mb);
	}
}

Output

Max memory 134.217728
Initial memory 134.217728
Free memory 132.285184
Consume memory 1.932544

2. -Xms: we can use this command to set a minimum or initial heap size.

 java  -Xms64m heapMemory

This command set the minimum size as 64Mb i.e totalMemory().

// import required packages
import java.io.*;
import java.lang.*;
import java.util.*;

// driver class
class heapMemory {

	// main method
	public static void main(String[] args)
	{
		double mb = 1000000;

		// creating runtime time object
		Runtime r = Runtime.getRuntime();

		// displaying max memory of heap in Mb
		System.out.println("Max memory"
						+ " " + r.maxMemory() / mb);

		// displaying initial memory in Mb
		System.out.println("Initial memory"
						+ " " + r.totalMemory() / mb);

		// displaying free memory in Mb
		System.out.println("Free memory"
						+ " " + r.freeMemory() / mb);

		// displaying consume memory in Mb
		System.out.println(
			"Consume memory"
			+ " "
			+ (r.totalMemory() - r.freeMemory()) / mb);
	}
}

Output

Max memory 134.217728
Initial memory 134.217728
Free memory 132.285192
Consume memory 1.932536

 

Submit Your Programming Assignment Details